Maryam Butt
Maryam Butt

Reputation: 63

How to get the value of a textbox on the same page in PHP

Here is my code:

<form action="" method="get" >
    <input type="text" name="un"> 
    <input type="password" name="un2" />
    <input type="submit" value="submit" name="submit" />
</form>

<?php 
    $users1 = $_GET["un"];
    $id     = $_GET["un2"];
    echo $users1;
?>

I am unable to display it through this way

error:

Notice: Undefined index: un in C:\wamp\www\vas1\register1.php on line 31

line 31:

$users1 = $_GET["un"];

Upvotes: 2

Views: 39494

Answers (3)

chrislondon
chrislondon

Reputation: 12031

You have a couple issues in your code.

Firstly, on the first page load the textarea hasn't been submitted so the request data will be blank. You'll need a isset() to test for it.

Secondly, your PHP is using $_GET when your form is using POST so you need to change those.

Putting it all together:

<form action="" method="post" >
    <input type="text" name="un"> 
    <input type="password" name="un2" />
    <input type="submit" value="submit" name="submit" />
</form>

<?php 
    if (isset($_POST['un'])) {
        $users1 = $_POST["un"];
        $id = $_POST["un2"];

        echo $users1;
    }
?>

Upvotes: 3

hellosheikh
hellosheikh

Reputation: 3015

you are sending the post request

   <form action="" method="post" >

and then you are getting the parameter in get request

$users1 = $_GET["un"];

you are doing wrong ..do this

   $users = $_POST["un"];

Upvotes: 2

SeanWM
SeanWM

Reputation: 16989

It's just a notice. You need to check if the form is being submitted:

if(!empty($_POST)) {
    $users1 = $_POST['un'];
    echo $users1;
}

You can't be using using get because your form is using post:

<form action="" method="post">

Upvotes: 3

Related Questions