当我尝试使用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注入的攻击。请查看使用 PDO 使用预处理语句。