php register script with jquery not working as it should -
i new php , cannot seem understand problem in code:
<?php require('mysql_connect.php'); // if values posted, insert them database. if (isset($_post['username']) && isset($_post['password'])&& isset($_post['phone'])){ $username = $_post['username']; $phone = $_post['phone']; $email = $_post['email']; $password = $_post['password']; $query = "insert `user` (username, password, email,phone) values ('$username', '$password', '$email', '$phone')"; $result = mysql_query($query); if($result){ $msg = "user created successfully."; } echo " <script > function changeit() { $( '#phone,#reg' ).css('display','inline'); } $(document).ready(changeit); </script>"; header('location: http://atestat-cpi.comeze.com/index.html'); } ?> as of understanding of php,this should execute jquery show required elements , display index.html elements shown.the problem when page accesed,it displays nothing,just blank window,so think jquery not executed.please help.
header('location: /') makes redirect, nothing happen. have reorder code , files. show through example way proceed.
in first instance, let's prepare index file. i'll use ajax directly instead typically form submit. this example:
<!doctype html> <html> <head> <!-- load jquery --> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- call own js --> <script type="text/javascript" src="register_user.js"></script> </head> <body> <!-- instead of create form can --> <div id="register"> <label><input id="user" type="text" value="" /></label> <label><input id="pass" type="password" value="" /></label> <label><input id="phone" type="phone" value="" /></label> <label><input id="email" type="email" value="" /></label> <input id="register_user" type="button" value="register!" /> </div> </body> </html> now, let's set javascript file, called register_user.js:
// on dom ready $(function(){ $('#register_user').on('click', function(){ // disable button, prevent multiple calls var $this = $(this); $this.prop('disabled', true); $.ajax({ url: 'register.php', type: 'post', data: { username: $('#user').val(), password: $('#pass').val(), phone: $('#phone').val(), email: $('#email').val() }, success: function(data){ var response = data.split('|'); if( response[0] == '1' ){ // part of code, should in index file $('#phone, #reg').css('display', 'inline'); // restore button (if want) or whatever want .) $this.prop('disabled', false); } else{ alert(response[1]); } } }); }); }); and let's make php user-register script. should standalone script , called register.php:
<?php $responses = array( '-1|one or more required fields not filled', '0|there error creating user. try again.', '1|your user account created successfully!' ); if( isset($_post['username'], $_post['password'], $_post['phone'], $_post['email']) ){ require_once 'mysql_connect.php'; $username = $_post['username']; $password = $_post['password']; $phone = $_post['phone']; $email = $_post['email']; // must use mysqli, let's use current code $query = "insert `user` (username, password, email,phone) values ('$username', '$password', '$email', '$phone')"; $result = mysql_query($query); // if user created successfully, ok. // yes or no (1 or 0) , manage message js in view. if( $result ) echo responses[2]; else echo responses[1]; exit(); } echo responses[0]; exit(); ?> ps: did not test code, can have fails or things fix. not remember example.
Comments
Post a Comment