0

Trying to send an array from:

<select name='galaddvenn[]'  class='sel_add vl hidden' multiple='multiple'>
<option value='53'>name1</option>
<option value='352'>name2</option>
<option value='632'>name3</option>
<option value='543'>name4</option>..etc
</select>

...to/from Jquery with:

var ar = $("select#galaddvenn").serialize();

        j.ajax({
        data: ({ar : ar}), //tried with 'ar':ar
        dataType: "html",
        type:"post",
        url: "/ajax_gal_addvenn.php",
        cache: false,

....etc to PHP:

if(isset($_POST['ar'])){
$ar = mysql_real_escape_string($_POST['ar']);
var_dump($ar);

Gives me: bool(false) :(

What am i doing wrong here?

2 Answers 2

3

.serialize() gets a complete POST string by itself, so it'll be formatted like this:

galaddvenn=53&galaddvenn=352&galaddvenn=632

So your call should look like this:

j.ajax({
    data: ar,
    dataType: "html",
    type:"post",
    url: "/ajax_gal_addvenn.php",
    cache: false
});

Then on the PHP side, you're looking for $_POST['galaddvenn'] instead.

You can test the output here.

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

Comments

0

mysql_real_escape_string() expects a string. You cannot use it on your array. Check the docs for further information. You should use a loop to do this:

foreach ($_POST['ar'] as $key => $element) {
    $_POST['ar'][$key] = mysql_real_escape_string($element);
}

If you get errors like this, you should test your incoming data before manipulating them (e.g. use var_dump() before applying mysql_real_escape_string()).

2 Comments

@Thundercat - It is essentially empty, or rather a useless string, see my answer :)
@Thundercat: The reason is that you should look for $_POST['galaddvenn'] like suggested by Nick Craver. Use var_dump() on $_POST to see why ;)

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.