JQuery string not parsing correctly in PHP

In a previous post, I asked about how to deserialize a JQuery string, and was told to use parse_str(). However, I didn't post my code until later. So, here is my JQuery string:

age_gender=1&age_gender=2&age_gender=3&age_gender=4&age_gender=5&age_gender=6

And here is my PHP code:

if(isset($_POST['age_gender'])) { 
     $formSerialized = $_POST['age_gender'];
     $formData = array();
     parse_str($formSerialized, $formData);
     addRow($formData, $link); 
}

function addRow($dataArray, $link) {
     $age_group = $dataArray[0];
     $populations = array(intval($dataArray[1]) + intval($dataArray[2]), intval($dataArray[1]), intval($dataArray[2]));
     $percents = array(doubleval($dataArray[3]) + doubleval($dataArray[4]), doubleval($dataArray[3]), doubleval($dataArray[4]));
     $m_per_100_f = doubleval($dataArray[6]);
     $query = "INSERT INTO national_age_gender_demographics (age_group, both_pop, male_pop, female_pop, both_percent, male_percent, female_percent, males_per_100_females)
     VALUES ('$age_group','$populations[0]','$populations[1]','$populations[2]','$percents[0]','$percents[1]','$percents[2]','$m_per_100_f')";
     $result = mysqli_query($link,$query);

     if(!$result)  die( "Query: " . $query . "\nError:" . mysql_error() );
}

For some reason, I am getting a blank string for $age_group, and 0's for all other values. Could anyone help me here?

Answers


Try to pass the data like this instead:

age_gender[]=1&age_gender[]=2&age_gender[]=3&age_gender[]=4&age_gender[]=5&age_gender[]=6

note the '[]' after the name, if they come from html input you would just have to name your html input age_gender[]. Then you function addRow should look like:

    function addRow($dataArray, $link) {
     $dataArray = $dataArray['age_gender'];//Added this line
     $age_group = $dataArray[0];
     $populations = array(intval($dataArray[1]) + intval($dataArray[2]), intval($dataArray[1]), intval($dataArray[2]));
     $percents = array(doubleval($dataArray[3]) + doubleval($dataArray[4]), doubleval($dataArray[3]), doubleval($dataArray[4]));
     $m_per_100_f = doubleval($dataArray[6]);
     $query = "INSERT INTO national_age_gender_demographics (age_group, both_pop, male_pop, female_pop, both_percent, male_percent, female_percent, males_per_100_females)
     VALUES ('$age_group','$populations[0]','$populations[1]','$populations[2]','$percents[0]','$percents[1]','$percents[2]','$m_per_100_f')";
     $result = mysqli_query($link,$query);

     if(!$result)  die( "Query: " . $query . "\nError:" . mysql_error() );
}

Need Your Help

Adding property values to a maven archetype

java xml maven archetypes

Is it possible to add property values to a maven archetype so that these are requested on generation from a repository?

How do I turn the sample code below into accepting multiple inputs?

python web-services flask

I would like to create a web service from a simple invoice app I have wrote. I would like it to return json and hopefully pdf files from apache fop. I do not want a html web page, I will access the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.