HELP!!! Defining variables with form values

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
RossBryan
Forum Newbie
Posts: 19
Joined: Fri Aug 19, 2011 3:03 pm

HELP!!! Defining variables with form values

Post by RossBryan »

How would i go about correctly defining the following variables that i am taking in from a form.

Code: Select all

<?php require ("design/top.php"); ?>

<title>Care2Share - Register</title>
    
<div id= 'left'>
    
<?php
$form = "<form action='register.php' method='post'>
<table cellspacing='10px'>
<tr>
	<td><input type='text' id='usernameboxReg' name='user' tabindex='1' value='Username' class='textbox' onfocus='usernameboxReg_focus();' onblur='usernameboxReg_blur();'></td>
    <td><font size='-1' color='#FF0000'>(*required)</font></td>
</tr> 
<tr>
    <td><input type='text' id='emailbox' name='email' tabindex='2' value='Email' class='textbox' onfocus='emailbox_focus();' onblur='emailbox_blur();' /></td>
	<td><font size='-1' color='#FF0000'>(*required)</font></td>
</tr>
<tr>
    <td><input type='text' id='passwordboxReg' name='pass' tabindex='3' value='Password' class='textbox' onfocus='passwordboxReg_focus();' onblur='passwordboxReg_blur();' /></td>
	<td><font size='-1' color='#FF0000'>(*required)</font></td>
</tr>
<tr>
    <td><input type='submit' name='registrationbutton' class='button' value='Register'/></td>
</tr>

</table>
</form>";

if (isset($_POST['registerbutton']))
{
	$user = $_POST['user'];	
	$email = $_POST['email'];
	$password = $_POST['pass'];
	
	if ($user == "Username")
		$user = "";
	if ($email == "Email")
			$email = ""; 
	
		if ($user && $email && $password)//ensures these three values are submitted 
		{
			if (strstr ($email, "@")&& strstr($email, "."))
			{
				require ("design/connect.php");
				
				$query = mysql_query("SELECT * FROM users WHERE user='$user'");//select everything from the users table where everything =the username provided
				$numrows = mysql_num_rows($query);//does the query say the username is taken?
				
				
			}
		
			else
				echo "That is not a valid email.$form";
				
		}
		else 
			echo "You did not fill in the required fields.$form"; 	

		}

else
	echo "$form";

?>
    
</div>
	
 <?php require ("design/bottom.php"); ?> 
Ive tried to apply the (isset ()) function for example :(isset($_POST='user')
but it doesnt seem to be defining my new variable. could anyone possible lend me some advise.
Last edited by RossBryan on Thu Aug 25, 2011 3:17 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: HELP!!! Defining variables with form values

Post by social_experiment »

Any value from the $_POST array can be accessed by using the name attribute of the specific field you want to retrieve. You already have that figured out but you used the incorrect value when attempting to use isset() to see if the button has been clicked.

Code: Select all

<?php
if (isset($_POST['registerbutton'])) {
 // rest of code
// should be 
if (isset($_POST['registrationbutton'])) {
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: HELP!!! Defining variables with form values

Post by phphelpme »

The name for your submit button is: name='registrationbutton'

Yet you isset($_POST['registerbutton']))

Best wishes
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: HELP!!! Defining variables with form values

Post by phphelpme »

Haha, social beat me too it... lol

Best wishes
RossBryan
Forum Newbie
Posts: 19
Joined: Fri Aug 19, 2011 3:03 pm

Re: HELP!!! Defining variables with form values

Post by RossBryan »

That hasnt worked. I didnt want to post the whole code because i didnt want to clog up the post, but have edited it with it anyway now; maybe that will help.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: HELP!!! Defining variables with form values

Post by phphelpme »

How do you mean? you have changed the above coding to show the change or you have just changed your code on your computer and it still does not work?

Are you getting any error codes at all?

Best wishes
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: HELP!!! Defining variables with form values

Post by social_experiment »

If you didn't change value to the name of the post button, as it was in the original script, it wouldn't have executed the code in that if statement.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
RossBryan
Forum Newbie
Posts: 19
Joined: Fri Aug 19, 2011 3:03 pm

Re: HELP!!! Defining variables with form values

Post by RossBryan »

Yes, I changed the value of the registerbutton post button to match according to with what is used in the if statement, thanks for that. But I'm still getting an undefined notice for '$user' when I call on it.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: HELP!!! Defining variables with form values

Post by social_experiment »

Code: Select all

/*
Array
(
    [user] => Billy
    [email] => me@me.me
    [pass] => cccccc
    [registrationbutton] => Register
)
*/
The form seems to be working, this is a dump of the $_POST array after submission.

Code: Select all

<?php
if ($user == "Username")
                $user = "";
        if ($email == "Email")
                        $email = ""; 
                if ($user && $email && $password)//ensures these three values are submitted 
                {
                        if (strstr ($email, "@")&& strstr($email, "."))
?>
The problem might be in your subsequent code.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply