jquery - Why does my sign up button redirect to index.php? -
i'm confused, can't explain problem well. when click on sign button, redirects index.php
. it's supposed check errors, not redirect or that. i've tried deleting bugs code, changing id's, changing css properties. adding , deleting jquery. have no idea how fix problem.
edit
in success function, have tried remove window.open()
, still redirects index.php
on click of #check_signup
. i'm going keep line there show original code suppose like. code isn't suppose redirecting because code hasn't reached success text yet.
what's suppose happen code checks errors through ajax through check_signup.php. page returns word "success" if code executed properly. code in script.js check word "success" before redirecting index.php.
i tried removing id #check_signup
, page didn't redirect. when try make page alert on click of #check_signup
, doesn't work. page redirecting before click element registered.
this code:
script.js
$(document).ready(function() { /* sign page */ $("#check_signup").click(function() { var username_signup = $("#signup_container input[key='username_signup']").val(); var email_signup = $("#signup_container input[key='email_signup']").val(); var password_signup = $("#signup_container input[key='password_signup']").val(); $.ajax({ type: "post", url: "check_signup.php", data: {username: username_signup, email: email_signup, password: password_signup}, success: function(data){ if(data.indexof("success")) { window.open("index.php","_self"); } else { $("#signup_container").html(data); } } }); }); });
signup.php
<?php require "connect.php"; ?> <!doctype html> <html> <head> <title> website </title> <!-- css files --> <link href="https://fonts.googleapis.com/css?family=open+sans" rel="stylesheet"> <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="css/styles.css"> <!-- js files --> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script> <script src='js/jquery.countdown.js'></script> <script src='js/script.js'></script> </head> <body> <!-- navigation --> <nav class="navbar navbar-default navbar-fixed-top"> <div class="container"> <div class="navbar-brand"> <a href="index.php"> <img src="http://logos-download.com/wp-content/uploads/2016/06/udemy_logo.png" class="img-responsive" id="logo"> </a> </div> <div class="pull-right"> <a href='login.php'> <button type="button" class="btn btn-danger" id="login_button">login</button> </a> </div> </div> </nav> <!-- end of navigation col-md-3 portfolio-item --> <div class="container"> <div class="row vertical-offset-100"> <div class="col-md-4 col-md-offset-4" id='login_form'> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">join free</h3> </div> <div class="panel-body" id='signup_container'> <form> <fieldset> <div class="form-group"> <input class="form-control" placeholder="username" name="username" type="text" key='username_signup' autocomplete="off"> </div> <div class="form-group"> <input class="form-control" placeholder="email" name="email" type="text" key='email_signup' autocomplete="off"> </div> <div class="form-group"> <input class="form-control" placeholder="password" name="password" type="password" value="" key='password_signup' autocomplete="off"> </div> <input class="btn btn-lg btn-primary btn-block" type="button" value="sign now" id='check_signup'> </fieldset> </form> </div> </div> </div> </div> </div> <!-- end of login page -->
check_signup.php
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src='js/script.js'></script> <?php require "connect.php"; error_reporting(0); // variables $username = $_post["username"]; $email = $_post["email"]; $password = $_post["password"]; $md5_password = md5($password); // username echo " <form> <fieldset>"; if(strlen($username) < 3) { echo "<div class='form-group has-error'> <label class='control-label' for='inputerror1'>username requires 3 characters</label> <input type='text' class='form-control' id='inputerror1' key='username_signup' value='$username'> </div>"; } else { $username_count++; } if(strlen($username) > 25) { echo "<div class='form-group has-error'> <label class='control-label' for='inputerror1'>the username limited 25 characters</label> <input type='text' class='form-control' id='inputerror1' value='$username' key='username_signup'> </div>"; } else { $username_count++; } $check_user = $db->query("select * users username='$username'"); $num_user = $check_user->num_rows; if($num_user == 0) { $username_count++; } else { echo "<div class='form-group has-error'> <label class='control-label' for='inputerror1'>username taken </label> <input type='text' class='form-control' id='inputerror1' value='$username' key='username_signup'> </div>"; } if($username_count == 3) { echo "<div class='form-group'> <input class='form-control' placeholder='username' name='username' type='text' id='username_signup' value='$username' key='username_signup'> </div>"; } // end of username // email if (!filter_var($email, filter_validate_email)) { // invalid emailaddress echo "<div class='form-group has-error'> <label class='control-label' for='inputerror1'>invalid email</label> <input type='text' class='form-control' id='inputerror1' value='$email' key='email_signup'> </div>"; } else { $email_count++; } $check_email = $db->query("select * users email='$email'"); $num_email = $check_email->num_rows; if($num_email == 0) { $email_count++; } else { echo "<div class='form-group has-error'> <label class='control-label' for='inputerror1'>email taken</label> <input type='text' class='form-control' id='inputerror1' value='$email' key='email_signup'> </div>"; } if($email_count == 2) { echo "<div class='form-group'> <input class='form-control' placeholder='username' name='username' type='text' id='username_signup' value='$email' key='email_signup'> </div>"; } // end of email // password if(strlen($password) < 6) { echo "<div class='form-group has-error'> <label class='control-label' for='inputerror1'>password requires 6 characters</label> <input type='password' class='form-control' id='inputerror1' value='$password' key='password_signup'> </div>"; } else { $password_count++; echo "<div class='form-group'> <input class='form-control' placeholder='password' name='password' type='password' id='password_signup' value='$password' key='password_signup'> </div>"; } echo "<input class='btn btn-lg btn-primary btn-block' type='button' value='sign now' id='check_signup'> </fieldset> </form>"; // end of password if($username_count == 3 && $email_count == 2 && $password_count == 1) { $db->query("insert users values('','$username','$email','$md5_password')"); $findid = $db->query("select * users username='$username' , password='$md5_password'"); $fetchid = $findid->fetch_object(); $real_id = $fetchid->id; session_start(); $_session["username"] = $real_id; echo "success"; } ?>
i know lot of code. have literally tried can think of. i'm sure it's simple bug, don't know how fix code. i'm trying fix code check errors , not redirect index.php.
i posted 1 before else out. maybe on correct path... tinker see fit , hope helps!!! :-)
notice few things: 1. not using form tags. eliminates normal actions of form submit , redirect issues browsers. means have handle redirect on own. 2. validation field done in php, , done before databases accessed. helps limit calls database , saves resources. 3. using mysqli , prepared statements. security purposes , should standard practice when coding anything. 4. checking number of characters greater , less in same if statments. keeps code clean. 5. closing connections after done them. both security , save resources on server. it's best practice. 6. using exceptions throughout code, throwing them when necessary. helps keeps code clean limiting number of if/else if/else loops have do.
//////////////// form html //////////////// <div class="form" id="signupform"> <fieldset> <div class="form-group"> <input class="form-control" placeholder="username" id="user" type="text" key='username_signup' autocomplete="off"> </div> <div class="form-group"> <input class="form-control" placeholder="email" id="email" type="email" key='email_signup' autocomplete="off"> </div> <div class="form-group"> <input class="form-control" placeholder="password" id="pass" type="password" key='password_signup' autocomplete="off"> </div> <input class="btn btn-lg btn-primary btn-block" type="button" value="sign now" id='signupbutton'> </fieldset> </div> //////////////// jquery/ajax code //////////////// <script> // call form submit on click action $('#signupbutton').on('click', function(e){ // prevent onclick propagating e.stoppropagation(); // set variables form inputs var user = $("#user").val(), email = $('#email').val(), pass = $('#pass').val(); // initiate ajax call external script $.ajax({ type: 'post', url: 'postpage.php', data: { user : user, email : email, pass : pass }, success: function(data){ if(data.indexof("success")) { // successful response var successmessage = data; alert(successmessage); } else { // error response var errormessage = data; alert(errormessage); } } }); }); </script> //////////////// page post ajax //////////////// <? # start try/catch statement check thrown exceptions (error messages) try { # check $_post initiate script if( !empty($_post) ){ # loop through each post value foreach( $_post $key => $val ){ # check if each post value empty , throw , exception , if not set variable if( !empty($val) ){ ${$key} = trim($val); } else { # throw exception (error message) throw new exception("error, missing fields."); } } # check if $user alphanumeric , @ least 3 25 characters if( !ctype_alnum($user) || strlen($user) < 3 || strlen($user) > 25 ){ # throw exception (error message) throw new exception("error, username must alphanumeric , @ least 3 20 characters."); } # check if $email valid if( filter_var($email, filter_validate_email) ){ # throw exception (error message) throw new exception("error, invalid email."); } # check if $pass @ least 6 25 characters if( strlen($pass) < 6 || strlen($pass) > 25 ){ # throw exception (error message) throw new exception("error, password must @ least 6 25 characters."); } # connection data $servername = ""; $username = ""; $password = ""; $dbname = ""; # make mysqli connection $mysqli = new mysqli($servername, $username, $password, $dbname); if ( $mysqli->connect_errno ) { # throw connections error message throw new exception("error, not connect database."); } # prepare query execution $stmt = $mysqli->prepare("select `username`,`email` `users` `username` = ? or `email` = ?"); # bind 2 parameters statement $stmt->bind_param("ss", $user, $email); if ( $stmt === false ) { # throw exception (error message) throw new exception("error, not process data submitted."); } # excecute query $stmt->execute(); if ( $stmt === false ) { # throw exception (error message) throw new exception("error, count not execute database query."); } # bind results variable $stmt->bind_result($users); # fetch data results while($stmt->fetch()){ $foundusers = $users; } if ( $stmt === false ) { # throw exception (error message) throw new exception("error, not results database."); } # set counters username , emails found $usernames = 0; $emails = 0; # loop through each database entry retrieved , check matching usernames , emails foreach( $foundusers $thisuser ){ if( !empty($thisuser["email"]) && $thisuser["email"] == $email ){ # add 1 $emails counter $emails++; } if( !empty($thisuser["username"]) && $thisuser["username"] == $user ){ # add 1 $usernames counter $usernames++; } } # close statement $stmt->close(); $thread = $mysqli->thread_id; $mysqli->kill($thread); #check if matching usernames or emails found if( $usernames > 0 || $emails > 0 ){ # check if $usernames , $emails counter great 0 if( $usernames >= 1 && $emails >= 1 ){ # throw exception (error message) throw new exception("error, username & email taken."); } # check if $usernames counter great 0 if( $usernames >= 1 ) { # throw exception (error message) throw new exception("error, username taken."); } # check if $emails counter great 0 if( $emails >= 1 ) { # throw exception (error message) throw new exception("error, email taken."); } } # make mysqli connection $mysqli = new mysqli($servername, $username, $password, $dbname); if ( $mysqli->connect_errno ) { # throw connections error message throw new exception("error, not connect database."); } # prepare query execution $stmt = $mysqli->prepare("insert `users` ( `username`, `email`, `password`) values (?, ?, ?)"); # bind 2 parameters statement $stmt->bind_param("sss", $user, $email, $pass); if ( $stmt === false ) { # throw exception (error message) throw new exception("error, not process data submitted."); } # excecute query $stmt->execute(); if ( $stmt === false ) { # throw exception (error message) throw new exception("error, count not execute database query."); } # close statement $stmt->close(); $thread = $mysqli->thread_id; $mysqli->kill($thread); # echo success message echo "success, account hase been created!"; } else { # throw exception (error message) throw new exception("error, not initiate script."); } } # catch exceptions thrown , output error catch( exception $e ) { # check if statement still open , close if($stmt){ $stmt->close(); $thread = $mysqli->thread_id; $mysqli->kill($thread); } # echo success message echo $e->getmessage(); }
Comments
Post a Comment