Jump to content

Probably a smiple syntax questions on $_POST array


rdrews

Recommended Posts

I have a list of variable that are "C" plus a number that are sent by a form using the POST method.  So, C0, C1, C2, C3, C4, .... It is NOT an array and the number of them created is dynamic so, obviously, I don't know how many there will be. 

 

My problem is this...I want to cycle through them using a while loop but I don't know what the syntax would be.

 

I have:

 

$i = 0

while(isset($_POST['$C$i'])){

...do whatever...

}

 

This is not working.  I have also tried while(isset($_POST["C"$i])) and probably a few other things.  I just can't figure out what to put in the POST brackets.  I am fairly new to PHP and like I said, I'm sure this is just something I am overlooking or there is probably a simple solution.  Any help would be appreciated. 

 

Thanks,

Ryan

Link to comment
Share on other sites

You'll need to remove the single quotes around the $i variable

 

while(isset($_POST['C' .$i])){

 

or put the whole thing in double quotes -

 

while(isset($_POST["C$i"])){

 

Evaluating an isset() in a while() statement will be exceedingly slow. Using the suggested array method will solve two problems, you can use a simple foreach() loop and if these are something like a checkbox/radio-button, where if one is not checked and won't be set, your loop won't stop at the first unchecked box/radio-button.

Link to comment
Share on other sites

Thanks to both of you for the quick responses.  Your help is appreciated but something is still just not working.  Here are the two files I am working with to give a clearer explanation. 

H

ere is my file1:

 

<?php

$gameDate = $_POST['gameDate'];

?>

<form name="form" method="post" action="file2.php">

    <?php

$i = 0;

while(isset($gameDate[$i])){

?>

    <p>Enter comments for the <?php echo $gameDate[$i]; ?> game:<br />

    <textarea name="C<?php echo $i; ?>" COLS=40 ROWS=6></textarea></p>

<?php

$i = $i + 1;

}

?>

    <p><input type="submit" name="Submit" value="Submit" /></p>

</form>

 

...all that is between the html and body tags. 

 

file2.php:

 

<?php

$i=0;

while(isset($_POST['C' .$i])){

$test[$i] = $_POST['C' .$i];

$i++;

}

 

echo $test[0];

echo $test[1];

echo $test[2];

 

?>

 

 

 

In file1, the "gameDate" is data sent over from another form.  For simplistic sake let's just say it just contains a number starting with 0 and ending wherever the user that submits the form sets.  So...that textarea will be generated however many times the user has set in the previous form with the "gameDate" variable. I need each textarea name to be unique (or any array) so that I can access them on file2.php.  I tried changing the name attribute for the textarea to "C[]" as suggested but could not get that to work either.  I also tried the other methods mentioned to run through the C0, C1, C2, ... and couldn't get anything to work.  Am I missing something here?  I would like the textarea data to be saved to an array but I don't know how to do that within the form.  Again, thanks for the quick responses.

Link to comment
Share on other sites

The posted code works for me. When I set $_POST['gameDate'] to be an array with three values, it makes a form with three textareas and submits it and the echo $test[0]... code echos what I enter in the textareas.

 

File1 assumes that $_POST['gameDate']; is an array. Is that so? Have you looked at the "view source" of the file1 form to make sure it is correct?

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.