[英]Jquery $.post Issue
我想将我的帖子数据登录到login.php页面。 但是问题是$.post()
无法正常工作。 告诉我这段代码的错误。
<form id="forgot-username-form" method="post" >
<label id="email">Forgotten your username ?</label>
<input type="email" name="forgot_username" id="user-email" />
<input type="submit" name="forgot_username" value="Send"/>
</form>
$(document).ready(function(){
$("#forgot-username-form").submit(function() {
var email = $("#user-email").val();
alert(email);
$.post("login.php",{email:email},function(result){
alert(result);
});
});
});
if(isset($_POST['email'])){
$email = $_POST['email'];
echo $email;
}
帮助我找到此代码的错误。
$(document).ready(function(){
$("#forgot-username-form").submit(function(e) { //<--note the "e" argument
e.preventDefault(); //<--you forgot this
var email = $("#user-email").val();
alert(email);
$.post("login.php",{email:email},function(result){
alert(result);
});
return false; //<--or you can use this
});
});
并捕获$_POST
。
if(isset($_POST['email'])):
$email = $_POST['email'];
echo $email;
endif;
尝试这个,
if(isset($_POST['email'])){
$email = $_POST['email'];
echo $email;
}
如果您想使用$_GET
方法,请尝试一下,
脚本
$.get("login.php",{email:email},function(result){
alert(result);
});
PHP页面 login.php
if(isset($_GET['email'])){
$email = $_GET['email'];
echo $email;
}
您需要尝试POST
因为您使用的是$.POST
而不是$.GET
if(isset($_POST['email'])){
$email = $_POST['email'];
echo $email;
}
而且您还需要防止诸如
$(document).ready(function(){
$("#forgot-username-form").submit(function(e) {
e.preventDefault(); // Prevent Here
var email = $("#user-email").val();
alert(email);
$.post("login.php",{email:email},function(result){
alert(result);
});
return false;
});
});
您做得正确,只是需要在脚本中进行更改
$(document).ready(function(){
$("#forgot-username-form").submit(function(e) {
e.preventDefault()
var email = $("#user-email").val();
alert(email);
$.post("login.php",{email:email},function(result){
alert(result);
});
});
});
在Submit函数中为事件添加参数e,并通过e.preventDefault函数防止其默认行为。
尝试这种方式:
$.post("login.php",{'email':email},function(result){
alert(result);
});
我所做的更改:我刚刚引用了您的post数据对象的键 。
通过将电子邮件值分配给帖子数据对象,您做错了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.