javascript - Cannot POST/test.php 当我试图将数据输入数据库时​​它给了我这个错误

标签 javascript php html css

当我尝试使用 php 输入数据时,它给我一个错误 Cannot POST/test.php 请教我如何解决它 我尝试了很多但它不起作用 请任何人提供帮助我来解决这个问题。这是我的元素工作

这是我的html代码

<html>
<head>
<title>Login and Registration Form </title>
<link rel="stylesheet" href="style.css" >
</head>
    <body>
        <div class ="login-page">
          <div class ="form">


          <form class="login-form">
            <h1>Log In </h1>
              <input type="text" placeholder="Email id"/>
                <input type="password" placeholder="Password"/>
                <button type="submit">Log In </button>
               <p class="message">Not Registered?<a href="#"> Signup</a></p>

                </form>

          <form class="register-form" action="test.php" method="post">
              <h1>Sign up </h1>  
              <input type="text"  name="name" required placeholder="User Name"/>
                <input type="email"  name="email" required placeholder="Email id"/>
                <input type="password" name="password" required placeholder="Password"/>
                <button name="subject" type="submit" value="Register">Sign up</button>
                <p class="message">Already Registered?<a href="#">Login</a></p>

                </form>

            </div>
            </div>
    <script src='https://code.jquery.com/jquery-3.4.0.min.js'></script>
    <script>
        $('.message a').click(function(){$('form').animate({height:"toggle", opacity : "toggle"}, "slow");
        });
    </script>

    </body>

</html>    

这是我的php代码

<?php
 $name = $_POST['name'];
 $email =$_POST['email'];
 $password =  $_POST['password'];
$con = mysqli_connect('localhost','root','','registeration');
$query = "INSERT INTO `signup`(`name`, `email`, `password`) VALUES ('$name','$email','$password')";

$run=mysqli_query($con,$query);

if($run==TRUE)
    echo "Data Insert Successfully";
 else
     echo "Error !";

?>

最佳答案

当您尝试 POST 到的 PHP 文件与 HTML 文件不在同一目录时,通常会出现此问题。

如果 PHP 文件位于不同的目录中,您需要提供该文件的路径(相对或绝对路径)。例如目录结构如

root
  - html
    - index.html
  - php
    - test.php

您的 HTML 表单应指向 "../php/test.php"

顺便说一句,您的代码容易受到 SQL Injection 的攻击.请使用 PDO 查看使用准备好的语句.

关于javascript - Cannot POST/test.php 当我试图将数据输入数据库时​​它给了我这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678483/

相关文章:

javascript - 从表单输入创建多维关联数组

javascript - 为 Highcharts 中的每一列设置不同的颜色

php - 将 PostgreSQL DEFAULT 与 Doctrine 一起使用?

php - 如何为 Laravel 安装所有必需的 PHP 扩展?

ruby-on-rails - Rails 链接助手中没有值的数据属性

Javascript嵌套For循环数组具有隐藏值

javascript - 如果 body 具有淡入 mp3 声音的类,否则淡出

php - 使用 JOIN 对多个表进行 SQL 查询

javascript - 在 html/jquery 中制作下拉列表,无需遵循任何教程

jquery - 清除字段值后显示标签