当我尝试使用 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/