Reputation: 343
I want to my post data to login.php page. But problem is $.post()
not working. tell me the error of this code.
<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;
}
help me to find a error of this code.
Upvotes: 0
Views: 63
Reputation: 697
try this way :
$.post("login.php",{'email':email},function(result){
alert(result);
});
Changed I have done : I have just quoted your post data object's key.
you were doing wrong by assigning your email value to your post data object.
Upvotes: 0
Reputation: 1064
You are doing correctly only change you need to make is in your script
$(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);
});
});
});
add a parameter e for event in the submit function and prevent its default behaviour by e.preventDefault function.
Upvotes: 0
Reputation: 50787
$(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
});
});
And capture $_POST
.
if(isset($_POST['email'])):
$email = $_POST['email'];
echo $email;
endif;
Upvotes: 1
Reputation: 40639
Try this,
if(isset($_POST['email'])){
$email = $_POST['email'];
echo $email;
}
And if you want to use $_GET
method then try it like,
SCRIPT
$.get("login.php",{email:email},function(result){
alert(result);
});
PHP Page login.php
if(isset($_GET['email'])){
$email = $_GET['email'];
echo $email;
}
Upvotes: 1
Reputation: 28763
You need to try with POST
Because you are using $.POST
not $.GET
if(isset($_POST['email'])){
$email = $_POST['email'];
echo $email;
}
And also you need to prevent the submit action like
$(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;
});
});
Upvotes: 0