dsandif Posted April 6, 2009 Share Posted April 6, 2009 Hi All, This is My first time here. I just started learning php about two weeks ago. My hope is as I ask questions and get better at this, that i would and will grow into a contributor and not just a leech. I have what is probably to you a very basic php question\problem. I have created a simple form and I want to generate output from the data\information that is put into the form fields. The problem is, I seem to only get one field to output and not all. I am using the POST method to store all post values in a associative array ($_POST), but unless I'm just not doing my variables correctly, I just can't seem to get the all display I want. Here is the html: <html> <head></head> <body> <form action="quick-form.php" method="post"> First Name: <Input type="text" name="msg" size="30" maxlength="12"><br /> Last Name: <input type="text" name="msg" size="30" maxlength="12"><br /> Address: <input type="text" name="msg" size="30" maxlength="12"><br /> <input type="radio" name="sex" value="male" /> Male <br /> <input type="radio" name="sex" value="female" /> Female<br /> <input type="submit" value="Submit"> </form> </body> </html> And here is the php: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>form22</title> </head> <body> <?php // retrieve form data $input = $_POST['msg']; // use it echo "You said: <i>$input</i>"; ?> </body> </html> Thxs to all. dsandif- Quote Link to comment https://forums.phpfreaks.com/topic/152703-solved-the-n-in-noob/ Share on other sites More sharing options...
SPraus Posted April 6, 2009 Share Posted April 6, 2009 You must put a unique name on the input boxes then read in that name later First Name: <Input type="text" name="msg" size="30" maxlength="12"><br /> Last Name: <input type="text" name="msg" size="30" maxlength="12"><br /> Address: <input type="text" name="msg" size="30" maxlength="12"><br /> <input type="radio" name="sex" value="male" /> Male to First Name: <Input type="text" size="30" maxlength="12" name="first"><br /> Last Name: <input type="text" size="30" maxlength="12" name="last"><><br /> Address: <input type="text" name="address" size="30" maxlength="12"><br /> <input type="radio" name="gender" value="male" /> Male<br> <input type="radio" name="gender" value="female" /> Female<br> PHP: <?php // retrieve form data $first = $_POST['first']; $last = $_POST['last']; $address = $_POST['address']; $gender = $_POST['gender']; // use it echo (" First Name: <strong>$first</strong><br> Last Name: <strong>$last</strong><br> Address: <strong>$address</strong><br> Gender: <strong>$gender</strong><br> "); ?> EDIT capped a couple words i shouldnt have, code ready now. Quote Link to comment https://forums.phpfreaks.com/topic/152703-solved-the-n-in-noob/#findComment-801911 Share on other sites More sharing options...
ToonMariner Posted April 6, 2009 Share Posted April 6, 2009 you need to give each field in your form a separate name.. <form action="quick-form.php" method="post"> First Name: <Input type="text" name="firstname" size="30" maxlength="12"><br /> Last Name: <input type="text" name="lastname" size="30" maxlength="12"><br /> Address: <input type="text" name="address" size="30" maxlength="12"><br /> <input type="radio" name="sex" value="male" /> Male <br /> <input type="radio" name="sex" value="female" /> Female<br /> <input type="submit" value="Submit"> </form> in your code just use print_r($_POST) to see resutls; Quote Link to comment https://forums.phpfreaks.com/topic/152703-solved-the-n-in-noob/#findComment-801913 Share on other sites More sharing options...
dsandif Posted April 6, 2009 Author Share Posted April 6, 2009 Thanks for the quick responses from all. This is what I get back now: Notice: Undefined index: first in C:\wamp\www\572_projects\quick-form.php on line 14 Notice: Undefined index: last in C:\wamp\www\572_projects\quick-form.php on line 15 Notice: Undefined index: address in C:\wamp\www\572_projects\quick-form.php on line 16 Notice: Undefined index: gender in C:\wamp\www\572_projects\quick-form.php on line 17 First Name: Last Name: Address: Gender: Do I need to use double quotes instead of single on the echos? Quote Link to comment https://forums.phpfreaks.com/topic/152703-solved-the-n-in-noob/#findComment-801928 Share on other sites More sharing options...
ToonMariner Posted April 6, 2009 Share Posted April 6, 2009 check your mark up and DOUBLE CHECK you have changed the names of the inputs. instead of echoing them out individual fields just use print_r($_POST); that will show you everything that has been posted and what vale it has been given.. Quote Link to comment https://forums.phpfreaks.com/topic/152703-solved-the-n-in-noob/#findComment-801930 Share on other sites More sharing options...
redarrow Posted April 6, 2009 Share Posted April 6, 2009 one page job tested ((look at the form, ((and say)) why it posting to the same page, that because if you remove the action from the form, it will post to the page it on. <?php if(isset($_POST['submit'])){ // retrieve form data <<< you mean post form data. $first = $_POST['first']; $last = $_POST['last']; $address = $_POST['address']; $gender = $_POST['gender']; //echo form data from posted variables. echo (" First Name: <strong>$first</strong><br> Last Name: <strong>$last</strong><br> Address: <strong>$address</strong><br> Gender: <strong>$gender</strong><br> "); } ?> <form method="POST"> First Name: <Input type="text" size="30" maxlength="12" name="first"><br /> Last Name: <input type="text" size="30" maxlength="12" name="last"><><br /> Address: <input type="text" name="address" size="30" maxlength="12"><br /> <input type="radio" name="gender" value="male" /> Male<br> <input type="radio" name="gender" value="female" /> Female<br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/152703-solved-the-n-in-noob/#findComment-801937 Share on other sites More sharing options...
dsandif Posted April 6, 2009 Author Share Posted April 6, 2009 I removed the line: if(isset($_POST['submit'])) and the brackets "{}" and that did the trick. Thanks to all who replied. dsandif- Quote Link to comment https://forums.phpfreaks.com/topic/152703-solved-the-n-in-noob/#findComment-802040 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.