0

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.

3
  • First things first, var_dump() it to see actually what kind of data you have Commented Sep 22, 2011 at 15:55
  • I'm not a PHP expert but shouldn't it be $passopts['passOptions'] instead of $_POST['passOptions'] in your foreach. Commented Sep 22, 2011 at 15:57
  • I'm using Jeremy's code but var_dump still outputs "NULL"... Commented Sep 22, 2011 at 16:26

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OK, using this code, here is the Firebug "Response" now: Warning: json_decode() expects parameter 1 to be string, array given in ...
0

Shouldnt the foreach ($_POST['passOptions'] as $opts){... be more like foreach ($_POST['passOptionsArray'] as $opts){...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.