AJAX Contact Form is sending empty parameters?
Not a question I want to ask but I really need another pair of eyes on this one as it must be something silly I am missing.
I have used this same script previously so I've no idea what's going wrong.
This is the frnt end code: http://jsfiddle.net/fU3Q3/1/
And the backend PHP is:
<?php
require("../db.php");
// get the posted values
$email=$_POST['email'];
$password=$_POST['password'];
// now validating the username and password
$sql="SELECT * FROM admin WHERE email='".$email."'";
$result=mysql_query($sql) or die ("No such username exists!");
$row=mysql_fetch_array($result);
// if username exists
if(mysql_num_rows($result)>0)
{
// compare the password
if(strcmp($row['password'],$password)==0)
{
echo "yes";
// set the session from here
$_SESSION['email']=$email;
}
else
echo "no";
}
else
echo "no"; 开发者_如何学编程// invalid Login
?>
Using firebug, the url that is sent to ajax-login.php seems to be email=&password=&rand=0.636999888626322
where obviously the bit on the end is the random number.
Any help is much appreciated.
Martin
$.post("ajax-login.php",{ email:$("input#email").val(),password:$("input#password").val
(),rand:Math.random() } ,function(data)
you have 2 id's of the same type, a div id of email, a input id of email for example, you can not call #email as it will stop at the first occurance
For further explanation... id's without associated class names are considered unique, only 1 id can be used however if you have:
<div class='content' id='email'>
<input class='content' id='email'>
you can call these in jquery by using following examples:
formal declaration, type > classname '.' > id '#'
$("div.content#email");
$("input.content#email");
and this will not work without matching the first tag
$(".content#email");
$(".content#email");
as the other said don't share ids and try this:
replace:
{ email:$("#email").val(),password:$("#password").val(),rand:Math.random() }
with
$(this).parents("form").serialize()
http://jsfiddle.net/sjD9q/
You're password and email inputs share an ID with the parent divs that contain them, if you change the div id's then the form should work.
NB: It's bad practice to have multiple ID's of the same name as this will break relevant CSS and Javascript in many browsers.
精彩评论