Javascript 如何使用jquery在ajax调用中传递多个参数
祝你节日快乐 我在使用ajax URL传递参数时遇到了问题。 我试图使用jquery$.ajax方法将多个数据发送到我的php脚本,但在连接多个数据时,我只能传递单个数据。 当我尝试更新另一个字段但该字段未更新且第一个字段已更新时。只有第一个字段更新。未更新剩余字段。我面临更新其他字段的问题。我还尝试在ajax URL中传递多个参数,但出现了错误。不更新任何字段。Javascript 如何使用jquery在ajax调用中传递多个参数,javascript,php,jquery,css,ajax,Javascript,Php,Jquery,Css,Ajax,祝你节日快乐 我在使用ajax URL传递参数时遇到了问题。 我试图使用jquery$.ajax方法将多个数据发送到我的php脚本,但在连接多个数据时,我只能传递单个数据。 当我尝试更新另一个字段但该字段未更新且第一个字段已更新时。只有第一个字段更新。未更新剩余字段。我面临更新其他字段的问题。我还尝试在ajax URL中传递多个参数,但出现了错误。不更新任何字段。 请检查我的代码并给我解决方案 我希望你们都能理解 谢谢你 这是我的密码: <?php include("connect.php
请检查我的代码并给我解决方案 我希望你们都能理解 谢谢你 这是我的密码:
<?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>
使用jQuery-jqueryajaxphp的可编辑表
单击带下划线的单词进行编辑
FName
名字
电子邮件
性别
地址
城市
课程
爱好
jQuery(文档).ready(函数(){
$.fn.editable.defaults.mode='popup';
$('.xedit').editable();
$(文档)。在('单击','上。可编辑提交',函数(){
var x=$(this).parents('td').children('span').attr('id');
变量y=$('.input sm').val();
var z=$(this.parents('td')。children('span');
警报(x);
警报(y);
警报(z);
$.ajax({
url:“process.php?id=“+x+”&fname=“+y,
键入:“GET”,
成功:功能{
如果(s==‘城市’){
$(z).html(y);}
如果(s=='错误'){
警报('处理请求时出错!');}
},
错误:函数(e){
警报('处理您的请求时出错!!');
}
});
});
});
这是我的另一个文件:
<?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';
}
?>
<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>
问题在于:
url:"process.php?id="+x+"&fname="+y,
在这里,您只发送id
和fname
,并在php脚本中尝试获取:
$id = $_GET['id'];
$fname = $_GET['fname'];
$lname=$_GET['lname'];
有这么多参数,这是错误的
发送多个参数的正确方法是:
data: {
key1: value1,
key2: value2,
key3: value3,
and so on
}
或者通过在其中添加所有键
:值
来设置正确的url格式,如下所示:
key1=value1&key2=value2&key3=value3
问题在于:
url:"process.php?id="+x+"&fname="+y,
在这里,您只发送id
和fname
,并在php脚本中尝试获取:
$id = $_GET['id'];
$fname = $_GET['fname'];
$lname=$_GET['lname'];
有这么多参数,这是错误的
发送多个参数的正确方法是:
data: {
key1: value1,
key2: value2,
key3: value3,
and so on
}
或者通过在其中添加所有键
:值
来设置正确的url格式,如下所示:
key1=value1&key2=value2&key3=value3
用于发送单个参数
数据:“id=“+id,
用于发送多个参数
data: {
id: id,
fname: fname
},
用于发送单个参数
数据:“id=“+id,
用于发送多个参数
data: {
id: id,
fname: fname
},
@Enstage是的,它不是实时的,但这是演示,当我更正此代码时,我会更改。谢谢。请看一下$(“表单”).serialize()代码>方法来自Okay@SrishinKp谢谢@Enstage是的,它不是实时的,但这是演示,当我更正此代码时,我会更改。谢谢。请看一下$(“表单”).serialize()代码>方法来自Okay@SrishinKp谢谢!!还有另一个参数传入数据?我想传入这个URL:URL:“process.php?id=“+x+”&fname=“+y”,所以我需要在URL中更改什么。url方法附加所有数据的可能性,如url:“process.php?id=“+x+”&fname=“+y+”&key1=“+value1+”&key2=“+value2+”&key3=“+value3
等等,让我试试。谢谢大家!@Mayank Pandeyzi如果我的问题是正确的,那么请放弃投票(有用)。还有另一个参数传入数据?我想传入这个URL:URL:“process.php?id=“+x+”&fname=“+y”,所以我需要在URL中更改什么。url方法附加所有数据的可能性,如url:“process.php?id=“+x+”&fname=“+y+”&key1=“+value1+”&key2=“+value2+”&key3=“+value3
等等,让我试试。谢谢大家!@如果我的问题是正确的,那么请放弃投票。