Jump to content

Help with making/reading dynamic _POST names


grimbergen

Recommended Posts

Hello,

 

I'm trying to read dynamic form input fields but the receiving page isn't able to read it, and I can't figure out why.

 

Here's what I'm trying to accomplish...

 

Page 1:  Creates a list of event name from my database.  Next to each event are select menus for the user to select dates.

 

I've tried various ways of constructing the select input field names, but none seem to transfer to the next page.

 

The code looks something like this:

 

//this code is from a while loop through $optionslist

 

print "<input type=checkbox name='$optionslist[name]'>

print $optionslist[name];

 

$optionyear=$optionslist[name]."yyyy";

 

print "

<select name='$optionslist[name]mm'>

<option>MM</option>

$monthselect

<select name='$optionslist[name]dd' >

<option>DD</option>

$dayselect

<select name='$optionyear'>

<option>YYYY</option>

$yearselect    ";

 

($monthselect, $dayselect, $yearselect are pregenerated lists of date elements)

 

 

Page 2: The receiving page is supposed to receive the form post, again it is a while loop through the array of events.  Again I tried various ways of naming the date form post names, but none seem to work.  However the name of the event that is passed thru for the checkbox is captured properly. 

$optionyear=$optionsrow[name]."yyyy";

$monthinput="mm";

$dayinput="dd";

 

$optionname=$optionslist[name];

 

if ($_POST[$optionname])

print "<input type=hidden name='option$counter' value='$optionname | $_POST[$optionname]| $_POST[$optionslist[name].$monthinput]| $_POST[$optionslist[name].$dayinput]| $_POST[$optionyear'>";

 

(I've added in spacing to make it legible for this post)

 

Any ideas on what I'm doing wrong?

thanks!

Link to comment
Share on other sites

the only thing i've noticed from looking here just briefly is you have quite a few quotes missing.

 

ie.

 

$something=$_POST['some'];

 

also i noticed that your printing incorrectly

 

wrong way:
print "<input type=checkbox name='$optionslist[name]'>

output:
<input type=checkbox name='$optionslist[name]'>

 

correct way:
print"<input type='checkbox' name='" . $optionslist[name] . "'>";

output:
<input type='checkbox' name='Optionslistname'>

 

 

Link to comment
Share on other sites

@ag3nt42: Not quite.  The "wrong" method (that he has now) should actually error him out.  I'm interested as to why it isn't.

 

Hello DW, can you let me know which one is the wrong method? I don't receive any errors, the page renders fine, just the post variables are blank.

 

thanks.

Link to comment
Share on other sites

change form part to

<?php
//this code is from a while loop through $optionslist

print "<input type=checkbox name='chk[]' value='$optionslist[name]'>"
;
print $optionslist[name];

//$optionyear=$optionslist[name]."yyyy";

print "
   <select name='mm[$optionslist[name]]'>
   <option>MM</option>
   $monthselect
   <select name='dd[$optionslist[name]]' >
   <option>DD</option>
   $dayselect
   <select name='yy[$optionslist[name]]'>
   <option>YYYY</option>
   $yearselect    ";
?>

and Page 2 to

<?php
if (isset($_POST['chk'])){
foreach ($_POST['chk'] as $name){
	$m = $_POST['mm'][$name];
	$d = $_POST['dd'][$name];
	$y = $_POST['yy'][$name];
	echo 'Name: ', $name,' | MM: ', $m, ' | DD: ', $d, ' | YYYY: ', $y, "<br />\n";
}
}
?>

Link to comment
Share on other sites

Not using standard coding practices will slow down the speed in which we can help you.

 

Though PHP will fall back to a string when the constant isn't found (using a string without quotes), it will generate a 'Notice' error (assuming error reporting levels allow this), and is still technically wrong.

 

Here are the right ways to use associative (named, string-based keys) arrays in your output/declarations

 

<?php

error_reporting( E_ALL );

$arr['val1'] = 'something';
# or
$arr["val2"] = 'something else';
#or also
$text = 'val3';
$arr[$text] = 'something further';

# WRONG
$arr[val4] = 'another';
# As PHP is expecting val4 to be a constant


# Using in returns/output

echo 'This is ' . $arr["val1"] . ' or ' . $arr['val2'] . ' or even ' . $arr[$text] . '<br>';

echo "Using in a double quoted string is {$arr['val1']} different as well. {$arr[$text]} can be used as well.";

?>

 

Returns

 

Notice: Use of undefined constant val4 - assumed 'val4' in C:\wamp\www\test.php on line 13
This is something or something else or even something further
Using in a double quoted string is something different as well. something further can be used as well.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.