tjverge Posted November 14, 2010 Share Posted November 14, 2010 when the page loads it is showing "Registration Successful" under the form, even when nothing has yet to be submitted <script language='javascript'> function verifyMe(){ var msg=''; if(document.getElementById('username').value==''){ msg+='- User Name\n\n';} if(document.getElementById('password').value==''){ msg+='- Password\n\n';} if(document.getElementById('userid').value==''){ msg+='- Eve User ID\n\n';} if(document.getElementById('api').value==''){ msg+='- Eve API\n\n';} if(msg!=''){ alert('The following fields are empty or invalid:\n\n'+msg); return false }else{ return true } } </script> <form name='Sign Up' action='signup.php' method='POST' onsubmit='return verifyMe();'> <table class='table_form_1' id='table_form_1' cellspacing='0'> <tr> <td class='ftbl_row_1' ><LABEL for='username' ACCESSKEY='none' ><b style='color:red'>*</b>User Name </td> <td class='ftbl_row_1a' ><input type='text' name='username' id='username' size='45' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='password' ACCESSKEY='none' ><b style='color:red'>*</b>Password </td> <td class='ftbl_row_2a' ><input type='password' name='password' id='password' size='45' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='userid' ACCESSKEY='none' ><b style='color:red'>*</b>Eve User ID </td> <td class='ftbl_row_1a' ><input type='text' name='userid' id='userid' size='45' value=''> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='api' ACCESSKEY='none' ><b style='color:red'>*</b>Eve API </td> <td class='ftbl_row_2a' ><input type='text' name='api' id='api' size='45' value=''> </td> </tr> <tr> <td class='ftbl_row_1' ><LABEL for='email' ACCESSKEY='none' ><b style='color:red'>*</b>E-mail </td> <td class='ftbl_row_1a' ><input type='text' name='email' id='email' size='45' value=''> </td> </tr> <tr> <td colspan='2' align='right'><input type='submit' name='submit' value='Sign Up'> <input type='reset' name='reset' value='Reset'><br /> </td> </tr> </table> </form> <?php $username = $_POST['username']; $password = $_POST['password']; $userid = $_POST['userid']; $api = $_POST['api']; $email= $_POST['email']; mysql_connect("x", "x", "x"); mysql_select_db ("evepay")or die(mysql_error()); $check = "select username from users where username = '".$_POST['username']."';"; $qry = mysql_query($check) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "Sorry, there the username $username is already taken. "; echo "Try again"; exit; } else { $query="insert into users (username, password, userid, api, email) values ('$username', '$password', '$userid', '$api', '$email)"; mysql_query($query); if($query) { echo "Registration Successful"; } else { echo "error in registration".mysql_error(); } } ?> How do I make it only show the successful message after the user fills out the form? Quote Link to comment https://forums.phpfreaks.com/topic/218603-echo-returning-when-it-shouldnt/ Share on other sites More sharing options...
savagenoob Posted November 14, 2010 Share Posted November 14, 2010 Do mysql_query($query) or die(mysql_error()); echo "Registration Successful"; Quote Link to comment https://forums.phpfreaks.com/topic/218603-echo-returning-when-it-shouldnt/#findComment-1133946 Share on other sites More sharing options...
tjverge Posted November 14, 2010 Author Share Posted November 14, 2010 made the change now it reads "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '')' at line 1" Quote Link to comment https://forums.phpfreaks.com/topic/218603-echo-returning-when-it-shouldnt/#findComment-1133947 Share on other sites More sharing options...
Pikachu2000 Posted November 14, 2010 Share Posted November 14, 2010 Look at '$email in the query string. See something missing there? Quote Link to comment https://forums.phpfreaks.com/topic/218603-echo-returning-when-it-shouldnt/#findComment-1133958 Share on other sites More sharing options...
tjverge Posted November 14, 2010 Author Share Posted November 14, 2010 Thanks that fixed that problem, but it's still showing an echo of "Sorry, there the username $username is already taken." when you first go to the page <?php $username = $_POST['username']; $password = $_POST['password']; $userid = $_POST['userid']; $api = $_POST['api']; $email= $_POST['email']; mysql_connect("x", "x", "x"); mysql_select_db ("evepay")or die(mysql_error()); $check = "select username from users where username = '".$_POST['username']."';"; $qry = mysql_query($check) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "Sorry, there the username $username is already taken. "; echo "Try again"; exit; } else { $query="insert into users (username, password, userid, api, email) values ('$username', '$password', '$userid', '$api', '$email')"; mysql_query($query) or die(mysql_error()); echo "Registration Successful"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/218603-echo-returning-when-it-shouldnt/#findComment-1133971 Share on other sites More sharing options...
tjverge Posted November 14, 2010 Author Share Posted November 14, 2010 as a side note when I put the php in it's own file and point the form post to it the whole thing works perfect, I can do it that way if need be but would like it to all be in one file. Quote Link to comment https://forums.phpfreaks.com/topic/218603-echo-returning-when-it-shouldnt/#findComment-1133973 Share on other sites More sharing options...
DavidAM Posted November 14, 2010 Share Posted November 14, 2010 The entire script is executed whenever the page is requested. So when the user visits the page for the first time, the form is displayed and your registration script at the bottom is executed even though the form has not been submitted yet. To prevent this, you should check to see if the form was submitted: if (isset($_POST['submit'])) { // test the name of the submit button // Registration code here } Quote Link to comment https://forums.phpfreaks.com/topic/218603-echo-returning-when-it-shouldnt/#findComment-1133981 Share on other sites More sharing options...
tjverge Posted November 14, 2010 Author Share Posted November 14, 2010 Another lesson learn thanks all for the help. Quote Link to comment https://forums.phpfreaks.com/topic/218603-echo-returning-when-it-shouldnt/#findComment-1133982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.