POST 500内部服务器错误--PHP 一个令我困惑的奇怪问题

POST 500内部服务器错误--PHP 一个令我困惑的奇怪问题,php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,这段代码在localhost上运行得非常好 上传到服务器后,我发现: 加载资源失败:服务器响应状态为500(内部服务器错误) 有时它会显示:POST$link 500(内部服务器错误) Wordpress AJAX代码: function fetch_select(val) { $.ajax({ type: 'post', url: 'test2.php', data: { get_option:val

这段代码在localhost上运行得非常好

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

有时它会显示:POST$link 500(内部服务器错误)

Wordpress

AJAX代码:

function fetch_select(val) {
    $.ajax({
        type: 'post',
        url: 'test2.php',
        data: {
            get_option:val
        },
        success: function (response) {
            $('.restaurnat').html(response);
        }
    });
}
<?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>";
        }
    }
}
?>
<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>
        <br />
        <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>
PHP代码:

function fetch_select(val) {
    $.ajax({
        type: 'post',
        url: 'test2.php',
        data: {
            get_option:val
        },
        success: function (response) {
            $('.restaurnat').html(response);
        }
    });
}
<?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>";
        }
    }
}
?>
<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>
        <br />
        <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>

您需要以
admin ajax.php
为目标来处理WordPres中的ajax请求

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,可能也会对您有所帮助。

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

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,可能也会对您有所帮助。

已解决。 问题是:我忘了做:

require_once('wp-config.php');
在页面的开头

解决了。 问题是:我忘了做:

require_once('wp-config.php');

在页面的开头

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

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

当您在localhost上尝试此操作时,您是使用WordPress方式还是简单PHP方式尝试AJAX?当您在localhost上尝试此操作时,我使用简单PHP方式(PDO)进行了尝试,你是用WordPress方式还是简单PHP方式尝试过AJAX?我用简单PHP方式(PDO)尝试过。问题是,该文件正在识别.PHP文件,这是一个很好的证据,当我键入echo$_POST['get_option']时;我得到了$\u POST的值,但是当我生成这样的变量$city=$\u POST['get\u option']echo$city时;我收到一个错误,你完全不知道还有其他想法吗?问题是,该文件正在识别.php文件,这是一个很好的证据,当我键入echo$\u POST['get\u option']时;我得到了$\u POST的值,但是当我生成这样的变量$city=$\u POST['get\u option']echo$city时;我犯了一个错误,你完全迷路了还有其他想法吗?把你的答案作为答案贴出来,如果这个答案还没有被别人回答,把你的答案标记为最好的答案,这样以后其他人可能会觉得它有用。@Junaid done。我只等了两天就把它标为最佳答案。t将您的解决方案作为答案,如果该解决方案尚未得到他人的回答,请将您的答案标记为最佳解决方案,以便将来其他人可能会发现它很有用。@Junaid done。我只等了两天就把它标为最佳答案。T