Jump to content

Echo returning when it shouldn't


tjverge

Recommended Posts

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?

Link to comment
Share on other sites

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"; 
}
?>

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.