简体   繁体   中英

html form does not link to php file

I have created a html form and want to connect it to a database on xampp. I have created the database and tables but the when I click submit, the php file does not work. I am new to web development. If I just run http://localhost/connect.php then it works but when I click submit on the form, it redirects to file:///F:/Xampp/htdocs/connect.php opens and shows the error. This is the html code for my form.

<form method="post" onSubmit="return validateForm();" action="connect.php">

    <input type="text" id="fname" name="fname" class="input-box" maxlength="20" pattern="[A-Z]{1}[a-z]{2,}"placeholder="First Name" required autofocus>
    <input type="text" id="lname" name="lname" class="input-box" maxlength="20" pattern="[A-Z]{1}[a-z]{2,}"  placeholder="Last Name"  required>
    <br>
    <br>
    <input type="email" id="dataemail" name="dataemail" class="input-box" style="width: 48%;" placeholder="Your Email ID"  required>
    <br><br>
    <input type="password" id="datapass" name="datapass" class="input-box" style="width: 48%;" placeholder="Password"  required>
    <br><br>
    <input type="text" id="dob" name="dob" class="input-box" style="width: 48%;" placeholder="Date of Birth" onfocus="(this.type='date')" min="1901-01-01" max="1999-12-31"  required>
    <br>
    <p style="font-size:150%;">

    <input type="radio" id="male" name="gender" value="Male" required>
    <label for="male">Male</label>
    <input type="radio" id="female" name="gender" value="Female">
    <label for="female">Female</label>
    <input type="radio" id="other" name="gender" value="Other">
    <label for="other">Other</label>
    </p>

    <p><span><input type="checkbox" required></span> I agree to these <a href= "##">terms and conditions </a></p>
    <button type:"button" class="signupbutton">Sign Up</button>

    <hr>
    <p class="or">OR</p></form> 

This is my php code

<?php
    $dbhost = 'localhost' ;
    $username = 'root' ;
    $password = '' ;
    $db = 'task';

    $fname = filter_input(INPUT_POST,'fname');
    $lname = filter_input(INPUT_POST,'lname');
    $dataemail = filter_input(INPUT_POST,'fdataemail');
    $datapass = filter_input(INPUT_POST,'datapass');
    $dob = filter_input(INPUT_POST,'dob');
    $gender = filter_input(INPUT_POST,'gender');


    $conn =new mysqli("$dbhost", "$username", "$password", "$db" );
    echo "Connected to db";

    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    $sql = "INSERT INTO `signup` (`First name`, `Last name`, `Email ID`, `Password`, `Date of Birth`, `Gender`)
    VALUES ('$fname','$lname','$dataemail','$datapass','$dob','$gender')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

$conn->close();
?>

错误

There is no error in the connection between <html> and <php> . <php> seems to be not properly connected to the database.

<button type:"button" class="signupbutton">Sign Up</button> : No "=" after "type".

Please check your PHP version and if possible update the question with that information too. If you are working with PHP version prior to 5.2.9 and 5.3.0 you have to use the following code as the $connect_error was broken until PHP 5.2.9 and 5.3.0

if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

instead of

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

Refer the official page of mysqli construct

I did this and it worked. Thanks.

action="http://localhost/connect.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