Getting serialization of data in php file using ajax

Ajax code submission form:

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 to access the each of the box variable values in php side.
+5
source share
8 answers

Your js should look like this:

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

With php you have to encode an array of results.

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

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

+6
source

your data in php will contain a line like this

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

so that you can get the value value1 with $_POST['field1], value2 with$_POST['field2']

+1
source

, , :

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

... - , , , :

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

$params , , . , HTML.

. : http://www.php.net/manual/en/function.parse-str.php

, . !

+1

data: "box1="+str,

data: str,

serialize() : input1=value1&input2=value2. , php , , $value1 = $_PHP['input1'];

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

JS :

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);
          }
    });

php script -

print_r($_POST);

.

0
$data = array();
foreach(explode('&', $_POST[data]) as $value)
{
    $value1 = explode('=', $value);
    $data[$value1[0]] = validateInput($value1[1]);
}

var_dump ($ [ '']);

0

$ box = $ _ POST ['window']; and $ box is an array.

-1
source

All Articles