此代码在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>
答案 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