2

I've got a hidden field on my form and I would like to collate and post dates associated to prices in an array so I can use them on another page. At present, I have the following input field defined below.

<input name="arrayDatesPrices[]" type="hidden" 
value="<?php echo PrintDateTime($recordsCourseWeeks->getField('Start_date'),"d/m/Y"); ?>"> 

and when I do a print_r(arrayDatesPrices) on the following page, it lists all the dates in the array. This works exactly as expected. I have another field called $Price_per_week, and I would like to associate the price to the date. I therefore tried the following.

<input name="arrayDatesPrices[]" type="hidden" 
value="<?php echo array(PrintDateTime    ($recordsCourseWeeks->getField('Start_date'),"d/m/Y"), array( $Price_per_week) ); ?>"> 

When I do a print_r(arrayDatesPrices) on the follwoing pages - I get a blank array. Have I got my formatting incorrect or can you do multi dimension arrays input fields?

2
  • Try transferring these things via session instead? Commented Dec 5, 2012 at 12:13
  • You are doing <?php echo array(); ?> in your code. It leads to converting array to string which leads to value="Array". You can print_r($array, true) if you need whole array inside value attribute. Commented Dec 5, 2012 at 12:28

3 Answers 3

2

You can use json_* or serialize finctions to store structure.
For example:

<input name="arrayDatesPrices[]" type="hidden" 
value="<?php echo json_encode(PrintDateTime($recordsCourseWeeks->getField('Start_date'),"d/m/Y")); ?>"> 

And in php code u must call:

$val = json_decode($_REQUEST['arrayDatesPrices'][$i]);
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe you could just put the two values on the field with a ; separator and explode it with you server code after the submit ?

Or think your fields in an other way like :

<input name="arrayDatesPrices[0][start_date]" type="hidden" value="<?php  echo PrintDateTime($recordsCourseWeeks->getField('Start_date'),"d/m/Y"); ?>" />
<input name="arrayDatesPrices[0][price_per_week]" type="hidden" value="<?php  echo $Price_per_week ?>" />

And incrementing the first parameter for each couple of values

Comments

-1
Try like this..

<input name="arrayDatesPrices[]" type="hidden" 
value="<?php echo PrintDateTime($recordsCourseWeeks->getField('Start_date'),
"d/m/Y").':'.$Price_per_week ; ?>"> 

After submit...
$i = 0;
foreach( $arrayDatesPrices as $arr )
{
 list($stat,$wk) = explode(':',$arr);
 $new_arr[$i]['start_date'] = $stat;
 $new_arr[$i++]['wek'] = $wk;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.