Hi,

I am trying to assign the value of text box to a variable in PHP. But I am consistently getting an error. The error says, "Undefined index: fname" and "Undefined index: age ". Can anyone please help me in this matter? How can I resolve this?

form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> 
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

Any help will be very much appreciated.

Regards,

Bilal A. Khan

Recommended Answers

All 4 Replies

Hi,

If you read the documentation you'll see that:
$_POST is an associative array of variables passed to the current script via the HTTP POST method.

In your example you get the errors because you are not passing this variables, and the last to lines are executed before you click on the submit button (which in your case should send the 2 variables to welcome.php script)

To solve this, leave only the last 2 lines in your welcome.php script
And move the form in another file, a simple welcome.html file should do it.

GL!

<?php 
if(!isset($_POST['fname']) || !isset($_POST['age'])) { 
echo '<form action="welcome.php" method="post">
Name: <input type="text" name="fname"';
if(isset($_POST['fname']) echo " value='$_POST['fname']"; // adds the value of any part-completed form
echo ' /><br />
Age: <input type="text" name="age" ';
if(isset($_POST'age']) echo " value='$_POST['age']";  // adds the value of any part-completed form
echo ' />
<input type="submit" />
</form>';}
else { echo "Welcome $_POST['fname'] !<br />
You are $_POST['age'] years old. ";} ?>

presents the form until all required fields are properly filled, including updating completed values, then presents the welcome screen.
line 2 can be expanded to check the presence of any number of requred fields, separated by || , it is not validating the content, just the existence.

There is no requirement for submission to another file

just assign them to a variable like this

form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> 

$fname = $_POST["fname"];
$age = $_POST["age"];

Welcome <?php echo $fname; ?>!<br />
You are <?php echo $age; ?> years old.

to learn more coding join www.fivestargamerz.com

commented: The code is wrong, just like the OP there is NO variable until the form is submitted, you have tgo validate first -3
form action="welcome.php" method="post">
    Name: <input type="text" name="fname" />
    Age: <input type="text" name="age" />
    <input type="submit" />
    </form>
	<?php 
	// check to see if the submit button has been pressed, if it has display your variables, if not... don't
	if isset($_POST['submit']) {
	?>
		Welcome <?php echo $_POST["fname"]; ?>!<br />
		You are <?php echo $_POST["age"]; ?> years old.
	<?php
	} else {
		echo "Please fill out the form and press submit!";
	}
	?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.