POST 500内部服务器错误 - PHP

时间:2017-08-08 00:20:26

标签: 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
        },
        success: function (response) {
            $('.restaurnat').html(response);
        }
    });
}

PHP代码:

<?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代码:

<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>

3 个答案:

答案 0 :(得分:3)

解决。

问题是:我忘记了:

htdocs

在页面的开头!

答案 1 :(得分:2)

您需要定位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,

This answer也可以帮到你。

答案 2 :(得分:2)

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