简体   繁体   中英

Passing array in JQuery Script

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:


: Invalid argument supplied for foreach() in on line :第行的为foreach()提供的参数无效

I'm stuck. If you can help I would really be grateful.

It seems to me you're looking for this:

$('#save').click(function(){
    var passOptions = [];
    $('.selected').each(function(){
        passOptions.push($(this).attr('option'));
     });
console.log(passOptions);

in your PHP, use something like this:

$myArray = $_POST['passOptionsArray'];
if (is_array($myArray)({

    ...

}

I expect that will make the difference.

不应该foreach ($_POST['passOptions'] as $opts){...更像foreach ($_POST['passOptionsArray'] as $opts){...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM