Java_NewBie
Java_NewBie

Reputation: 93

php POST method error while submitting

Im trying to submit a form from one php to another. But it gives me error. Please help

<form action="script.php" method="POST">
            <p>
                <label class = "log3"> Username</label>
                <input class = "log" type="text" id="user" name="studentid">
            </p>

            <p>
                <label class = "log4"> Password </label>
                <input class = "log2" type="password" id="pass" name="Pass">
            </p>

            <p>
                <input type="submit" id="studentbtn" value="As Student">
                <input type="submit" id="lecturerbtn" value="As Lecturer">
            </p>
        </form>

Here is the php file which gets the submitted values

$lecturerid = $_POST['user'];
$password = $_POST['pass'];

Error Message :

Notice: Undefined index: user in C:\xampp\htdocs\Coursework\script.php on line 4

Notice: Undefined index: pass in C:\xampp\htdocs\Coursework\script.php on line 5.

Upvotes: 0

Views: 62

Answers (5)

Vihanga Bandara
Vihanga Bandara

Reputation: 383

The mistake you have done is instead of using the name you have used the id which is wrong since if you want to call something using the POST Super variable on another page u need to call with its 'name' that you have given in the previous page.

 <input class = "log" type="text" id="user" name="studentid">

In the above code the name you given is 'studentid' therefore when you are trying to access the data entered in this you need to do as below.

$lecturerid = $_POST['studentid'];

For further reference please check out https://www.w3schools.com/php/php_forms.asp and http://php.net/manual/en/reserved.variables.post.php

Upvotes: 0

Anushil Nandan
Anushil Nandan

Reputation: 294

you need to use name instead of id

The name attribute is used when sending data in a form submission. You may have several radio buttons with different id attributes, but the same name. When submitted, there is just the one value in the response - the radio button you selected.

For your form:

    <form action="script.php" method="POST">
        <p>
            <label class = "log3"> Username</label>
            <input class = "log" type="text" id="user" name="studentid">
        </p>

        <p>
            <label class = "log4"> Password </label>
            <input class = "log2" type="password" id="pass" name="Pass">
        </p>

        <p>
            <input type="submit" id="studentbtn" value="As Student">
            <input type="submit" id="lecturerbtn" value="As Lecturer">
        </p>
    </form>


$lecturerid = $_POST['studentid'];
$password = $_POST['Pass'];

Upvotes: 2

Vimukthi Guruge
Vimukthi Guruge

Reputation: 285

Your form inputs name are studentid and Pass

 <input class = "log" type="text" id="user" name="studentid">
 <input class = "log2" type="password" id="pass" name="Pass">

therefor your $_POST[''] methods are should like bellow

$lecturerid = $_POST['studentid'];
$password = $_POST['Pass'];

Upvotes: 0

JazZ
JazZ

Reputation: 4579

You need to ask for the name attribute of the input in the $_POST variable :

$lecturerid = $_POST['studentid'];
$password = $_POST['Pass'];

Follow this link for the php manual tutorial : http://php.net/manual/en/tutorial.forms.php

Upvotes: 1

HARI
HARI

Reputation: 1

$lecturerid = $_POST['studentid'];
$password = $_POST['Pass'];

Use name attribute instead of id.

Upvotes: 0

Related Questions