php - ajax 调用没有得到任何响应
问题描述
我在 wordpress 插件上有三个下拉列表,第二个值取决于第一个下拉列表的选择值,所以我使用 ajax 发送请求但没有得到任何响应这是我的代码,我在坐下并尝试使用它
索引.php
<?php
/**
* Plugin Name:index
*
* Version: 1.0
*
*/
function try_form()
{
include "connect_db.php"; ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<tr>
<td>Country</td>
<td>
<!-- Country dropdown -->
<select id='sel_country' >
<option value='0' >Select Country</option>
<?php
## Fetch countries
$stmt = $conn->prepare("SELECT * FROM cities ");
$stmt->execute();
$countriesList = $stmt->fetchAll();
foreach($countriesList as $country){
echo "<option value='".$country['ID']."'>".$country['city_name']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>State</td>
<td>
<select id='sel_state' >
<option value='0' >Select State</option>
</select>
</td>
</tr>
<tr>
<td>City</td>
<td>
<select id='sel_city' >
<option value='0' >Select City</option>
</select>
</td>
</tr>
</table>
</table>
<script type="text/javascript">
$(document).ready(function(){
// Country
$('#sel_country').change(function(){
var countryid = $(this).val();
// Empty state and city dropdown
$('#sel_state').find('option').not(':first').remove();
$('#sel_city').find('option').not(':first').remove();
// AJAX request
$.ajax({
url: "ajaxfile.php",
type: 'post',
data: {request: 1, countryid:countryid},
dataType: 'json',
success: function(response){
alert (countryid);
var len = response.length;
for( var i = 0; i<len; i++){
var ID = response[i]['ID'];
var name = response[i]['name'];
$("#sel_state").append("<option value='"+ID+"'>"+name+"</option>");
}
},
error: function(){
alert('failure');}
});
});
// State
$('#sel_state').change(function(){
var stateid = $(this).val();
// Empty city dropdown
$('#sel_city').find('option').not(':first').remove();
// AJAX request
$.ajax({
url: 'ajaxfile.php',
type: 'post',
data: {request: 2, stateid: stateid},
dataType: 'json',
success: function(response){
var len = response.length;
for( var i = 0; i<len; i++){
var id = response[i]['id'];
var name = response[i]['name'];
$("#sel_city").append("<option value='"+id+"'>"+name+"</option>");
}
}
});
});
});
</script>
<?php } add_shortcode('t', 'try_form');
ajaxfile.php 文件
<?php
include "connect_db.php";
$request = 0;
if(isset($_POST['request'])){
$request = $_POST['request'];
}
// Fetch state list by countryid
if($request == 1){
$countryid = $_POST['countryid'];
$stmt = $conn->prepare("SELECT * FROM m where mm= :country");
// $stmt->bindValue(':country', (int)$countryid, PDO::PARAM_INT);
$stmt->execute();
$statesList = $stmt->fetchAll();
$response = array();
foreach($statesList as $state){
$response[] = array(
"ID" => $state['ID'],
"name" => $state['name']
);
}
echo json_encode($response);
exit;
}
// Fetch city list by stateid
if($request == 2){
$stateid = $_POST['stateid'];
$stmt = $conn->prepare("SELECT * FROM cities WHERE state=:state ORDER BY name");
$stmt->bindValue(':state', (int)$stateid, PDO::PARAM_INT);
$stmt->execute();
$statesList = $stmt->fetchAll();
$response = array();
foreach($statesList as $state){
$response[] = array(
"id" => $state['id'],
"name" => $state['name']
);
}
echo json_encode($response);
exit;
}
所以知道为什么我没有得到任何回应
编辑我已经看到了我的浏览器开发者工具栏的网络标签,我明白了
[ https://i.stack.imgur.com/Y59jC.png ] 我将 bindValue 编辑为 bindParam,现在我遇到了失败
解决方案
推荐阅读
- go - 将两个 if 条件合二为一
- javascript - 有没有办法使用纯文本和 javascript 创建注册/登录“数据库”?
- javascript - 如何将字符串(对象)拆分为新项目中的“保存”?
- c# - 如何使 Json.NET 序列化和反序列化也实现 IDictionary 的自定义动态类型的声明属性
? - reactjs - AntDesign TimePicker React 组件未呈现为下拉菜单
- permutation - 如何在我的词频程序中使用 LineNo(单词出现的行)和 CurLine(当前行)扩展这些数据结构?
- flutter - 如何在 Flutter 中从 ByteData 加载图像小部件
- windows - Composer 卸载程序冻结
- java - 如何在数组内用填充长度填充字符串?
- ruby-on-rails - 我应该覆盖 Activerecord 的 update() 方法吗?