Forms Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GioG7
Forum Newbie
Posts: 6
Joined: Tue Nov 02, 2010 7:19 pm

Forms Question

Post 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.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Forms Question

Post by yacahuma »

enters blank fields?

I dont undesrtand your question. You have a demo page?
tutigan
Forum Newbie
Posts: 18
Joined: Thu Oct 28, 2010 5:43 am

Re: Forms Question

Post 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!)
GioG7
Forum Newbie
Posts: 6
Joined: Tue Nov 02, 2010 7:19 pm

Re: Forms Question

Post 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>
GioG7
Forum Newbie
Posts: 6
Joined: Tue Nov 02, 2010 7:19 pm

Re: Forms Question

Post by GioG7 »

Anyone?
tabutcher
Forum Newbie
Posts: 20
Joined: Fri Aug 21, 2009 7:10 am

Re: Forms Question

Post 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.
GioG7
Forum Newbie
Posts: 6
Joined: Tue Nov 02, 2010 7:19 pm

Re: Forms Question

Post 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.
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Forms Question

Post 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.
User avatar
awebtech
Forum Newbie
Posts: 21
Joined: Sun Nov 07, 2010 4:54 pm
Location: Russian Federation

Re: Forms Question

Post 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]}')";
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Forms Question

Post by McInfo »

It is impossible to defend a castle that has no walls.
GioG7
Forum Newbie
Posts: 6
Joined: Tue Nov 02, 2010 7:19 pm

Re: Forms Question

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Forms Question

Post 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.
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Forms Question

Post by s992 »

Code: Select all

<?php

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

<!-- Drop your form in -->

<?php
} // closes else
?>
GioG7
Forum Newbie
Posts: 6
Joined: Tue Nov 02, 2010 7:19 pm

Re: Forms Question

Post 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!
Post Reply