Jump to content

Recommended Posts

Without going into the complete coding, I have a page that calls up data from a form using the $_POST command.  This works just as it should, but I am trying to add a twist to it but it is not working. The page it loads from has a feature that automaticly generates a set number of fields to be entered based on the previous page like this:

 

Page 1: Select number of fields

Page 2: Display the number of fields based on page one.

 

What I am working on is page 3, which is to get the $_POST of each field.  I have each field set so it auto generated a number with each field like this:

 

<input type="text" name="question<?PHP echo $num_q; ?>">

 

$num_q is set so that it is always one higher than the previous field, thus there will never be fields with the same name.  Now when trying to call up the data, I tried this but if failed:

 

$num_ans = $_POST['question'.num_q];

 

$num_q was previously defined and set so it would go up by one every time also.  How would I get this to show up properly?  Have I made any sense with this?

Link to comment
https://forums.phpfreaks.com/topic/204119-post-and-variables/
Share on other sites

Without going into the complete coding, I have a page that calls up data from a form using the $_POST command.  This works just as it should, but I am trying to add a twist to it but it is not working. The page it loads from has a feature that automaticly generates a set number of fields to be entered based on the previous page like this:

 

Page 1: Select number of fields

Page 2: Display the number of fields based on page one.

 

What I am working on is page 3, which is to get the $_POST of each field.  I have each field set so it auto generated a number with each field like this:

 

<input type="text" name="question<?PHP echo $num_q; ?>">

 

$num_q is set so that it is always one higher than the previous field, thus there will never be fields with the same name.  Now when trying to call up the data, I tried this but if failed:

 

$num_ans = $_POST['question'.num_q];

 

$num_q was previously defined and set so it would go up by one every time also.  How would I get this to show up properly?  Have I made any sense with this?

 

Well obviously the num_q doesn't pass to the next page. The best thing would be to have a hidden field posted with the 2nd form that says the maximum num_q value. Then you can do a for loop on page 3 that will concat the enumerated value in your for loop to your POST variable something like this

 

for($i = 0;$i<$_POST['num_q_max'];$i++) {
   echo $_POST['question' . $i];
}

Link to comment
https://forums.phpfreaks.com/topic/204119-post-and-variables/#findComment-1069135
Share on other sites

That is actually exactly what I have done.  The problem lies with the code here not working:

 

$num_ans = $_POST['question'.num_q];

 

All that does is show me the variable num_q.

 

If your code looks exactly the way you have posted it here then that's not what you are doing. You need to make a new enumerated variable. Unless num_q is a variable included in the action file then you are not doing this correctly. POST does not assume that you are trying to increment the variables like you did in your form. Without seeing your entire code I can do nothing but assume that you are doing what I have posted here and can not further help you. I don't believe anyone else can either. So till ya post your code... Good luck  ;D

Link to comment
https://forums.phpfreaks.com/topic/204119-post-and-variables/#findComment-1069403
Share on other sites

Bored this Am, so I figured I'd give this a shot (not sure if is exactly what you are looking for).

 

Form Page (example)

<?PHP
/* set the number of fields from whereever /*

$num_fields_one = 7;

/* set your incrementing variable */
$i = 0;

?>
<form action="dynamic_fields_2.php" method="post">
<?PHP
/* loop thru naming and displaying your input fields */

while($i<$num_fields_one) {
$field_name = "my_field". $i;
?>
<?PHP echo "this is field named " . $field_name . " "; ?><input type="text" name="<?PHP echo $field_name; ?>" size="10" maxlength="10" value=""><br>
  <?PHP
$i ++;
}
?>
<input type="submit" value="submit">
</form>
<?PHP
?>

 

Processing Page

 

<?PHP
/* set your incrementing variable */
$i=0;

/* loop thru the post variables puting the values into an array */
foreach($_POST as $key => $value){
$field[$i] = $value;
$field_name[$i] = $key;
$i++;
}

/* count the elements in the array (reduce by 1 as the last element contains the number of elements) */
$count = count($field) - 1;

/* use the values as you choose - here we simply display them */
$i=0;
while($i<$count) {
echo "the field name is " . $field_name[$i] ." and its value is " . $field[$i] . "<br>";
$i++;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/204119-post-and-variables/#findComment-1069446
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.