你能帮我解决这个问题吗?
假设我现在有这个:
HTML
<select name="insta_country_code" class="widget-user-country country-selector" data-account-name="
<?php echo $account1['Username'] ?>">
<option value="br">Brazil</option>
<option value="cl">Chile</option>
<option value="es" selected>Spain</option>
<option value="mx">Mexico</option>
<option value="gb">United Kingdom</option>
<option value="cr">Costa Rica</option>
</select>
JS
$(document).ready(function () {
$('.country-selector').change(function () {
var ig_name = $(this).data('account-name');
var country_code = $(this).val();
$.ajax({
type: 'POST',
dataType: 'json',
url: 'change-country.php',
data: {
insta_user: ig_name,
country_code: country_code,
},
success: function (data) {
if (data['status'] != 1) {
$.alert('server error');
}
},
error: function () {
$.alert('error');
}
});
})
});
我的.php
文件应该是什么?我需要做这样的事情:
$InstagramCountry = $data['country_code'];
$InstagramUsername = $data['insta_user']);
$sql = mysql_query("UPDATE igaccounts SET Country='$InstagramCountry' WHERE Username='$InstagramUsername'");
非常感谢!
当您使用POST
方法将数据发送到服务器端的PHP代码时,您可以使用$_POST
或$_REQUEST
全局变量捕获数据,如下所示 -
$InstagramCountry = $_POST['country_code'];
$InstagramUsername = $_POST['insta_user']);
你说vai评论你在ajax调用时遇到错误,你可以尝试这种方式调试错误的原因是什么 -
error: function(xhr, textStatus, error){
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
要捕获mysqli错误,(我建议您使用mysqli并避免使用旧的/已弃用的mysql),请参阅MORE
$result = mysql_query('UPDATE igaccounts SET Country='$InstagramCountry' WHERE Username='$InstagramUsername');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
对于其他类型的SQL语句,INSERT,UPDATE,DELETE,DROP等,mysql_query()在成功时返回TRUE,在出错时返回FALSE。
试试这个,它应该适用于您的情况。我在我的localhost中测试过它。对于您的HTML文件:
<select name="insta_country_code" class="widget-user-country country-selector" data-account-name="<?php echo $account1['Username'] ?>">
<option value="br">Brazil</option>
<option value="cl">Chile</option>
<option value="es" selected>Spain</option>
<option value="mx">Mexico</option>
<option value="gb">United Kingdom</option>
<option value="cr">Costa Rica</option>
</select>
对于你的JS文件:
<script>
$(document).ready(function () {
$('.country-selector').change(function () {
var ig_name = $(this).data('account-name');
var country_code = $(this).val();
$.ajax({
type: 'POST',
dataType: 'json',
url: 'change-country.php',
data: {
insta_user: ig_name,
country_code: country_code,
},
success: function (data) {
if (data['status'] != 1) {
$.alert('server error');
}
},
error: function () {
$.alert('error');
}
});
})
});
</script>
对于.PHP文件:
<?php
$data = $_REQUEST;
$InstagramCountry = $data['country_code'];
$InstagramUsername = $data['insta_user'];
echo "insta_user=".$InstagramUsername . " AND country_code=" . $InstagramCountry; exit;
告诉我是否还需要任何帮助。
嗨,你可以这样做:
你的PHP脚本:
if (isset($_POST["action"])) {
$action = $_POST["action"];
switch ($action) {
case 'SLC':
if (isset($_POST["id"])) {
$id = $_POST["id"];
if (is_int($id)) {
$query = "select * from alumni_users where userId = '$id' ";
$update = mysqli_query($mysqli, $query);
$response = array();
while($row = mysqli_fetch_array($update)){
.......
fill your response here
}
echo json_encode($response);
}
}
break;
}
}
where action是你想要做的命令SLC,UPD,DEL等,id是一个参数
然后在你的ajax:
function getdetails() {
var value = $('#userId').val(); // value can be your array ! note: if you send a object json_encode(json_decode(,MyObj,true))
return $.ajax({
type: "POST",
url: "getInfo.php",
data: {action: "SLC",id: value }
})
}
这样叫:
getdetails().done(function(response){
var data=JSON.parse(response);
if (data != null) {
//do somthing with your Data
}
})
希望能帮助到你