JQuery PHP Response Validation
My problem is that I want to validate the email with jquery. Not just the syntax, but rather if the email is already registered. There are some tutorials out there, but they don't work! First the JQuery code:
<script id="demo" type="text/javascript">
$(document).ready(function() {
// validate signup form on keyup and submit
var validator = $("form#signupform").validate({
rules: {
Vorname: {
required: true,
minlength: 3
},
Nachname:{
required: true,
minlength: 4
},
password: {
required: true,
minlength: 5
},
password_confirm: {
required: true,
minlength: 5,
equalTo: "#password"
},
Email: {
required: true,
email: true,
type: "POST",
remote: "remotemail.php"
},
dateformat: "required",
...
</script>
And now the PHP code:
<?php
include('dbsettings.php');
$conn = mysql_connect($dbhost,$dbuser,$dbpw);
mysql_select_db($dbdb,$conn);
$auslesen1 = "SELECT Email FROM flo_user";
$auslesen2 = mysql_query($auslesen1,$conn);
$registered_email = mysql_fetch_assoc($auslesen2);
$requested_email = $_POST['Email'];
if( in_array($requested_email, $registered_email) ){
echo "false";
}
else{
echo "true";
}
?>
I tried returning TRUE / return FALSE, but it shows "email is registered" all the time. json_encode
doesn't work either.
Thank you very much!
+2
a source to share
1 answer
From the docs, the value is passed as a get parameter. Replace $ _POST ['Email'] with $ _GET ['Email'] and see if that works for you.
Also in_array does not handle multidimensional arrays where the array is located. Perhaps a better technique would be to add a WHERE clause to your SQL statement, for example
$email = mysql_real_escape_string($_GET['Email']);
$sql = 'select Email from users where Email = '.$email;
$result = mysql_query($query, $dbconn);
$resultAsArray = mysql_fetch_assoc($result);
if(count($resultAsArray)==0) {
echo true;
}
else {
echo false;
}
0
a source to share