8

My Problem is:

I want to get the value of textbox1 then transfer it to another page where the value of textbox1 will be appeared in the textbox2.

Below is my codes for PHP:

<html>
<body>

<form name='form' method='post' action="testing2.php">

Name: <input type="text" name="name" id="name" ><br/>

<input type="submit" name="submit" value="Submit">  

</form>
</body>
</html>

I also add the code below and the error is "Notice: Undefined index: name"

<?php 
$name = $_GET['name'];
echo $name;
?>

or

<?php 
$name = $_POST['name'];
echo $name;
?>
5
  • wrap it around if (isset($_POST['name'])) Commented Jun 9, 2013 at 15:10
  • In your form you've specified the method post, so in your php code you should look for the value in the $_POST array, so $_POST['name'] Commented Jun 9, 2013 at 15:11
  • And don't use isset as Dave Chen recommended, use if( ! empty($_POST['name']) instead. Commented Jun 9, 2013 at 15:13
  • Depends on what he wants. If he wants an empty name that is. Also, empty(0) returns false. Commented Jun 9, 2013 at 15:14
  • @Philistyne Brigid Bellisim - Andrew Gibson has a point about the naming convention. "name" will only cause confusion, and I suggest that when you write a form, it will help to use a naming convention such as first_name and last_name respectively. You could also use a shorter form such as fname or f_name - lname or l_name. It's only good practice ;-) Commented Jun 9, 2013 at 15:27

4 Answers 4

12

In testing2.php use the following code to get the name:

if ( ! empty($_POST['name'])){
    $name = $_POST['name']);
}

When you create the next page, use the value of $name to prefill the form field:

Name: <input type="text" name="name" id="name" value="<?php echo $name; ?>"><br/>

However, before doing that, be sure to use regular expressions to verify that the $name only contains valid characters, such as:

$pattern =  '/^[0-9A-Za-zÁ-Úá-úàÀÜü]+$/';//integers & letters
if (preg_match($pattern, $name) == 1){
    //continue
} else {
    //reload form with error message
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the information for valid characters. I can used it in the future :) and it works too!
This answer is good, it encourages the beginners to make their websites more secured!
4

I think you should need to check for isset and not empty value, like form was submitted without input data so isset will be true This will prevent you to have any error or notice.

if((isset($_POST['name'])) && !empty($_POST['name']))
{
    $name = $_POST['name']; //note i used $_POST since you have a post form **method='post'**
    echo $name;
}

4 Comments

empty() does not generate a warning if the variable does not exist. So you only need just empty or just isset.
@DaveChen That's another issue - The OP should dive into tutorials and learn as he earns. ;-) In this equation, $earns = "Knowledge";
Oh. thank you so much.. It works! I have last question... how can I transfer the variable of $name to a textbox?
@PhilistyneBrigidBellisima Look it up on the Web. There is a plethora of answers out there for you to look for. We don't write code for you, we try to help with issues you may have for some probable and inexplicable reason. Google how to automatically insert text in a form field or something to that affect. Enjoy the adventure, and happy coding! Cheers
1

You are posting the data, so it should be $_POST. But 'name' is not the best name to use.

name = "name"

will only cause confusion IMO.

Comments

0

Inside testing2.php you should print the $_POST array which contains all the data from the post. Also, $_POST['name'] should be available. For more info check $_POST on php.net.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.