Skip to main content

JQuery Ajax - Code did work now it does not?



I built a basic form with ajax validation and page redirection with PHP and jQuery. This was working fine a few days back. I have come back to it and the ajax seems to have stopped working. At the moment the ajax is just checking a test var and echoing back data. It is really bugging me as it working fine initially! Seems strange as I cannot spot anything obvious, hoping some fresh eyes can help.





The ajax has stopped sending data across so I click submit and the ajax sends nothing so my if() does not do anything! I get the form values fine. Tried with basic ajax and alerts, something strange seems to be happening.





Hope you can help







<form id="registerform">

<ul id="form">

<li>

<label for="email">Email:</label><span class="fieldbox"><input type="text" id="email"/></span>

</li>

<li>

<label for="confirmemail">Confirm Email:</label><span class="fieldbox"><input type="text" id="confirmemail"/></span>

</li>

<li>

<label for="password">Password:</label> <span class="fieldbox"> <input type="password" id="password"/></span>

</li>

<li>

<label for="confirmpassword">Confirm Password:</label> <span class="fieldbox"> <input type="confirmpassword" id="confirmpassword"/></span>

</li>

<input type="submit" id="register" value="Sign me up!"/>

</ul>

</form>







<div id="errorbox"></div>







jQuery:







$(document).ready(function(){



$("#register").click(function(){



var formvalues = [$("#email").val(), $("#confirmemail").val(), $("#password").val(), $("#confirmpassword").val() ];

var checklength = formvalues[2].length;





if(formvalues[0] == "" || formvalues[1] == ""){

$('#errorbox').fadeIn('slow');

$("#errorbox").html('<p class="errormsg">Please complete email fields.</p><div class="errormsgshodow"></div>');

$("#main").shake(3, 5, 600);

return false;//stop click function

}



if(formvalues[2] == "" || formvalues[3] == ""){

$('#errorbox').fadeIn('slow');

$("#errorbox").html('<p class="errormsg">Please complete password fields.</p><div class="errormsgshodow"></div>');

$("#main").shake(3, 5, 600);

return false;//stop click function

}



if(formvalues[0] != formvalues[1]){

$('#errorbox').fadeIn('slow');

$("#errorbox").html('<p class="errormsg">Password field do not match</p><div class="errormsgshodow"></div>');

$("#main").shake(3, 5, 600);

return false;//stop click function

}



if(formvalues[2] != formvalues[3]){

$('#errorbox').fadeIn('slow');

$("#errorbox").html('<p class="errormsg">Password field do not match</p><div class="errormsgshodow"></div>');

$("#main").shake(3, 5, 600);

return false;//stop click function

}



if(checklength < 6 || checklength > 6){

$('#errorbox').fadeIn('slow');

$("#errorbox").html('<p class="errormsg">Password is too long.</p><div class="errormsgshodow"></div>');

$("#main").shake(3, 5, 600);

return false;//stop click function

}



var datastring = 'email=' + formvalues[1] + '&password=' + formvalues[3];



$.ajax({

type: "POST",

url: "signup.php",

data: datastring,

success: function(data){

//see redirect script for stuff here

if(data != "error"){

window.location="http://localhost/creativetree/projectboard.php?id="+data;

}else{

$('#errorbox').fadeIn('slow');

$("#errorbox").html('<p class="errormsg">User account already exists with that email.</p><div class="errormsgshodow"></div>');;

}







}



});



});



});//jquery end













//shake functions by Amit Dhamu

jQuery.fn.shake = function(intShakes /*Amount of shakes*/, intDistance /*Shake distance*/, intDuration /*Time duration*/) {

this.each(function() {

$(this).css({position:"relative"});



for (var x=1; x<=intShakes; x++) {

$(this).animate({left:(intDistance*-1)}, (((intDuration/intShakes)/4)))

.animate({left:intDistance}, ((intDuration/intShakes)/2))

.animate({left:0}, (((intDuration/intShakes)/4)));

}

});

return this;

};







And minimal PHP for result:







<?php





if(isset($_POST['email'])){



$email = $_POST['email'];

$password = $_POST['password'];



if($email == "test"){

echo $email;

}else{

echo 'error';

}

}





?>




Comments

  1. It's hard to tell without seeing the error you are receiving, but from what I can see, you are not returning anything from your function upon successful validation, so your form will be submitted normally as well.

    You can prevent the form from submitting by using something like:

    $("#registerform").submit(function() {
    return false;
    });


    but perhaps returning false from the submit button click handler is enough as well.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex