【问题标题】:html form does not link to php filehtml 表单没有链接到 php 文件
【发布时间】:2020-08-09 17:19:12
【问题描述】:

我创建了一个 html 表单并希望将其连接到 xampp 上的数据库。我已经创建了数据库和表,但是当我单击提交时,php 文件不起作用。我是网络开发的新手。如果我只是运行http://localhost/connect.php,那么它可以工作,但是当我单击表单上的提交时,它会重定向到file:///F:/Xampp/htdocs/connect.php 打开并显示错误。这是我的表单的 html 代码。

<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> 

这是我的php代码

<?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();
?>

【问题讨论】:

  • 您不能以file://..的身份访问PHP文件。
  • 我的提交按钮将它重定向到这个地址,即使我的 html 和 php 文件都在同一个文件夹中,即 F:\Xampp\htdocs
  • 试过action="http://localhost/connect.php"?
  • onSubmit="return validateForm();" 你能发布validateForm() 的作用吗?

标签: php html mysql xampp


【解决方案1】:

请检查您的 PHP 版本,并在可能的情况下使用该信息更新问题。如果您使用的是5.2.95.3.0 之前的PHP 版本,则必须使用以下代码,因为$connect_error 被破坏,直到PHP 5.2.95.3.0

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

而不是

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

参考mysqli construct的官方页面

【讨论】:

    【解决方案2】:

    &lt;html&gt;&lt;php&gt; 之间的连接没有错误。 &lt;php&gt; 似乎没有正确连接到数据库。

    &lt;button type:"button" class="signupbutton"&gt;Sign Up&lt;/button&gt;: "type" 后面没有 "="。

    【讨论】:

    • 没有。当我只运行 php 文件时,我得到 连接到成功创建的 dbNew 记录。和数据库显示新数据。
    【解决方案3】:

    我这样做了,它奏效了。谢谢。

    action="http://localhost/connect.php"
    

    【讨论】:

    • 主要原因是您通过file://访问.html这是错误的。您应该通过完整的 URL http://localhost/myhtml.html 访问 .html 文件
    • 哦。不知道那个。谢谢。
    猜你喜欢
    • 2022-10-15
    • 2019-12-07
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 2016-11-19
    • 2018-02-11
    • 2020-10-05
    相关资源
    最近更新 更多