I am new with php and I want to get form data in php from jQuery serialize array. In php I am not able to get this data in proper format.
Here how I am passing this data from jquery
var form_data = jQuery(this).serializeArray();
form_data = JSON.stringify(form_data);
In php I have implemented json_decode function but I am still getting this array in not useful format.
$data = json_decode(stripslashes($_POST['data']),true);
It is returning form data in this format
Array
(
[0] => Array
(
[name] => fname
[value] => MyFirstName
)
[1] => Array
(
[name] => lname
[value] => MyLastName
)
[2] => Array
(
[name] => phone
[value] => 324242
)
[3] => Array
(
[name] => institution_name[]
[value] => institution
)
[4] => Array
(
[name] => degree_name[]
[value] => Graduated
)
How I can get data properly from this array so that I can use this properly. There are more then 50 input fields in my form and I have few fields like degree_name[] so there can be more then one degree_name[] input fields so how I will be able to fetch data when I will have multiple input fields with the same name. I will appreciate your help.
I think what you're looking for if I'm reading the question right is:
$data = json_decode(stripslashes($_POST['data']),true);
foreach($data as $row) {
echo "{$row['name']}: {$row['value']}";
}
for multiple input fields with the same name
use array name in input field,
for example:
<input name="degree_name[]" type="text">
Related
Trying to get data following a shopping cart purchase on the success page but having issues with $_GET. The URL is:
http://domain.com/success.php?customer[email]=email%40gmail.com&order[button][description]=music+download&order[button][id]=89765464465423184847654556&order[button][name]=music&order[button][repeat]=&order[button][resource_path]=%2Fv2%2Fcheckouts%2F9db9d0ef-9cfd-52b9-b2d7-792683d2431d&order[button][subscription]=false
How can I parse the data from this in PHP?
If you print_r $_GET variable, then it will produce output:
Array
(
[customer] => Array
(
[email] => email#gmail.com
)
[order] => Array
(
[button] => Array
(
[description] => music download
[id] => 89765464465423184847654556
[name] => music
[repeat] =>
[resource_path] => /v2/checkouts/9db9d0ef-9cfd-52b9-b2d7-792683d2431d
[subscription] => false
)
)
)
That means you can access your data via $_GET['customer']['email'] and $_GET['order']['button']['description'] etc..
In your example, you are using what is called a query string. In order to retrieve the information from the query string, the $_GET super global exists and you can use it as follows:
$customer_email = $_GET['customer']['email'];
$order_button_description = $_GET['order']['button']['description'];
$order_button_id = $GET['order']['button']['id'];
// etc.
Let me know if that helps.
I have various forms in a single page.
All the forms get submitted by the below jQuery function.
All is ok, but there is a specific form that has duplicated input names.
In fact in this form the inputs get cloned on request.
So for example, this form represents how many people attend an event.
The user can add as many people as he wants and adding details of each by filling the form.
The inputs get cloned on the fly and inserted inside the form.
The problem is that jQuery is sending them in the wrong way, like shown below, and my php code is not responding well. In fact it just sees one of the posted "people" instead that them all.
Example of jquery post:
protagonist_description zxcvvcbhcvbjfg
protagonist_description jfghjfghjh
protagonist_email
protagonist_email
protagonist_name zxcvzxcv
protagonist_name dfghdfgh
protagonist_phone
protagonist_phone
protagonist_role zxcvxzcv
protagonist_role hgfjgfhjhgh
protagonist_surname zcxvzxcv
protagonist_surname dfghd
protagonist_video
protagonist_video
protagonist_website
protagonist_website
Example of PHP response:
Array
(
[protagonist_name] => dfghdfgh
[protagonist_surname] => dfghd
[protagonist_role] => hgfjgfhjhgh
[protagonist_description] => jfghjfghjh
[protagonist_email] =>
[protagonist_phone] =>
[protagonist_website] =>
[protagonist_video] =>
)
As you see it just gets the last posted.
Here is my jQuery function:
$(".save").click(function() {
$.ajax({
type: 'POST',
data: $('form[name="wizard_form"]').serialize(),
success: function() {
$('#publish').modal('show');
}
});
});
Please note that all the cloned inputs are inside the same form.
I can only use one form. So please do not suggest using more cloned forms. It won't work for my code as it is.
I am looking for a way to correctly merge the inputs with the same name in specific arrays posted to the PHP code.
How to do that?
You first need to change your form inputs to be arrays by adding [] to the name like so:
<input name="protagonist_description[]" />
And then in your PHP it will look like this:
Array
(
[protagonist_name] => Array
(
[0] => zxcvzxcv,
[1] => dfghdfgh,
)
[protagonist_surname] => Array
(
[0] => zcxvzxcv,
[1] => dfghd,
)
[protagonist_role] => Array
(
[0] => zxcvxzcv,
[1] => hgfjgfhjhgh,
)
[protagonist_description] => Array
(
[0] => zxcvvcbhcvbjfg,
[1] => jfghjfghjh,
)
[protagonist_email] => Array
(
[0] => ,
[1] => ,
)
[protagonist_phone] => Array
(
[0] => ,
[1] => ,
)
[protagonist_website] => Array
(
[0] => ,
[1] => ,
)
[protagonist_video] => Array
(
[0] => ,
[1] => ,
)
)
At this point you can then loop through the values like this:
for ($i = 0, $max = count($_POST['protagonist_name']); $i < $max; $i++) {
$first_protagonist = array(
'protagonist_name' => $_POST['protagonist_name'][$i],
'protagonist_surname' => $_POST['protagonist_surname'][$i],
// ...
);
}
Per David's suggestion, here's the code for a foreach if you wish:
foreach ($_POST['protagonist_name'] as $key => $val) {
$first_protagonist = array(
'protagonist_name' => $val,
'protagonist_surname' => $_POST['protagonist_surname'][$key],
'protagonist_description' => $_POST['protagonist_description'][$key],
// ...
);
}
You need to add brackets to your input's name.
Example:
<input name="my_name[]" value="david" />
<input name="my_name[]" value="john" />
<input name="my_name[]" value="francis" />
$_POST['my_name'] will be an array containing all results.
Array
(
[my_name] => Array
(
[0] => david
[1] => john
[2] => francis
)
)
To handle arrays of input elements, in other words defined as having the same element name. If you define the name with two square brackets in the end it will be transformed to an array on the server side for php.
<input type='text' name='elmname[]' .....
<input type='text' name='elmname[]' .....
if you submit the form consisting these elements and then on the php side you will see that $_POST['elmname'] will be an array
I want to use jQuery UI Autocomplet to make a form field autocomplete grabbing the values from a database:
http://jqueryui.com/demos/autocomplete/#remote
I have copied the code accross, but the example (birds) is an associative array.
$items = array(
"Great Bittern"=>"Botaurus stellaris",
"Little Grebe"=>"Tachybaptus ruficollis",
"Black-necked Grebe"=>"Podiceps nigricollis",
"Little Bittern"=>"Ixobrychus minutus");
etc etc
I want to query my database taking two fields, id and author.
However the query returns an multidimensional array, where each returned row from the database is an array.
e.g. ( [0] => Array ( [ID] => 1 [Author] => Higgins ) ) ( [0] => Array ( [ID] => 2 [Author] => Darl) )( [0] => Array ( [ID] => 3 [Author] => Lewis) )
How can I return the query so it is in the format:
"1=>Higgins,
2=>Darl,
3=>etc etc,"
so that i can use the script?
Given that your database array is $dbresult, you can do it like this:
foreach($dbresult as &$arr) {
$completearray[$arr['ID']] = $arr['Author'];
}
var_dump($completearray);
above will output the following array:
1=>Higgins
2=>Darl
3=>Lewis
UPDATE: I've updated the code above so the resulting array is indexed by the ID field.
The scenario is
I would like to insert an record like this:
Firstly, i have an array of what datafield i need to insert
$field = Array ( [0] => Email [1] => Name )
Secondly, I have an array of mail
$mail = Array ( [0] => a#a.com [1] => foodil#g.com )
Lastly, I have an multi dimension array that has Name,
$set = Array ( [1] => Array ( [1] => leo [4] => NULL ) )
however, It can be more than one field,
eg. it can be also have a field of phone (and also address, geneder...whatever) , then it will be:
$field = Array ( [0] => Email [1] => Name [2] => Phone )
$set = Array ( [1] => Array ( [1] => leo [4] => NULL ) [5] => Array ( [1] => 4343343 [4] => 3453343 ))
The problem is , how to insert in the scenario like this? :
The query should look like this
$query="INSERT INTO subscriber (Email,Name,Phone) VALUES ($mail[] , $set[][], $set[][])";
why you not simply insert a single record for each subscriber?, if the fields on the databases are NOT-NULL you can insert default values instead. however, i would use json_encode() instead serialized(), this will maintain the data readble for others languages using a json parser.
You can use serialize() and unserialize (its opposite) to store multidimensional arrays into mysql
i have a form with some checkboxes. if i activate a ceckbox, jquery is sending the data with the .serialize() function to a php file via ajax. The problem is, that jquery send some double parameters. Here is the Query:
area=26-50&area=51-75&area=76-100&area=100&std=1&std=3
How can i create a array like this:
array(
'area' => array(0 => '26-50',1 => '51-75',2 => '76-100'), std => array(0 => 1,1 => 3)
)
PHP overwrites the last variable with a new one...
Thanks for the help!
greetings
[] notation will make it possible to transmit array data in a form.
Name the checkboxes in the form like this:
<input name="area[]" type="checkbox" value="51-75">
this should build an array of all selected check boxes.
PHP can support this if the key name is appended with []:
area[]=26-50&area[]=51-75&area[]=76-100&area[]=100&std[]=1&std[]=3
/*
Array
(
[area] => Array
(
[0] => 26-50
[1] => 51-75
[2] => 76-100
[3] => 100
)
[std] => Array
(
[0] => 1
[1] => 3
)
)
*/