1

I have this code in JavaScript and jQuery and I need to fix it:

function valid_form() {
val_avail_email=valid_avail_email();

if (val_avail_email!="") {
$("#email_valid").html(val_avail_email);
return false;
}

return true;
}

function valid_avail_email() {
    var error="";

    $.get("valid_avail_email.php",{email:document.reg_form.email_reg.value},function(output) {
        error=output;
    });

    return error;
}

So obviously I want the error variable to contain the output of the valid_avail_email.php and than make the valid_avail_email() function return that value.

Here is the code in valid_avail_email.php:

<?php
include("config.php");

$email=$_GET["email"];

$sql="select email from users";
$sql_query=mysql_query($sql);

if (!$sql_query) {
    echo mysql_error();
}

$res_assoc = mysql_fetch_assoc($sql_query);

foreach ($res_assoc as $field=>$value) {
   if ($value==$email) {
       echo "please use another email this one is taken ";
   }
}

?>  
mak
  • 11
  • 2
  • 4
    This is an important moment in your javascript career :) – Kenan Banks Nov 05 '11 at 10:38
  • Sidenote: you fetch ALL email addresses from your database and then loop them to find $email? This is very inefficient. $email = mysql_real_escape_string($_GET['email']); $sql = "SELECT id FROM users WHERE email = '{$email}'"; // check if there are results – dirkbonhomme Nov 05 '11 at 10:47

2 Answers2

1

Problem is that the $.get() function initiates an asynchronous ajax call, means, the function returns while the ajax call is still running. You have to inform the GUI from within the ajax callback function, e.g.:

$.get("valid_avail_email.php",{email:document.reg_form.email_reg.value},function(output) {
  error=output;
  if (error !== '')
    alert('Sorry, use another email!');

});

Svante Svenson
  • 12,315
  • 4
  • 41
  • 45
Alex Schenkel
  • 728
  • 1
  • 7
  • 13
  • thank you for replying but i really need the valid_avail_email() function to return the error value becuase i have another function that is getting the errors and stop the submitting if there is an error – mak Nov 05 '11 at 11:05
  • Well, if you want check the emails on the server-side, you HAVE to wait for the asynchronous return of you ajax call. There is a possibility to initiate a synchronous ajax call, but this is highly not recommended... So call your 2nd function from within the ajax success() callback, and check it first. – Alex Schenkel Nov 05 '11 at 11:21
0

you don't need to do a foreach loop to check if the email is in db. just do a following query:

SELECT email FROM users WHERE email = '$email'

and check if the result contains any item. this method is faster than enumerating all the emails with "foreach" loop because the db returns 1 or 0 results and not thousands if you have a big db.

your valid_avail_email() is ok.. just check in your JS if the function returns anything, if yes, there alert shows the error:

var emailOk = valid_avail_email("blabla@email.com");
if(emailOk != ""){
//the following alert will show the error from php, it can show mysql_error() or "please use another email.." message
alert(emailOk);
//do stuff to abort the script
}
MilMike
  • 12,571
  • 15
  • 65
  • 82
  • i added the whole code you can see why i need the valid_avail_email() function to return an error value. – mak Nov 05 '11 at 11:24