PHP Form Help With Varibale Vlaue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ajentp
Forum Newbie
Posts: 6
Joined: Thu Sep 10, 2009 10:46 pm

PHP Form Help With Varibale Vlaue

Post 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,
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP Form Help With Varibale Vlaue

Post 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.
ajentp
Forum Newbie
Posts: 6
Joined: Thu Sep 10, 2009 10:46 pm

Re: PHP Form Help With Varibale Vlaue

Post 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>";
}
 
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: PHP Form Help With Varibale Vlaue

Post 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;
 
Post Reply