Page 1 of 1

Forms Question

Posted: Tue Nov 02, 2010 7:26 pm
by GioG7
Hello everyone,

Don't mind me but I have just started messing around with php and databases. I just need some help with a form I have created. There are several fields like name, email, etc, that are then submitted into a mysql database once the user hits "Register". My question is why is it that as soon as the page is loaded the form automatically enters blank fields? How can I stop it from doing this? Oh and other than that the form works perfectly, whatever field you fill out gets entered in the database correctly.

Re: Forms Question

Posted: Tue Nov 02, 2010 10:51 pm
by yacahuma
enters blank fields?

I dont undesrtand your question. You have a demo page?

Re: Forms Question

Posted: Tue Nov 02, 2010 10:57 pm
by tutigan
can you show us your code please? that way we can help you much easier!
(don't forget to wrap it with the code tags by clicking the 'PHP Code' button when replying!)

Re: Forms Question

Posted: Wed Nov 03, 2010 6:22 pm
by GioG7
yacahuma wrote:enters blank fields?

I dont undesrtand your question. You have a demo page?
Yes exactly that. If you look at the database every time you load the page blank fields are inserted into the date base. So under first name, last name, email, phone number, etc is just blank with no information. It's supposed to insert information only when you hit the "Register button. So that is my question. Why is it doing it as soon as you load the page if the "Register" button has not been hit.

Here is my code:
Keep in mind I am only inserting "first_name" and "last_name" for now just for testing reasons.

Code: Select all

<?php
		$con = mysql_connect("localhost","username","password");
		if (!$con) {
			die('Could not connect: ' . mysql_error());
			}
			
		mysql_select_db("database", $con);
		
		$sql="INSERT INTO users (first_name, last_name) VALUES ('$_POST[first_name]', '$_POST[last_name]')";
		if (!mysql_query($sql, $con)) {
			die('Error: ' . mysql_error());
			}
		
		mysql_close($con)
	?>

<form  name="registration_form" method="post" action="registration.php" onsubmit="return Validate();">
        <table>
            <tr>
                <td align="right">Username</td>
                <td><input type="text" name="username"></td>
            </tr>
            
            <tr>
                <td align="right">Password</td>
                <td><input type="password" name="password"></td>
            </tr>
            
            <tr>
                <td align="right">Confirm Password</td>
                <td><input type="password" name="confirm_password"></td>
            </tr>
            
            <tr>
                <td align="right">First Name</td>
                <td><input type="text" name="first_name"></td>
            </tr>
            
            <tr>
                <td align="right">Last Name</td>
                <td><input type="text" name="last_name"></td>
            </tr>
            
            <tr>
                <td align="right">Birthdate</td>
                <td>
                    <SELECT name="birth_date_month">
                    <option value="">Month</option>
					<?php for ($i = 1; $i <= 12; $i++) : ?>
                    <option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option>
					<?php endfor; ?>
                    </SELECT>
                    
                    <SELECT name="birth_date_day">
                    <option value="">Day</option>
					<?php for ($i = 1; $i <= 31; $i++) : ?>
                    <option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option>
					<?php endfor; ?>
                    </SELECT>
                    
                   <SELECT name="birth_date_year">
                   <option value="">Year</option>
				   <?php for ($i = 1900; $i < date('Y'); $i++) : ?>
                   <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
				   <?php endfor; ?>
                   </SELECT>
                </td>
            </tr>
            
            <tr>
                <td align="right">Email</td>
                <td><input type="text" name="email"></td>
            </tr>
            
            <tr>
                <td align="right">Confirm Email</td>
                <td><input type="text" name="confirm_email"></td>
            </tr>
            
            <tr>
                <td align="right">Phone</td>
                <td><input type="text" name="phone_number"></td>
            </tr>
            
            <tr>
                <td align="right">Address</td>
                <td><input type="email" name="address"></td>
            </tr>
            
            <tr>
                <td align="right"></td>
                <td><input type="submit" name="submit" value="Register"></td>
            </tr>
        </table>
    </form>
	
	<script language="JavaScript" type="text/javascript">
		function Validate() {
			if (document.registration_form.username.value == '') {
				alert('Please fill in your username.');
				return false;
				}
				
			if (document.registration_form.password.value == '') {
				alert('Please fill in your password.');
				return false;
				}
				
			if (document.registration_form.confirm_password.value == '') {
				alert('Please fill in your password again for confirmation.');
				return false;
				}
				
			if (document.registration_form.password.value != document.registration_form.confirm_password.value) {
				alert("The two passwords are not identical." + "Please enter the same password again for confirmation.");
				return false;
				}
			
			if (document.registration_form.first_name.value == '') {
				alert('Please fill in your first name.');
				return false;
				}
				
			if (document.registration_form.last_name.value == '') {
				alert('Please fill in your last name.');
				return false;
				}
				
			if (document.registration_form.birth_date_month.value == '') {
				alert('Please select the month you were born.');
				return false;
				}
				
			if (document.registration_form.birth_date_day.value == '') {
				alert('Please select the day you were born.');
				return false;
				}
				
			if (document.registration_form.birth_date_year.value == '') {
				alert('Please select the year you were born.');
				return false;
				}
				
			if (document.registration_form.email.value == '') {
				alert('Please fill in your email address.');
				return false;
				}
				
			if (document.registration_form.email.value != document.registration_form.confirm_email.value) {
				alert("The two email addresses are not identical." + "Please enter the same email again for confirmation.");
				return false;
				}
				
				return true;
			}
    </script>

Re: Forms Question

Posted: Thu Nov 04, 2010 6:35 pm
by GioG7
Anyone?

Re: Forms Question

Posted: Thu Nov 04, 2010 6:52 pm
by tabutcher
I think it enters blank information into the database because it is running the php code at the start before you have a chance to fill out the form. In my opinion you should separate the php code that connects you to the database and only call it when you click the register button.

Re: Forms Question

Posted: Sun Nov 07, 2010 3:13 pm
by GioG7
Nope, that didn't help... unless I'm not writing it correctly where it is supposed to go.

I can't figure what causes this problem, please help.

Re: Forms Question

Posted: Sun Nov 07, 2010 4:12 pm
by s992

Code: Select all

$sql="INSERT INTO users (first_name, last_name) VALUES ('$_POST[first_name]', '$_POST[last_name]')";
Try wrapping "first_name" and "last_name" in quotes, i.e.:

Code: Select all

$sql="INSERT INTO users (first_name, last_name) VALUES ('" . $_POST['first_name'] . "', '" . $_POST['last_name'] . "')";
To elaborate, echoing $_POST[first_name] (no quotes), you'll still get the value but you'll also get an error:

Code: Select all

Notice: Use of undefined constant first_name - assumed 'first_name' in D:\wamp\www\form.php on line 3
I'm not sure if the data will get passed to the DB with that error being thrown.

Re: Forms Question

Posted: Sun Nov 07, 2010 5:11 pm
by awebtech
Hi,

I would replace this line:

Code: Select all

$sql="INSERT INTO users (first_name, last_name) VALUES ('$_POST[first_name]', '$_POST[last_name]')";
with this one:

Code: Select all

$sql="INSERT INTO users (first_name, last_name) VALUES ('{$_POST[first_name]}', '{$_POST[last_name]}')";

Re: Forms Question

Posted: Sun Nov 07, 2010 6:33 pm
by McInfo
It is impossible to defend a castle that has no walls.

Re: Forms Question

Posted: Sun Nov 07, 2010 11:28 pm
by GioG7
I just tried both of s992's and awebtech's suggestion but every time the page loads it still inserts blanks fields under first name, last name, etc.

Image

Re: Forms Question

Posted: Sun Nov 07, 2010 11:40 pm
by McInfo
GioG7 wrote:Why is it doing it as soon as you load the page if the "Register" button has not been hit.
Because you are not telling it not to.

The $_POST fields are empty if the form is not submitted, yet the query runs every time the page is loaded.

Re: Forms Question

Posted: Mon Nov 08, 2010 12:15 am
by s992

Code: Select all

<?php

if($_POST) {
	// Database stuff here
} else {
?>

<!-- Drop your form in -->

<?php
} // closes else
?>

Re: Forms Question

Posted: Mon Nov 08, 2010 7:02 pm
by GioG7
GOT IT! Thanks for the help everyone! You guys are the best! I might be around in the future with some other questions if you all don't mind haha thanks again guys!