Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

When I push data into my serialized array in jquery it's not being recognized in my php function?

Posted on 2012-08-12
2
Medium Priority
?
1,395 Views
Last Modified: 2012-08-14
Hi, I am trying to push data into a serialized array that I have, and even though it's making its way into my ajax call, it's coming up as undefined in my php.  Mind you, these are values from file input fields (if that makes a difference... its the reason I have to manually push them bc they won't be serialized otherwise).  Anyone know why my php might not like this??

javascript:

var addProduct = function (e) {
    e.preventDefault();
    var data = $('#addProductForm').serializeArray();

    data.push({ addProductForm: true });
    data.push({ primary: $('input[name="primary"]').val().split('\\')[2] });
    data.push({ other1: $('input[name="other1"]').val().split('\\')[2] });
    data.push({ other2: $('input[name="other2"]').val().split('\\')[2] });
    data.push({ other3: $('input[name="other3"]').val().split('\\')[2] });
    data.push({ other4: $('input[name="other4"]').val().split('\\')[2] });
    
    $.ajax({
        type: 'Post',
        url: 'includes/library.php',
        data: data,
        success: function(result) {
            console.log(result);
        },
        error: function(result) {
            console.log(result);
        }
    });  
};

Open in new window

php:

if (isset($_POST['title'])) {
	$output = '';

	foreach ($_POST as $key => $value) {
            $output .= $key . ": " . $value . "<br />";
	}

	echo $output;
}  

Open in new window

0
Comment
Question by:FairyBusiness
2 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 38285937
Read comments in code:
var addProduct = function (e) {
    e.preventDefault();
/*

at http://api.jquery.com/serializeArray/, it states:
...This produces the following data structure...
[
  {
    name: "a",
    value: "1"
  },
  {
    name: "b",
    value: "2"
  },
  {
    name: "c",
    value: "3"
  },
  {
    name: "d",
    value: "4"
  },
  {
    name: "e",
    value: "5"
  }
]
...

So, if you have <input type="text" name="title" value="Test"/>

then serializeArray() should be giving you:
[
	{"name":"title","value":"Test"}
]

so you would NOT be able to retrieve that as $_POST['title'].  Instead you would need something like:
	$_POST[0]['name'] ==>title
	$_POST[0]['value']==>Test


	So, if you are interested in retrieving the data as $_POST['title']
	then instead of:

	    var data = $('#addProductForm').serializeArray();
	
	    data.push({ addProductForm: true });
	    data.push({ primary: $('input[name="primary"]').val().split('\\')[2] });
	    data.push({ other1: $('input[name="other1"]').val().split('\\')[2] });
	    data.push({ other2: $('input[name="other2"]').val().split('\\')[2] });
	    data.push({ other3: $('input[name="other3"]').val().split('\\')[2] });
	    data.push({ other4: $('input[name="other4"]').val().split('\\')[2] });


	try using serialize() instead of serializeArray():

	    var data = $('#addProductForm').serialize();
	
	    data+='&addProductForm=true';
	    data+='&primary='+$('input[name="primary"]').val().split('\\')[2];
	    data+='&other1='+$('input[name="other1"]').val().split('\\')[2];
	    data+='&other2='+$('input[name="other2"]').val().split('\\')[2];
	    data+='&other3='+$('input[name="other3"]').val().split('\\')[2];
	    data+='&other4='+$('input[name="other4"]').val().split('\\')[2];


	but if you really want to use serializeArray(), then create a json object explicitly
	by extracting the keys form data.name - see below.
*/
	var data = $('#addProductForm').serializeArray(),
	var json = {},i;

	for (i in data) {
		json[data[i].name] = data[i].value;
	}

	json.addProductForm=true;
	json.primary=$('input[name="primary"]').val().split('\\')[2];
	json.other1=$('input[name="other1"]').val().split('\\')[2];
	json.other2=$('input[name="other2"]').val().split('\\')[2];
	json.other3=$('input[name="other3"]').val().split('\\')[2];
	json.other4=$('input[name="other4"]').val().split('\\')[2];
       data=json;

    $.ajax({
        type: 'Post',
        url: 'includes/library.php',
        data: data,
        success: function(result) {
            console.log(result);
        },
        error: function(result) {
            console.log(result);
        }
    });  
};

Open in new window


Regards,
Hielo
0
 

Author Closing Comment

by:FairyBusiness
ID: 38294132
thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question