php - AJAX: return true or false on 'success:' -
i have following ajax script, reason var ok it's not returning true or false form can continue:
function ajax_call(email,title,url){ var email = document.getelementbyid("email").value; var title = document.getelementbyid("title").value; var url = document.getelementbyid("url").value; var parametros = {"emaail":email, "tiitle":title, "uurl":url}; var ok = true; $.ajax({ data: parametros, url: 'validate.php', type: 'post', error: function () { alert("an error has occurred! try again!"); }, success: function (response) { if(response == 'bien') { ok = true; } else { $("#ajax_cal").html(response); ok = false; } } }); return ok; }
html:
<form onsubmit="return ajax_call();"> ... </form>
php:
<?php //////.... if(!empty($errors)) { foreach($errors $error) { echo '<li>'.$error.'</li>'; } } else { echo 'bien'; } ?>
everything works good, except return value. in advance.
prevent submit completely, send ajax request, if it's good, submit form.
html:
<form id="myform"> ... </form>
javascript:
$("#myform").submit(function(e){ // prevent submit e.preventdefault(); var email = document.getelementbyid("email").value; var title = document.getelementbyid("title").value; var url = document.getelementbyid("url").value; var parametros = {"emaail":email, "tiitle":title, "uurl":url}; $.ajax({ data: parametros, url: 'validate.php', type: 'post', context: this, error: function () { alert("an error has occurred! try again!"); }, success: function (response) { if($.trim(response) == 'bien') { this.submit(); // submit, bypassing jquery bound event } else { $("#ajax_call").html(response); } } }); });
Comments
Post a Comment