Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: get the value of TEXTBOX then pass it to a VARIABLE

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;
?>
like image 873
Philistyne Brigid Bellisima Avatar asked Jun 09 '13 15:06

Philistyne Brigid Bellisima


People also ask

How can get input field value in php variable?

Use PHP's $_POST or $_GET superglobals to retrieve the value of the input tag via the name of the HTML tag.

How to store textbox value in php?

Here is the code of the form with decode script. $t1v=$_GET['t1v']; $t1v=urldecode($t1v); echo " <form method=post action=pb-t. php> <input type=text name=t1 value='$t1v'> <input type=submit value=Submit> "; ?>

How can we keep textbox value after submit in php?

PHP - Keep The Values in The Form To show the values in the input fields after the user hits the submit button, we add a little PHP script inside the value attribute of the following input fields: name, email, and website. In the comment textarea field, we put the script between the <textarea> and </textarea> tags.

How can I get input field value in php without submit?

php $a = $_GET["housemodel"]; if($a<>'') { if($a == $model1) { echo "<input type=\"text\" name=\"a\" value=\"something model1\">"; } else if($a == $model2) { echo "<input type=\"text\" name=\"b\" value=\"something model2\">"; } else if($a == $model3) { echo "<input type=\"text\" name=\"c\" value=\"something model3\">"; ...


3 Answers

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
}
like image 94
Expedito Avatar answered Sep 30 '22 12:09

Expedito


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;
}
like image 31
Fabio Avatar answered Sep 30 '22 13:09

Fabio


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.

like image 28
Andy G Avatar answered Sep 30 '22 13:09

Andy G