Page 1 of 1

PHP Form Help With Varibale Vlaue

Posted: Sun Sep 13, 2009 12:41 pm
by ajentp
Hi

I am trying to make a Form which can pass multiple inputs using varibale DisEarn[$x] to add2.php file where that input can be processed. However I am not receiving the value.

Here is the lines of code for the Form

Code: Select all

 
for($x=0; $x<2; $x++)
        {
            echo "<td align='center' bgcolor='#009933'><font face='Tahoma' size='2' color='#FFFFFF'><b>";
        echo "<input type = 'text' name='DisEarn[$x]'  Size='10' />";
        echo"</font></b></td>";
        
            }
            echo "</tr><tr>";
 
            echo"<input type='submit' value='Submit' name='B1'></form>";
 

And here is the line of code for add2.php

Code: Select all

 
for($x=0; $x<2; $x++)
        {
 
$DisEarn1 = $_GET['DisEarn[$x]'];
echo "Here".$DisEarn1."<br>";
}
 
I tried $_POST, $_GET, $_REQUEST, nothing work. Any help please?

Thanks & Regards,

Re: PHP Form Help With Varibale Vlaue

Posted: Sun Sep 13, 2009 1:55 pm
by califdon
For starters, since you haven't shown the <form...> tag, it's not clear whether your action= parameter is POST or GET. Big difference!

But I think the issue is with the way you are naming your form elements. Using square brackets [ ] in a name probably won't work. That makes it look like an array. Try removing the brackets, both in the form and in your script. Just let it be DisEarn1, DisEarn2.

Re: PHP Form Help With Varibale Vlaue

Posted: Sun Sep 13, 2009 3:21 pm
by ajentp
Never mind I found out:

First I changed the method to POST in Form file as well as add2.php.

Then add " " instead of ' ' for $_POST command in add2.php. It works now

Code: Select all

 
for($x=0; $x<2; $x++)
        {
 
$DisEarn[] = $_POST["DisEarn[$x]"];
echo "First-".$DisEarn[$x]."<br>";
}
 

Re: PHP Form Help With Varibale Vlaue

Posted: Sun Sep 13, 2009 3:37 pm
by Mirge
ajentp wrote:Hi

I am trying to make a Form which can pass multiple inputs using varibale DisEarn[$x] to add2.php file where that input can be processed. However I am not receiving the value.

Here is the lines of code for the Form

Code: Select all

 
for($x=0; $x<2; $x++)
        {
            echo "<td align='center' bgcolor='#009933'><font face='Tahoma' size='2' color='#FFFFFF'><b>";
        echo "<input type = 'text' name='DisEarn[$x]'  Size='10' />";
        echo"</font></b></td>";
        
            }
            echo "</tr><tr>";
 
            echo"<input type='submit' value='Submit' name='B1'></form>";
 

And here is the line of code for add2.php

Code: Select all

 
for($x=0; $x<2; $x++)
        {
 
$DisEarn1 = $_GET['DisEarn[$x]'];
echo "Here".$DisEarn1."<br>";
}
 
I tried $_POST, $_GET, $_REQUEST, nothing work. Any help please?

Thanks & Regards,
Glad you got your problem resolved. Just a quick tip though... instead of echo'ing several lines, it's much easier (and more readable) to use a here document (heredoc for short). For example:

Code: Select all

 
        echo "<td align='center' bgcolor='#009933'><font face='Tahoma' size='2' color='#FFFFFF'><b>";
        echo "<input type = 'text' name='DisEarn[$x]'  Size='10' />";
        echo"</font></b></td>";
 
Would be rewritten as:
 
print <<<EOT
<td align='center' bgcolor='#009933'><font face='Tahoma' size='2' color='#FFFFFF'><b>
<input type = 'text' name='DisEarn[$x]'  Size='10' />
</font></b></td>
EOT;