[英]how to pass multiple parameter in ajax call using jquery
當天的問候!!!
我在用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>
問題在這里:
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
}
或通過在其中附加所有key
: value
來格式化適當的url,例如:
key1=value1&key2=value2&key3=value3
用於發送單參數
data: "id="+id,
用於發送多個參數
data: {
id: id,
fname: fname
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.