12

Form sending AJAX code:

var str = $("form").serialize();
alert(str);
// var uns=@unserialize(str);
//alert(uns);
$.ajax({
    type: "POST",
    url: "update.php",
    data: "box1="+str,
    success: function(value)
    {
        $("#data").html(value);  
    }

HTML Form:

<form>
  <input type=checkbox name=box[] value='1'/><input type=checkbox name=box[] value='2'/>
</form>  

In my PHP:

$box=$_POST['box1'];    

How can I access each of the box variable values in PHP side?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
MAK
  • 773
  • 4
  • 9
  • 30

8 Answers8

14

Your js should be like this:

var str = $("form").serializeArray();
$.ajax({  
    type: "POST",  
    url: "update.php",  
    data: str,  
    success: function(value) {  
            $("#data").html(value);
    }
});

With php you should loop your result array.

$box = $_POST['box'];
foreach ($box as $x) {
    echo $x;
}

Edit: You have to use serializeArray function in jQuery. Then it will work with this code.

rasmusx
  • 887
  • 8
  • 14
  • i made the changes what you suggest me, but it giving me the following error message: Warning: Invalid argument supplied for foreach() in – MAK Feb 24 '12 at 13:23
9

Provided that your server is receiving a string that looks something like this

    $("form").serialize();
   "param1=someVal&param2=someOtherVal"

...something like this is probably all you need:

    $params = array();
    parse_str($_GET, $params);

$params should then be an array modeled how you would expect. Note this works also with HTML arrays.

See the following for more information: http://www.php.net/manual/en/function.parse-str.php

Hope that's helpful. Good luck!

user3230699
  • 91
  • 1
  • 1
5

your JS should be like this -

var str = $( "form" ).serializeArray();
    var postData = new FormData();
     $.each(str, function(i, val) {
                postData.append(val.name, val.value);
 });
$.ajax({
           type: "POST",
           data: postData,
           url: action,
           cache: false,
           contentType: false,
           processData: false,
           success: function(data){
              alert(data);
          }
    });

Now do this in your php script -

print_r($_POST);

you will get all form data in alert box.

Sujit Verma
  • 172
  • 1
  • 8
3
$data = array();
foreach(explode('&', $_POST[data]) as $value)
{
    $value1 = explode('=', $value);
    $data[$value1[0]] = validateInput($value1[1]);
}

var_dump($data['box']);
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
2

your data in php will contain a string like this

field1=value1&field2=value2&....

so you can get your value1 using $_POST['field1] , value2 with $_POST['field2']

Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
0
values=$("#edituser_form").serialize();//alert(values);
    $.ajax({
        url: 'ajax/ajax_call.php',
        type: 'POST',
        dataType:"json",
        data: values,
        success: function(){
            alert("success");
        },
        error: function(){
            alert("failure");
        }
    });
0

Change

data: "box1="+str,

into

data: str,

serialize() will produce a string like: input1=value1&input2=value2. So in your php you can access each value with, for instance $value1 = $_PHP['input1'];

Alasjo
  • 1,240
  • 8
  • 17
-3

$box=$_POST['box']; and $box is an array.

Lance
  • 30
  • 4
  • $_POST is an array, too, so your answer is redundant. I'm left wondering what you intend to do with `$box`? Are you implying something? – Krista K Jan 18 '13 at 21:18