arrays

jomapi

Guppy
heys guys i need your help, ive created a page where a user can input the number of textbox he needs to iputs some fields, here the code:
$ctr=1;
while($ctr <= $poll_specific)
{
echo "<tr><td align='right'>";
echo "<div id ='normal'>" . "Option " . $ctr . ": ". "</div>";
echo "</td><td>";
echo "<input name='opt$ctr' type='text' maxlength='20' size='60'>";
echo "</td></tr>";
$ctr++;
}

...now heres the problem on the next page how can i recover the textboxes? im trying to use the method _GET

$ctr=1;
while($ctr <= $poll_specific) //poll_specific is the # of txtbox
{
$option[$ctr]=$_GET[' '];
$ctr++;
}

WHAT SHOULD I PUT INSIDE THE $_GET[' ']?????
 
Hi Jomapi

I think your immediate problem is that all your text boxes are literally called opt$ctr.

You've sort of got your single and double quotes confused and reversed. PHP can use either to define a string but the difference is that with double quotes, any variable names in the string will be replaced with the variable's value.

At the vary least, you need to change the name attribute of the input tag to use double quotes, ie name="opt$ctr". After the post, $_GET[] should then contain 'opt1', 'opt2' etc so you could use $_GET["opt$ctr"] in your code.

I tend to always use single quotes unless I specifically want the variable expansion so most of your double quotes could be singles. But to be correct HTML, the values of attributes ('right', '20' etc) should use doubles.

You're usually better off using POST (method="POST") rather than GET for a form so then you'd use $_POST on the next page.

Cheers
Ross
 
tetranz said:
I think your immediate problem is that all your text boxes are literally called opt$ctr.
Nope, he's using double quotes on the line in question, so that's not it.
Double quotes should indeed also be used for the HTML though, using "name=\"opt$ctr\"". Most likely the browser overlooked that problem though.

_GET["opt$ctr"] should indeed work, though personally I tend to use the use the "opt{$ctr}" syntax when there's non-whitespace at either side of the variable name, to make it more readable.
 
Oops, you're quite right. I was confused myself :) by the double quotes around the echo which is all that matters to PHP.
 
hey mako,

ive already tried using _GET["opt$ctr"] (as well as in _POST), but it stil does not work,..your suggestion on "opt{$ctr}",..how will i use that?.,,like this: _POST["opt{$ctr}"]
thanks
 
You really need to do some simple debugging. When something just doesn't work I like to look at it step by step. Problems and errors happen in the most unexpected places. The echo statement is a useful debugging tool :) First look at the resulting html output on your form page to confirm that you really are generating input tags like:

<input name="opt1" etc ...>
<input name="opt2" etc ...>

Then, assuming your form has action="POST", directly check that these exist on the page you're posting to. I tend to do something like this:

echo 'X = ' . $_POST['opt1'];
exit;

I put the 'X' stuff in there in case its blank you'll know exactly where it should appear. If that works then try your code with some debugging stuff ...

PHP:
$ctr=1;
while($ctr <= $poll_specific) //poll_specific is the # of txtbox
{
echo 'A = ' . $ctr . '<br />';
echo 'B = ' . $_POST["opt{$ctr}"] . '<br />';
$option[$ctr]=$_POST["opt{$ctr}"] . '<br />';
echo 'C = ' . $option[$ctr] . '<br />';
$ctr++;
}
The screen will be a mess but somewhere you'll have an 'a ha!' moment and see what's not working.

Ross
 
thanks tetranz, i realized the mistake was within the $_POST[], i didn't know $_POST["opt{$ctr}"] was possible, (embedding the curly braces w/in the _POST)
thanks for all the help guys appreciated it.
 
Back
Top