简体   繁体   中英

Simple php “Undefined index” error on form submit

I get this error for every $_POST item, except $fname , $mname and $lname . I don't understand why though. I have searched for a solution, and the suggestion of isset is most common, but I have tried it in several different forms and it is still not working for me.

Notice: Undefined index: postcode in C:\\xampp\\htdocs\\php\\signin.php on line 13

Notice: Undefined index: homephone in C:\\xampp\\htdocs\\php\\signin.php on line 15

Notice: Undefined index: mobphone in C:\\xampp\\htdocs\\php\\signin.php on line 16

signin.php

<?php

    if(isset($_POST['submit'])) {

    $fname=$_POST['firstname'];
    $mname=$_POST['middlename']; 
    $lname=$_POST['lastname'];
    $dob=$_POST['dateofbirth'];
    $address=$_POST['address'];
    $city=$_POST['city'];
    $state=$_POST['state'];
    $country=$_POST['country'];
    $postcode=$_POST['postcode'];
    $email=$_POST['email'];
    $homephone=$_POST['homephone'];
    $mobphone=$_POST['mobphone'];
    $preferred=$_POST['preferred'];

    include_once("includes/link.php");

    $sql=("INSERT INTO `member` VALUES ('$fname', '$mname', '$lname', '$dob', '$address', '$city', '$state', '$country', '$postcode', '$email', '$homephone', '$mobphone', 'preferred')");

    mysqli_query($link, $sql);

    mysqli_close($link);
    }
?>

link.php

<?php

    $hostname = "localhost";
    $user = "root";
    $password = "********";
    $link = mysqli_connect($hostname, $user , $password);

    if (!$link){
    echo "server is being lame";
    exit;
    };
    mysqli_select_db($link, "cp2cwonrlvDB3");
    if (!mysqli_select_db($link, "cp2cwonrlvDB3")){
    echo "Could not connect brudda";
    };

?>

Form html:

<html>
<head>
<title>Example form</title>
<style type="text/css">

label
{
width: 100px;
float: left;
text-align: right;
margin-right: 0.5em;
display: block
}

.reset {
margin-left:50px;
}

h3 {
margin-left:15px;
}

</style>
<script>
<?php
    include 'includes/connection.php'
?>
</script>
</head>

<body>

<div class="container">
    <h2>Sign up to CWON</h2>
    <form method="POST" action="signin.php" novalidate>

        <label for="fisrtname">First Name:</label>
        <input type="text" name="firstname" required placeholder="Enter your first name" maxlength="25"><br />

        <label for="middlename">Middle Name:</label>
        <input type="text" name="middlename" placeholder="Enter your middle name"  maxlength="25"><br />

        <label for="lastname">Last Name:</label> 
        <input type="text" name="lastname" required placeholder="Enter your last name"  maxlength="25"><br />

        <label for="dateofbirth">Date of Birth:</label> 
        <input type="date" name="dateofbirth" required maxlength="25"><br />

        <label for="address">Address:</label> 
        <input type="text" name="address" required maxlength="100" placeholder="Enter your home address"><br />

        <label for="city">City:</label> 
        <input type="text" name="city" required maxlength="100" placeholder="Enter your city"><br />

        <label for="state">State:</label> 
        <select name="state">
            <option value="NSW">NSW</option>
            <option value="QLD">QLD</option>
            <option value="VIC">VIC</option>
            <option value="SA">SA</option>
            <option value="WA">WA</option>
            <option value="NT">NT</option>
            <option value="TAS">TAS</option>
        </select> <br />

        <label for="country">Country:</label> 
        <input type="text" name="country" required maxlength="100" placeholder="Enter your country"><br />

        <h3>Preferred contact:</h3>
        <label for="preferred">Home Phone:</label>
        <input type="radio" name="preferred" value="Home Phone"><br /><br />

        <label for="preferred">Mobile Phone:</label>
        <input type="radio" name="preferred" value="Mobile Phone"><br /><br />

        <label for="preferred">Email:</label>
        <input type="radio" name="preferred" value="Email"><br /><br />

        <label for="homenumber">Home Number:</label>
        <input type="text" name="homenumber" placeholder="Enter your home number"><br />

        <label for="mobnumber">Mobile Number:</label>
        <input type="text" name="mobnumber" placeholder="Enter your mobile number"><br />

        <label for="email">Email Address:</label> 
        <input type="email" name="email" required maxlength="100" placeholder="Enter your email address"><br />

        <label for="postcode">Postcode:</label>
        <input type="number" name="postcode" required min="1000" max="9999" placeholder="4-digit"><br />

        <label for="occupation">Occupation:</label> 
        <input type="text" name="occupation" maxlength="50" placeholder="Enter your occupation"><br />

        <label for="hobbies">Hobbies:</label> 
        <input type="text" name="hobbies" maxlength="200" placeholder="separate with commas"><br />

        <label for="interest">Interests:</label> 
        <select name="interest">
            <option value="Fund Raisers">Fund Raisers</option>
            <option value="Domestic Volunteering">Domestic Volunteering</option>
            <option value="Foreign Volunteering">Foreign Volunteering</option>
            <option value="All of the above">All of the above</option>
        </select> <br /><br />

        <label for="terms">Terms and Conditions</label> 
        <input type="checkbox" name="terms"><br /><br /><br />

        <label for="newsletter">Sign up for the CWON newsletter</label> 
        <input type="checkbox" name="newsletter"><br /><br /><br /><br />

        <input type="reset" name="reset" value="Reset" class="reset" onclick="set_focus()" />
        <input type="submit" name="submit" value="Submit" class="submit">

    </form>
</div>

See you have $ inside of the $_POST array. Remove them all.

 $dob=$_POST['$dateofbirth'];
              ^------------------ That (Remove them all)

EDIT :

You need to change the name of your postcode as it reflects the mobphone.

 <label for="postcode">Postcode:</label>
 <input type="number" name="postcode" required min="1000" max="9999" placeholder="4-digit"><br />

Now change them as...

$homephone=$_POST['homenumber']; //<--- Should be homenumber.`
$mobphone =$_POST['mobnumber'];
$postcode =$_POST['postcode'];

You need to check and make sure there are values before you try to access the parameters. That will make sure that they are set before you try and use them. This can be done using the isset() function. Example:

// check values exist before using them
if(isset($_POST['$dateofbirth'])){
    $dob=$_POST['$dateofbirth'];
}

Also, your SQL is vulnerable to injection. You really should sanitize your parameters before you use them in a query. MySQLi Parameterized Queries or PDO Queries would be recommended for this.

Notice how the final error doesn't include the $ , so removing that won't full solve the issue.

Change:

$dob=$_POST['$dateofbirth'];

To:

$dob=$_POST['dateofbirth'];

Same for all.

you don't have inputs with name 'homephone' and 'mobphone', just 'homenumber' and 'mobnumber'

$homephone=$_POST['homenumber'];
$mobphone=$_POST['mobnumber'];

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