简体   繁体   中英

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.

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'];

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

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

Use name attribute instead of id.

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'];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM