我想发送电子邮件地址到我的submit.php
并通过jquery接收回复。
submit.php
和index.html
在网站根目录中,而script.js
在'js'文件夹中。
我的index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input id="email" type="email" placeholder=" Email"><br>
<a href=""><button id="submit-btn">send</button></a>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous">
</script>
<script src="js/script.js"></script>
</body>
</html>
我的script.js
:
$(document).ready(()=>{
$("#submit-btn").click(()=>{
$.post("../submit.php", { user_email: $("#email").val()})
.done((data)=>{
alert(data);
})
.fail((xhr, status, error)=>{
alert(error);
});
});
});
我的submit.php
:
<?php
$email = (isset($_POST['user_email']) ? $_POST['user_email'] : "nothing");
echo $email;
我希望输出为我的电子邮件地址,但实际输出为not found
。
我认为我的请求失败了
答案 0 :(得分:1)
由于您说两个文件都在同一个文件夹中,因此不需要../
。
$.post("../submit.php", { user_email: $("#email").val()})
$.post("submit.php", { user_email: $("#email").val()})