-1

I have a function that calls a HTML code switch when the 'signup' button is clicked. The problem I am having is when I test for a wrong password or wrong email on sign up. I successfully check if the data is correct, but it loads the previous html. Do you know how I can prevent this?

      $(document).ready(function(){
    $("#signupButton").click(function(e){
        e.preventDefault();
        $.ajax({url: "/bookwebsite/html/signup.txt", success: function(result){
            $("body").html(result);
        }});
    });

Above code is the way ajax switches out the HTML body which leads to the 'signup' page.

    //confirm password and confirmed password are the same
      if($signup_password != $signup_password_confirm){
        echo "<font color='red'>Passwords do not match</font>";
        //session_destroy();
        //echo $signup_password . $signup_password_confirm;
        break;
      }

Above code is the PHP that checks that passwords are matching. The commented stuff is things I was just trying.

Thank you!

odus
  • 48
  • 8
  • The signup.txt is what switches with the body in the HTML. – odus Jan 14 '16 at 20:49
  • 1
    So then where does any PHP code get executed? It's not clear what you're asking. This code simply gets data from the server and puts it in the `body` tag, but it sounds like you're asking why that exact thing is happening... – David Jan 14 '16 at 20:50
  • Are you trying to `empty()` the `` and then add `html()`? – Kisaragi Jan 14 '16 at 20:51
  • It seems to me that you are loading the same page with ajax so the proces start over again. Why don't you just submit a form using ajax and than return a status code in json form. Something like $data = array('status' => 'success'); echo json_encode($data); You can than load the results with javascript: var data_array = JSON.parse(data); You can also return status => error etc. After that you can than redirect to a success page or something on success or display errors – Rolf Jan 14 '16 at 20:54
  • Well me and a friend are writing this website. I feel this is bad, but all the php, html, ajax are written into one file titled, homePage.php. The only thing that is not in the homePage.php is the signup.txt which is it's own file but the same folder. sorry for not being clear. When the signup.txt is switched into the HTML body it allows for a Sign up form. When this checked for errors using PHP, the HTML body is then switched out causing the user to leave the signup page. – odus Jan 14 '16 at 20:56
  • Please don't use one php page for this. This is a terrible way of thinking. I suggest using something like this: http://code.tutsplus.com/tutorials/building-a-sleek-ajax-email-signup-form--net-13645 – Rolf Jan 14 '16 at 20:59
  • Okay thank you, I'll look over that tutorial and probably rewrite the PHP. Thank you! – odus Jan 14 '16 at 21:03

1 Answers1

0

"break" probably doesn't do what you think it does. You probably want "die". Is there other code in your PHP file that might be displayed?

EDIT: "break" is for leaving a loop. Any code after the break statement will continue to run, so if you have a html echoed out after that somewhere, it will appear in the browser. The "die" statement will end all code execution immediately and no code after it will run.

Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • The other PHP is similar for the error checking. Anything specific you could tell me to post? – odus Jan 14 '16 at 21:02