gpt4 book ai didi

javascript - 如何使用jquery在ajax调用中传递多个参数

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:02 25 4
gpt4 key购买 nike

今日问候!!!

我在使用 ajax URL 传递参数时遇到问题。我正在尝试使用 jquery $.ajax 方法将多个数据发送到我的 php 脚本,但是当我连接多个数据时我只能传递单个数据。当我尝试更新另一个字段但该字段未更新且第一个已更新时。仅第一个字段更新。遗留字段未更新。我在更新其他字段时遇到问题。并且我尝试在 ajax URL 中传递多个参数但出现错误。不更新任何字段。
请检查我的代码并给我解决方案。

希望大家明白。

谢谢!!!

这是我的代码:

<?php
include("connect.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta charset="utf-8">
<title>Editable Tables using jQuery - jQuery AJAX PHP</title>
<link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="assets/css/custom.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container">
<div style="text-align:center;width:100%;font-size:24px;margin-bottom:20px;color: #2875BB;">Click on the underlined words to edit them</div>
<div class="row">
<table class= "table table-striped table-bordered table-hover">
<thead>
<tr>
<th colspan="1" rowspan="1" style="width: 180px;" tabindex="0">FName</th>
<th colspan="1" rowspan="1" style="width: 220px;" tabindex="0">LName</th>
<th colspan="1" rowspan="1" style="width: 288px;" tabindex="0">Email</th>

<th colspan="1" rowspan="1" style="width: 288px;" tabindex="0">Gender</th>
<th colspan="1" rowspan="1" style="width: 288px;" tabindex="0">Address</th>
<th colspan="1" rowspan="1" style="width: 288px;" tabindex="0">City</th>
<th colspan="1" rowspan="1" style="width: 288px;" tabindex="0">Course</th>
<th colspan="1" rowspan="1" style="width: 288px;" tabindex="0">Hobby</th>
</tr>
</thead>

<tbody>
<?php
$query = mysql_query("SELECT * FROM student_data");
$i=0;
while($fetch = mysql_fetch_array($query))
{
if($i%2==0) $class = 'even'; else $class = 'odd';
echo'<tr class="'.$class.'">
<td><span class= "xedit" id="'.$fetch['id'].'">'.$fetch['fname'].'</span></td>
<td><span class= "xedit" id="'.$fetch['id'].'">'.$fetch['lname'].'</span></td>
<td><span class= "xedit" id="'.$fetch['id'].'">'.$fetch['email'].'</span></td>

<td><span class= "xedit" id="'.$fetch['id'].'">'.$fetch['gender'].'</span></td>
<td><span class= "xedit" id="'.$fetch['id'].'">'.$fetch['address'].'</span></td>
<td><span class= "xedit" id="'.$fetch['id'].'">'.$fetch['city'].'</span></td>
<td><span class= "xedit" id="'.$fetch['id'].'">'.$fetch['course'].'</span></td>
<td><span class= "xedit" id="'.$fetch['id'].'">'.$fetch['hobby'].'</span></td>
</tr>';
}
?>
</tbody>
</table>
</div>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/js/bootstrap-editable.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
$.fn.editable.defaults.mode = 'popup';
$('.xedit').editable();
$(document).on('click','.editable-submit',function(){
var x = $(this).parents('td').children('span').attr('id');
var y = $('.input-sm').val();
var z = $(this).parents('td').children('span');
alert(x);
alert(y);
alert(z);
$.ajax({
url:"process.php?id="+x+"&fname="+y,
type: 'GET',
success: function(s){
if(s == 'city'){
$(z).html(y);}
if(s == 'error') {
alert('Error Processing your Request!');}
},
error: function(e){
alert('Error Processing your Request!!');
}
});
});
});
</script>
</div>
</body>
</html>

这是我的另一个文件:

<?php
include("connect.php");
if($_GET['id'])
{
$id = $_GET['id'];
$fname = $_GET['fname'];
$lname=$_GET['lname'];
$email=$_GET['email'];

$gender=$_GET['gender'];
$address=$_GET['address'];
$city=$_GET['city'];
$course=$_GET['course'];
$hobby = explode(',', $_GET['hobby']);
if(mysql_query("UPDATE student_data SET fname='$fname', lname = '$lname', email = '$email', gender='$gender', address='$address', city='$city', course='$course', hobby='$hobby' where id='$id'"));
echo 'success';
}
?>

这里是 Ajax 代码:

<script type="text/javascript">
jQuery(document).ready(function() {
$.fn.editable.defaults.mode = 'popup';
$('.xedit').editable();
$(document).on('click','.editable-submit',function(){
var x = $(this).parents('td').children('span').attr('id');
var y = $('.input-sm').val();
var z = $(this).parents('td').children('span');
alert(x);
alert(y);
alert(z);
$.ajax({
url:"process.php?id="+x+"&fname="+y,
type: 'GET',
success: function(s){
if(s == 'city'){
$(z).html(y);}
if(s == 'error') {
alert('Error Processing your Request!');}
},
error: function(e){
alert('Error Processing your Request!!');
}
});
});
});
</script>

enter image description here

最佳答案

问题在这里:

url:"process.php?id="+x+"&fname="+y,

这里你只发送了 idfname 并且在你试图获取的 php 脚本中:

$id = $_GET['id'];
$fname = $_GET['fname'];
$lname=$_GET['lname'];

ans 这么多参数,这是错误的。

发送多个参数的正确方法是:

data: {
key1: value1,
key2: value2,
key3: value3,
and so on
}

或通过在其中附加所有 key : value 来格式化正确的 url,例如:

key1=value1&key2=value2&key3=value3

关于javascript - 如何使用jquery在ajax调用中传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44149307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com