在 JQuery 脚本中传递数组
这两天一直在研究这个愚蠢的问题。如果您能提供帮助,我将不胜感激!
所以我的 html 是这样的:
<a class='selected' option ='2' category='1' price='1750.00'>Round Corners</a>
<a class='selected' option ='3' category='1' price='2200.00'>Chamfer Corners</a>
然后我的脚本是:
$('#save').click(function(){
var passOptions = new Array();
var i=0;
$('.selected').each(function(){
passOptions[i] = $(this).attr('option');
i++;
});
console.log(passOptions);
$.ajax({
type: "POST",
url: "processsaveconfig.php?configid=<? echo $configid; ?>",
data: { passOptionsArray : passOptions },
success: function() {
$('#pricediv').html(data);
}
});
});
我的 php 页面是:
$passopts = $_REQUEST['passOptionsArray'];
mysql_connect($serverpath, $dbusr, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
mysql_query("DELETE FROM se_config_opt_link
WHERE se_config_opt_link.f_config_id = '$configid'");
foreach ($_POST['passOptions'] as $opts){
mysql_query("INSERT INTO se_config_opt_link (f_config_id, f_opt_id)
VALUES ('$configid', '$opts')");
};
在控制台选项卡中的 Firebug 中,我得到: ["1", "4", "7"] 但在“响应”选项卡中显示:
警告:第 17 行的 /home/users/c/companion/public_html/dynamic/builder_app/processsaveconfig.php 中为 foreach() 提供的参数无效b>
我被困住了。如果您能提供帮助,我将非常感激。
Been working on this dumb problem for two days now. If you can help I would sure appreciate it!
So my html goes like this:
<a class='selected' option ='2' category='1' price='1750.00'>Round Corners</a>
<a class='selected' option ='3' category='1' price='2200.00'>Chamfer Corners</a>
And then my script is:
$('#save').click(function(){
var passOptions = new Array();
var i=0;
$('.selected').each(function(){
passOptions[i] = $(this).attr('option');
i++;
});
console.log(passOptions);
$.ajax({
type: "POST",
url: "processsaveconfig.php?configid=<? echo $configid; ?>",
data: { passOptionsArray : passOptions },
success: function() {
$('#pricediv').html(data);
}
});
});
My php page goes:
$passopts = $_REQUEST['passOptionsArray'];
mysql_connect($serverpath, $dbusr, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
mysql_query("DELETE FROM se_config_opt_link
WHERE se_config_opt_link.f_config_id = '$configid'");
foreach ($_POST['passOptions'] as $opts){
mysql_query("INSERT INTO se_config_opt_link (f_config_id, f_opt_id)
VALUES ('$configid', '$opts')");
};
In Firebug in the Console tab I get: ["1", "4", "7"]
But in the Response tab it reads:
Warning: Invalid argument supplied for foreach() in /home/users/c/companion/public_html/dynamic/builder_app/processsaveconfig.php on line 17
I'm stuck. If you can help I would really be grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,你正在寻找这个:
在你的 PHP 中,使用类似这样的东西:
我希望这会有所作为。
It seems to me you're looking for this:
in your PHP, use something like this:
I expect that will make the difference.
foreach ($_POST['passOptions'] as $opts){...
不应该更像foreach ($_POST['passOptionsArray'] as $opts){...< /代码>
Shouldnt the
foreach ($_POST['passOptions'] as $opts){...
be more likeforeach ($_POST['passOptionsArray'] as $opts){...