[英]AJAX call not sending data through
I am created a form that has a SELECT
drop-down that I can SELECT
a user and then their current division I have in my database outputs. 我创建了一个具有
SELECT
下拉列表的表单,可以SELECT
一个用户,然后SELECT
我在数据库输出中具有的当前分区。 This works great. 这很好。 However, I am trying to add/create a new script that I can
UPDATE
the current division that user is in, but I am not getting it to work correctly. 但是,我试图添加/创建一个新脚本,以便可以
UPDATE
用户所在的当前部门,但是我无法使其正常工作。
<label>Current Division
<input type="text" id="current_division">
</label>
<form name="update_group_form" action="" type="POST">
<select name="division_name">
<option value="1">East</option>
<option value="2">West</option>
</select>
<input type="submit" value="submit" name="division_update_button">
</form>
My AJAX call #1 我的AJAX呼叫#1
I am getting an error for the + next to user in the data. 我在数据中用户旁边的+号出现错误。
$(document).ready(function(){
$("#update_group_form").on("change", function(){
$user = this.value;
$.ajax({
url: "update_division.php",
type: "POST",
data: {
"username="+$user,
//console.log($user);
division_name: $(this).find('select[name="group_id"]').val()
},
success: function(text){
alert(data);
},
error: function(jqXHR, textStatus,errorThrown )
{
// alert on an http error
alert( textStatus + errorThrown );
}
});
return false;
});
});
This is the AJAX call #2 of how I get the current division of the user I select. 这是我如何获得所选择用户的当前部门的AJAX调用#2。 It works perfectly and I do not get an error for the + next to user...
它工作正常,并且用户旁边的+不会出现错误...
$(document).ready(function(){
$("#member_division").on("change", function(){
$user = this.value;
$.ajax({
url: "show_division.php",
type: "POST",
data: "username="+$user,
success: function(text){
if(text == "Error!"){
alert("Unable to get user info!");
} else {
var txtArr = text.split('|');
//0: Contains current division
//1: Contains losses
$("#current_division").val(txtArr[0]);
//$("#losses").val(txtArr[1]);
}
},
error: function(xhr, textStatus, errorThrown){
alert(textStatus + "|" + errorThrown);
}
});
});
});
Posting my PHP file in case this will help anyone.. 发布我的PHP文件以防万一。
$update_division = $_POST['division_name'];
$con = mysqli_connect("localhost","","","");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $con->prepare("UPDATE division FROM team_rankings WHERE username = :user");
if ( !$stmt || $con->error ) {
// Check Errors for prepare
die('User Group update prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->bind_param('i', $update_division)) {
// Check errors for binding parameters
die('User Group update bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$stmt->execute()) {
die('User Group update execute() failed: ' . htmlspecialchars($stmt->error));
}
?>
What is wrong with my AJAX call #1 我的AJAX呼叫#1有什么问题
because in your first ajax you are trying to alert data but you are passing text 因为在您的第一个ajax中,您试图警告数据,但是您正在传递文本
if you endpoint is correct you should have: 如果端点正确,则应具有:
$(document).ready(function(){
$("#update_group_form").on("change", function(){
$user = this.value;
$.ajax({
url: "update_division.php",
type: "POST",
data: {
"username="+$user,
//console.log($user);
division_name: $(this).find('select[name="group_id"]').val()
},
success: function(data){
alert(data);
},
error: function(jqXHR, textStatus,errorThrown )
{
// alert on an http error
alert( textStatus + errorThrown );
}
});
return false;
});
});
Data is an object here, so to add username you should make the data to look like 数据是这里的对象,因此要添加用户名,您应该使数据看起来像
data: { username: $user, division_name: $(this).find('select[name="group_id"]').val() }
Also, probably you need to add id to the form, because for now you don't have id #update_group_form only name. 另外,可能您需要在表单中添加ID,因为目前您没有ID #update_group_form仅名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.