title: jQuery plugin Validation email check if else CRAZY WIERD
body: <p>I have successfully implemented the Jquery Validation Plugin <a href="http://posabsolute.github.com/jQuery-Validation-Engine/" rel="nofollow">http://posabsolute.github.com/jQuery-Validation-Engine/</a> but i am now trying to get an ajax database email check to work (email exists / email available) and i have written some php script to get this done. Its kinda working but i am getting the most unexpected heretically odd behavior from my IF ELSE statement (seems really crazy to me). observe ### marked comments</p>
  
  <p>PHP code: LOOK AT THE IF ELSE STATEMENT</p>
  
  <pre><code>/* RECEIVE VALUE */
  $validateValue = $_REQUEST['fieldValue'];
  $validateId = $_REQUEST['fieldId'];
  
  
  $validateError = "This username is already taken";
  $validateSuccess = "This username is available";
  
  
  /* RETURN VALUE */
  $arrayToJs = array();
  $arrayToJs[0] = $validateId;
  
  $req = "SELECT Email
    FROM business
    WHERE Email = '$validateValue'";
  
  $query = mysql_query($req);
  while ($row = mysql_fetch_array($query)) {
    $results = array($row['Email']);
  }
  
  
  
  if (in_array($validateValue, $results)) {
  
     $arrayToJs[1] = false;
     echo json_encode($arrayToJs); // RETURN ARRAY WITH ERROR ### popup shows "validating, please wait" then "This username is already taken" when email typed is in database - i.e. Working
     file_put_contents('output.txt', print_r("1 in array - Email is Taken  " . $validateValue, true)); ### this runs!!
  
  }else{
  
    $arrayToJs[1] = true; // RETURN TRUE
    echo json_encode($arrayToJs); // RETURN ARRAY WITH success ### popup shows "validating, please wait" when email typed is NOT in the database - i.e. not Working
    file_put_contents('output.txt', print_r("2 else - Email is available  " .   $validateValue, true)); 
    //### THIS RUNS TOO !!!!!!!!!!!!! i.e. echo json_encode($arrayToJs) wont work for both.. If I change (in_array()) to (!in_array()) i get the reverse when email is in database. 
    //i.e. only the else statements echo json_encode($arrayToJs) runs and the popup msg shows up green "This username is available" crazy right??? 
    //so basically IF ELSE statements run as expected (confirmed by output.txt) but only one echo json_encode($arrayToJs) will work.!!!! 
    //If i remove the json_encode($arrayToJs) statements and place it once after the IF ELSE statement i get the same problem.
    //both $arrayToJs[1] = false; and $arrayToJs[1] = true; can work separately depending on which is first run IF or ELSE but they will not work in the one after another;
    }
  </code></pre>
  
  <p>HERE IS THE REST OF THE CODE--></p>
  
  <p>1-HTML FORM INPUT CODE:</p>
  
  <pre><code>    &lt;tr&gt;
          &lt;td&gt; &lt;Label&gt;Business Email&lt;/Label&gt;
              &lt;br&gt;
              &lt;input type="text" name="Email" id="Email" class="validate[required,custom[email],ajax[ajaxUserCallPhp]] text-input"&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  </code></pre>
  
  <p>2-Relevant JQUERY code in jquery.validationEngine.js:</p>
  
  <pre><code>$.ajax({
              type: type,
              url: url,
              cache: false,
              dataType: dataType,
              data: data,
              form: form,
              methods: methods,
              options: options,
              beforeSend: function() {
                  return options.onBeforeAjaxFormValidation(form, options);
              },
              error: function(data, transport) {
                  methods._ajaxError(data, transport);
              },
              success: function(json) {
                  if ((dataType == "json") &amp;&amp; (json !== true)) {
                      // getting to this case doesn't necessary means that the form is invalid
                      // the server may return green or closing prompt actions
                      // this flag helps figuring it out
                      var errorInForm=false;
                      for (var i = 0; i &lt; json.length; i++) {
                          var value = json[i];
  
                          var errorFieldId = value[0];
                          var errorField = $($("#" + errorFieldId)[0]);
  
                          // make sure we found the element
                          if (errorField.length == 1) {
  
                              // promptText or selector
                              var msg = value[2];
                              // if the field is valid
                              if (value[1] == true) {
  
                                  if (msg == ""  || !msg){
                                      // if for some reason, status==true and error="", just close the prompt
                                      methods._closePrompt(errorField);
                                  } else {
                                      // the field is valid, but we are displaying a green prompt
                                      if (options.allrules[msg]) {
                                          var txt = options.allrules[msg].alertTextOk;
                                          if (txt)
                                              msg = txt;
                                      }
                                      if (options.showPrompts) methods._showPrompt(errorField, msg, "pass", false, options, true);
                                  }
                              } else {
                                  // the field is invalid, show the red error prompt
                                  errorInForm|=true;
                                  if (options.allrules[msg]) {
                                      var txt = options.allrules[msg].alertText;
                                      if (txt)
                                          msg = txt;
                                  }
                                  if(options.showPrompts) methods._showPrompt(errorField, msg, "", false, options, true);
                              }
                          }
                      }
                      options.onAjaxFormComplete(!errorInForm, form, json, options);
                  } else
                      options.onAjaxFormComplete(true, form, json, options);
  
              }
          });
  </code></pre>
  
  <p>3-Relevent code for ajaxUserCallPhp in jquery.validationEngine-en.js:</p>
  
  <pre><code>"ajaxUserCallPhp": {
                  "url": "validation/php/ajaxValidateFieldUser.php",
                  // you may want to pass extra data on the ajax call
                  "extraData": "name=eric",
                  // if you provide an "alertTextOk", it will show as a green prompt when the field validates
                  "alertTextOk": "* This username is available",
                  "alertText": "* This user is already taken",
                  "alertTextLoad": "*Validating, please wait"
              },
  </code></pre>
  
  <p>Im sure the problem lies with this echo. </p>
  
  <p>echo json_encode($arrayToJs)</p>
  
  <p>Please help i've spent to long on this and its almost working fully. </p>
  
  <p>To clarify - I basically am trying to code it so that if i type an email in the db it shows red "This username is taken" then if i edit the input box to an email not in the database it changes to green "username is available" at the moment only one json_encode will run in any scenario no matter how i change the if else statement –</p>
  
  <p>Thank you very much in advance.</p>