[英]How to send multiple data fields via Ajax to php page?
i have a problem, I want to send 2 posts to my author php page by using ajax 我有一个问题,我想使用ajax将2个帖子发送到我的作者php页面
thats my code : 多数民众赞成在我的代码:
$.ajax({
type: "POST",
url: "includes/get_competitions.php",
data:'sport=<?php echo $_GET['sports']; ?>',
success: function(data){
$("#competitions-list").html(data);
}
and this what i want to do : 这就是我想做的:
$.ajax({
type: "POST",
url: "includes/get_competitions.php",
data:'sport1=<?php echo $_GET['sports1']; ?>, sport2=<?php echo $_GET['sports2']; ?>',
success: function(data){
$("#competitions-list").html(data);
}
but it didn't work 但这没用
You can try this 你可以试试这个
data: $('#myForm').serialize() <---- your form name
This will send all the form data 这将发送所有表格数据
Multiple parameters in a data string are separated by &
, not ,
(it's just like the syntax of parameters in a URL). 在数据串的多个参数之间用&
,不,
(它就像参数的URL语法)。
However, if you're using jQuery it's best to use an object. 但是,如果您使用的是jQuery,则最好使用一个对象。 $.ajax
will automatically convert it to the properly encoded query string. $.ajax
会自动将其转换为正确编码的查询字符串。 And you can use json_encode()
to convert a PHP value to the corresponding JavaScript literal. 您可以使用json_encode()
将PHP值转换为相应的JavaScript文字。
$.ajax({
type: "POST",
url: "includes/get_competitions.php",
data: {
sport1: <?php echo json_encode($_GET['sports1']); ?>,
sport2: <?php echo json_encode($_GET['sports2']); ?>
},
success: function(data){
$("#competitions-list").html(data);
}
You could also make a single sports
array, rather than numbering the parameters. 您也可以制作一个sports
数组,而不是对参数进行编号。
$.ajax({
type: "POST",
url: "includes/get_competitions.php",
data: {
sports: [<?php echo json_encode($_GET['sports1']); ?>,
<?php echo json_encode($_GET['sports2']); ?>]
},
success: function(data){
$("#competitions-list").html(data);
}
Then in get_competitions.php
, $_POST['sports']
will be an array. 然后在get_competitions.php
, $_POST['sports']
将是一个数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.