Php array with ajax pass/send to other php file and in the other php file use as array with foreach -


have input form <input type="text" name="record_date[] ...

part of ajax sends form other php file

var values = $("form").serialize(); $.ajax({ type: 'post', data: { 'values' : values }, datatype: 'json', 

the other php file receives data

$values = $_post['values']; 

as understand parse_str($_post['values'],$output); creates array ($output array)

but print_r(json_encode($output)); see nothing (expected see array values etc.)

if use echo json_encode($output['record_date']); works , entered values.

trying create array , use array

 foreach ($output $i=>$output_value ) {  echo json_encode($output_value[$i]);  } 

changed echo json_encode($output_value['record_date'][$i]); in both case echo nothing.

as understand main question how "modify/convert" parse_str($_post['values'],$output); php array

$_post['values'] looks this: record_date%5b%5d=02.07.2013&record_date%5b%5d=01.07.2013

possibly instead of parse_str need use else

update

if in ajax use datatype: 'json', , in php

foreach ($output $key => $output_value) { echo json_encode($output_value); } 

then nothing.

if commentdatatype: 'json',, see ["02.07.2013","01.07.2013"].

if instead of echo json_encode($output_value); use echo $output_value; see long list of arrayarrayarrayarrayarray.

if inside foreach use var_dump($output_value); see this

array(2) { [0]=> string(1) "2" [1]=> string(1) "6" }  ............... array(2) { [0]=> string(10) "02.07.2013" [1]=> string(10) "01.07.2013" } 

seems echo $output[$key][0] inside foreach necessary...

finally made conclusion must use such kind of code

foreach ($output[record_date] $key => $output_value) { echo $output_value. ' output value<br>'; echo $output[other_name_from_html_input][$key]. ' output date selector value<br>'; } 

parse_str() need.

parse_str($_post['values'], $output); foreach ($output $key => $output_value) {     echo json_encode($output_value); } 

your problem don't understand how foreach loop works. $output_value value of array element current iteration, don't need use indexes. if want use indexes should use them original array this:

foreach ($output $key => $output_value) {     echo json_encode($output[$key]); } 

read carefully: php: foreach

but there confusing me. why passing serialized form data single value, when can pass post data itself? in case don't need use parse_str() , code goes this:

js:

var values = $("form").serialize(); $.ajax({ type: 'post', data: values, datatype: 'json', 

php:

foreach ($_post $value) {     echo json_encode($value); } 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -