首页 文章

POST 500内部服务器错误 - PHP

提问于
浏览
1

一个令我困惑的奇怪问题

这段代码在localhost上完美运行

上传到服务器后,我发现:无法加载资源:服务器响应状态为500(内部服务器错误)

以及其他一些时间它显示:POST $ link 500(内部服务器错误)

Wordpress!

AJAX Code:

function fetch_select(val) {
    $.ajax({
        type: 'post',
        url: 'test2.php',
        data: {
            get_option:val
        },
        success: function (response) {
            $('.restaurnat').html(response);
        }
    });
}

PHP Code:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['get_option'])) {
        if (!empty($_POST['get_option']) && $_POST['get_option'] != 0) {
            global $wpdb;
            $rests = $wpdb->get_results("SELECT * FROM Cities WHERE ID = $_POST['get_option']", ARRAY_A);
            if (!empty($rests)) {
                foreach ($rests as $rest) {
                    echo "<option value='" . $rest['ID'] . "'>" . $rest['res'] . "</option>";
                }
            }
        } else {
            echo "<option value=''> --- Choose Country or City First --- </option>";
        }
    }
}
?>

HTML Code:

<div class='container'>
    <h1 class='text-center'>Choosing requests</h1>
    <form class='text-center form-horizontal' style='padding:10px;max-width:400px;margin:auto;' action="<?php echo $_SERVER['PHP_SELF']; ?>" method='POST'>
        <h1 class="text-center" style='font-size:3.9em;margin:40px 0;font-weight:bold;'>Choose City or Country</h1>
        <select name='city' onchange='fetch_select(this.value)'>
            <option value=''>--- Choosing City or Country ---</option>
            <?php
                global $wpdb;              
                $q = $wpdb->get_results("SELECT City_Name, ID FROM Cities ORDER BY ID ASC", ARRAY_A);
                if (!empty($q)) {
                    foreach ($q as $city) {?>
                        <option value="<?php echo $city['ID']; ?>"><?php echo $city['City_Name']; ?></option> 
                    <?php 
                    }
                } else {
                    echo "<div class='container'><div class='alert alert-warning'><p class='lead'>There is no city or Country at the database</p></div></div>";
                }
            ?>
        </select>
        
<h1 class="text-center" style='font-size:3.9em;margin:40px 0;font-weight:bold;'>Choose restaurant</h1> <select name='restaurant' class='restaurnat'> <option value=''>--- Choose Country or City first ---</option> </select> <input type='submit' value='Order now' class='btn btn-lg btn-primary' style='margin:10px;'/> </form> </div>

3 回答

  • 2

    解决了 .

    问题在于:我忘记了:

    require_once('wp-config.php');
    

    在页面的开头!

  • 3

    您需要以 admin-ajax.php 为目标来处理WordPres中的AJAX requets .

    function fetch_select(val) {
        $.ajax({
            type: 'post',
            url: 'http://example.com/wp-admin/admin-ajax.php',
            data: {
                get_option:val
            },
            success: function (response) {
                $('.restaurnat').html(response);
            }
        });
    }
    

    对于WordPress AJAX,This answer也可以帮到你 .

  • 2

    检查您的ht-access文件并将您的本地URL替换为数据库中的实时URL

相关问题