Jump to content

[SOLVED] The "N" in noob...


dsandif

Recommended Posts

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-

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.