Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2006, 02:38 AM
Registered User
 
Join Date: Jun 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default using forms in php

Hello, hope ur having a nice day

the following code is creating a form using "echo" from php, the page is saved as .php also, the problem is: the last 2 "while" statements aren't printing the $Math or $Student arrays because they cannot see them, the error I get is "Warning: Variable passed to each() is not an array or object in c:\inetpub\wwwroot\php tests\exam.php" and therefore arent processed in the next page "exam2.php" (not included here)

if anybody knows whats wrong with the syntax or sth, please help :) thnks

<body>

<form method="post" ACTION='exam2.php'>
<?php
$student = array("Albert", "Ivan", "Napoleon", "Simon", "Issac");
while(list (,$name) = each($student))
{

    echo "What grade did $name get in Math?";
    echo "<br><br>";
    echo "<select name='Math[]' >
            <option> Grade A </option>
            <option> Grade B </option>
            <option> Grade C </option>
            <option> Grade D </option>
            <option> Grade E </option>
        </select>";
    echo "<br><br>";
    echo "<input type='hidden' name='Student[]' value='$name'>";

}

while(list ($index,$value) = each($Math))
{
    echo "<br>$index - $value";
}

while(list ($index,$value) = each($Student))
{
    echo "<br>$index - $value";
}

echo "<input type='submit'></form>";

?>
</body>

 
Old June 19th, 2006, 11:15 AM
Authorized User
 
Join Date: May 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to guillermo
Default

$Math and $Student Arrays
You're accessing the form results incorrectly. You should be using $_POST['Math'], because it's a form with method="post". Same for $Student.

Looping
I don't know if you're having looping problems also, but are you opposed to using 'foreach' instead?

$student = array("Albert", "Ivan", "Napoleon", "Simon", "Issac");
foreach ($student as $name)
{
    echo "What grade did $name get in Math?";
    echo "<br><br>";
    echo "<select name='Math[]' >
            <option> Grade A </option>
            <option> Grade B </option>
            <option> Grade C </option>
            <option> Grade D </option>
            <option> Grade E </option>
        </select>";
    echo "<br><br>";
    echo "<input type='hidden' name='Student[]' value='$name'>";
}

'foreach' will produce the results you're looking for, and it's less awkward. And with key & value pairs, you'd write it like so:

foreach ($array as $key=>$val)
{
        echo "$key = $value";
}
 
Old June 20th, 2006, 05:02 AM
Registered User
 
Join Date: Jun 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried that guillermo,,ur way is easier to handle, but the problem is still there.. when I try to print an element from the $Math array it doesnt echo anything :( neither on this page, nor on the exam2.php (not included here), though I'm using $_POST['$Math']; as u can see, dont know wuts wrong !!! this is the code:

<body>
<?php
echo "<form method=post ACTION=exam2.php>";
$student = array("Albert", "Ivan", "Napoleon", "Simon", "Issac");

foreach($student as $name)
{
    echo "What grade did $name get in Math?";
    echo "<br><br>";
    echo "<select name=Math[] >
            <option> Grade A </option>
            <option> Grade B </option>
            <option> Grade C </option>
            <option> Grade D </option>
            <option> Grade E </option>
          </select>";
    echo "<br><br>";
    echo "<input type=hidden name=Student[] value=$name>";
}

$formArr[] = $_POST['Math'];

//try printing an element from the Math array
echo "<br> value of 4th element is $formArr[3]";
echo "<br>";

echo "<input type=submit>";
echo "</form>";
 ?>
</body>

 
Old June 20th, 2006, 08:37 AM
Authorized User
 
Join Date: May 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to guillermo
Default

sorry... I overlooked this the first time around:

Since the action in your form is exam2.php, you have to throw all of your form processing code into exam2.php. No $_POST variables exist in exam.php, because the information is not being posted to that file... <form method="post" action="exam2.php" essentially means: post this form's information to exam2.php. So exam.php should just have the form display code in it, and you should access the $_POST array in exam2.php, like so:

exam.php
<?php
echo "<form method=post ACTION=exam2.php>";
$student = array("Albert", "Ivan", "Napoleon", "Simon", "Issac");
foreach($student as $name)
{
    echo "What grade did $name get in Math?";
    echo "<br><br>";
    echo "<select name=Math[] >
            <option> Grade A </option>
            <option> Grade B </option>
            <option> Grade C </option>
            <option> Grade D </option>
            <option> Grade E </option>
          </select>";
    echo "<br><br>";
    echo "<input type=hidden name=Student[] value=$name>";
}
echo "<input type=submit>";
echo "</form>";
?>

exam2.php
<?php
$formArr[] = $_POST['Math'];

//try printing an element from the Math array
echo "<br> value of 4th element is $formArr[3]";
echo "<br>";
?>
 
Old June 20th, 2006, 09:20 AM
Registered User
 
Join Date: Jun 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

still not working guilllermo, exam2.php does NOT see the array $Math..it's only printing "value of the 4th element is", I dont know wuts wrong, I'm trying to check if this current form "exam.php" is storing the selection results in Math[], that's why I'm trying to print any element out, since I cant do this in the current form, I'm doing the code in exam2.php to check, but both are printing blanks..please check it on ur machine and see for ur self..and hey..thnx 4 trying to help!!

 
Old June 20th, 2006, 07:08 PM
Authorized User
 
Join Date: May 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to guillermo
Default

I CAN'T BELIEVE I MISSED THIS!

<?php
$formArr = $_POST['Math'];

//try printing an element from the Math array
echo "<br> value of 4th element is {$formArr[3]}";
echo "<br>";
?>

1st, $formArr[] = $_POST['Math'] is what you had before... what that does is set $formArr[0] = $_POST['Math']...

2nd... when printing an array value in double quotes, you must surround it with brackets... note the way i did it above...

finally got home to test it out and it works with the changes I made... sorry about that... I should've caught those mistakes earlier.

 
Old June 21st, 2006, 03:08 AM
Registered User
 
Join Date: Jun 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well well well...first I added the braces to my code and it still didnt work..then..I copy-pasted ur code and it WORKED !!! though I had the same lines!! magic or wut dont know..but anyway ..made me so happy :-) brilliant guillermo !!! thnx alot 4 ur patience :-):-) problem solved..






Similar Threads
Thread Thread Starter Forum Replies Last Post
manipulating forms with php and javascript Hylsan Beginning PHP 0 April 20th, 2007 09:11 AM
PHP & Forms: stopping spam and multiple posts stephen_c_ Beginning PHP 1 July 28th, 2005 02:36 PM
Displaying Multiple forms on same php page wokoci PHP How-To 0 August 17th, 2004 06:53 AM
PHP problem with forms WIx Beginning PHP 1 August 5th, 2004 08:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.