Php代码在成功验证来自JS的表单后不执行

s4n0splo  于 2023-02-28  发布在  PHP
关注(0)|答案(2)|浏览(107)

php代码在表单验证成功后没有执行。我已经在运行XAMPP服务器时运行了代码,但似乎不起作用。我在成功提交表单后在屏幕上看到php代码,而不是让它执行。

``

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">                  
    <title>Data Entry  </title>
    <script src="validate.js"></script>
</head>
<body>
    <form action="validate.php" method="POST">
        <label for="" onfocus="info_name()">Name</label>
        <input type="text" id="name" name="name"><br><br>

        <label for="" onfocus="info_age()">Age</label>
        <input type="number" min="1" name='age' id="age"><br><br>

        <label for="" onfocus="info_address()">Address</label>
        <input type="text" name="address" id="address"><br><br>

        <label for="" onfocus="info_phone()">Phone No.</label>
        <input type="text" name="number" id="number"><br><br>

        <label for="" onfocus="info_email()">Email Address</label>
        <input type="email" name='email' id="email"><br><br>

        <p><input type="submit" id="submit" onclick="validate(event)" value="Submit"></p>
    </form>
</body>
</html>

<?php
    $name=$_POST['name'];
    $age=$_POST['age'];
    $address=$_POST['address'];
    $phone=$_POST['number'];
    $email=$_POST['email'];

    $conn=new mysqli("localhost",'root','');

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

    $sql="create database if not exists DATA-ENTRY";
    if($conn->query($sql)){
`
hivapdat

hivapdat1#

检查您的文件扩展名,它应该以.php结尾

ewm0tg9j

ewm0tg9j2#

PHP代码似乎未被执行,因为它位于HTML代码之后。应在将任何输出发送到浏览器之前执行PHP代码。您可以尝试使用以下代码解决此问题:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $age = $_POST['age'];
    $address = $_POST['address'];
    $phone = $_POST['number'];
    $email = $_POST['email'];

    $conn = new mysqli("localhost", 'root', '', 'DATA-ENTRY');

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

    $sql = "CREATE TABLE IF NOT EXISTS data (
        id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(30) NOT NULL,
        age INT(3) NOT NULL,
        address VARCHAR(50) NOT NULL,
        phone VARCHAR(15) NOT NULL,
        email VARCHAR(50) NOT NULL
    )";

    if ($conn->query($sql)) {
        $sql = "INSERT INTO data (name, age, address, phone, email) VALUES ('$name', '$age', '$address', '$phone', '$email')";

        if ($conn->query($sql)) {
            echo "Data inserted successfully!";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
}

?〉

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">                  
    <title>Data Entry  </title>
    <script src="validate.js"></script>
</head>
<body>
    <form action="" method="POST">
        <label for="" onfocus="info_name()">Name</label>
        <input type="text" id="name" name="name"><br><br>

        <label for="" onfocus="info_age()">Age</label>
        <input type="number" min="1" name='age' id="age"><br><br>

        <label for="" onfocus="info_address()">Address</label>
        <input type="text" name="address" id="address"><br><br>

        <label for="" onfocus="info_phone()">Phone No.</label>
        <input type="text" name="number" id="number"><br><br>

        <label for="" onfocus="info_email()">Email Address</label>
        <input type="email" name='email' id="email"><br><br>

        <p><input type="submit" id="submit" onclick="validate(event)" value="Submit"></p>
    </form>
</body>
</html>

相关问题