PHP: get the TEXTBOX value, then pass it to VARIABLE

My problem:

I want to get the value of textbox1 and transfer it to another page where the value of textbox1 will appear in textbox2.

Below are 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 "Note: Undefined index: name"

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

or

 <?php $name = $_POST['name']; echo $name; ?> 
+6
source share
4 answers

In test2.php, use the following code to get the name:

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

When you create the following page, use the value of $name to populate the form field:

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

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

 $pattern = '/^[0-9A-Za-zÁ-Úá-úàÀÜü]+$/';//integers & letters if (preg_match($pattern, $name) == 1){ //continue } else { //reload form with error message } 
+10
source

I think you will need to check the isset value, not the empty value, for example, the form was submitted without input, so isset will be true. This will prevent any error or notification.

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

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

 name = "name" 

will cause only confusion IMO.

+1
source

Inside testing2.php you must print the $_POST array, which contains all the data from the message. In addition, $_POST['name'] must be available. For more information check $ _ POST at php.net .

0
source

Source: https://habr.com/ru/post/946896/


All Articles