Page 1 of 1

HELP!!! Defining variables with form values

Posted: Thu Aug 25, 2011 2:36 am
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.

Re: HELP!!! Defining variables with form values

Posted: Thu Aug 25, 2011 2:47 am
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'])) {
?>

Re: HELP!!! Defining variables with form values

Posted: Thu Aug 25, 2011 2:49 am
by phphelpme
The name for your submit button is: name='registrationbutton'

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

Best wishes

Re: HELP!!! Defining variables with form values

Posted: Thu Aug 25, 2011 2:49 am
by phphelpme
Haha, social beat me too it... lol

Best wishes

Re: HELP!!! Defining variables with form values

Posted: Thu Aug 25, 2011 3:20 am
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.

Re: HELP!!! Defining variables with form values

Posted: Thu Aug 25, 2011 4:07 am
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

Re: HELP!!! Defining variables with form values

Posted: Thu Aug 25, 2011 6:39 am
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.

Re: HELP!!! Defining variables with form values

Posted: Thu Aug 25, 2011 11:13 am
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.

Re: HELP!!! Defining variables with form values

Posted: Thu Aug 25, 2011 1:07 pm
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.