Need help with registering a form and showing user inputs that require attention

This code doesn’t seem to work. When i display html page nothing is echoed out from php. Please help.

	<?php
	
      

   if (isset  ($_POST ['First Name']) &&
   isset($_POST ['Last Name']) &&
   isset ($_POST ['Email_address']) &&
   isset ($_POST ['Username']) &&
   isset ($_POST['Password'])) {
   
   $fname = $_POST['First Name'];
   $lname = $_POST['Last Name'];
   $email = $_POST['Email address'];
   $username = $_POST['Username'];
   $password = $_POST['Password'];
   
   if (!empty ($fname) && !empty ($lname) && !empty ($email) && !empty ($username) && !empty ($password)) {
   echo 'ok';
   }else{
   
   echo 'all fields are required';
   

   }
   
   }
   
	
	 




	
?>

This is html

	<fieldset>
		
		
					
	
		<h1>Please register your details here</h1>
		<div>
		
		First Name: <input type="text" name="First Name"/><br/>
		Last Name: <input type="text" name="Last Name" /><br />
	   <label for="ea">Email address:</label><input type="text" name="Email address"/><br>
		Username:<input type="text" name="Username"/><br
	  <label for="pw">Password:</label><input type="password"name="passwd"id="pw"size="20" />

	  
  
		<input type="submit" name="submit" value="Submit" />
		 </div>
	
	</fieldset>
</form>

	

</form>
hi here is your solution as well as some tips for you.

TIPS
hi your doing following mistake because of your code is not executing successfully.
1.) do not use space for name or id attribute. if you still want to use space than you must treate that space as underscore.
example if your using like this
than in your php code you have to treat as below define
$_POST[‘First_Name’]; instead of $_POST[‘First Name’];
2.) you are using “passwd” as name attribute for password input box in your html form and trying to access this field in php is $_POST[‘Password’]; that is wrong. you have to access that field like this $_POST[‘passwd’];
3.) your are checking all form field with isset() function in if condition than no need to again check it with !empty() function in if condition.

i have posted both of your php and html file with proper code. check it

[php]
#This is html

Please register your details here

First Name:
Last Name:
Email address:
Username:<br Password:
[/php]

here is php file

[php]
#here is php file register.php

if(isset($_POST[‘First_Name’]) && isset($_POST[‘Last_Name’]) && isset($_POST[‘Email_address’]) && isset($_POST[‘Username’]) && isset($_POST[‘passwd’]))
{
$fname = $_POST[‘First_Name’];
$lname = $_POST[‘Last_Name’];
$email = $_POST[‘Email address’];
$username = $_POST[‘Username’];
$password = $_POST[‘passwd’];
}
else
{
echo ‘all fields are required’;
}

[/php]

Reply your feedback…

SR :slight_smile: :slight_smile:

Just curious, not to hijack your thread… except yea…

Anyway, how could you export that information to a text document of some sort, that can hold an array or something? I guess most people would be using a database for that, or something?

Also just a thought, you would probably want to throw some more code in there, like re type password & email and then check if its the same with an if else statement. Probably want to check your username with where ever this is going, to make sure you don’t get duplicates either. =/

First of all tell me, have You use the code given by me and its helpful for you?. second thing what exactly you want to do. so according to that i will reply you which is more use full for you.

SR

Thank you for your coding :slight_smile:

I am looking to validate my register form. Basically i would like to give a message to the user stating what parts of the field are not filled in very well and also security. If you can help me more that would be great :slight_smile:

OK , you want validate your form before submitting it. this can be done with java script function which is validate the form field which is necessary to filled by user correctly.

replace your current html page content with new one define below.

[php]

Please register your details here

First Name:
Last Name:
Email address:
Username:<br Password:

[/php]

reply…
~SR~

I can’t use javascript beauase i’m a student learning PHP only. Do you know a way i can validate showing empty fields with PHP?

Thanks

OK, for validation form using php you need to create more php file for validating form field. this new php file will contain following logic

So now there is three file
1.) index.php contain the form.
2.) process.php contain the validation logic
3.) thanks.php or thanks.html contain the thanks message if user fill up all the entry correct.

// save below file as index.php

[php]

// save below file as index.php

<?=$errorString?>

Please register your details here

First Name:
Last Name:
Email address:
Username:<br Password:

[/php]

save below process.php

[php]
##save below process.php
// list all form field
$allowedFields = array(
‘First_Name’,
‘Last_Name’,
‘Email_address’,
‘Username’,
‘passwd’,
);
// define form field which is required to fill
$requiredFields = array(
‘First_Name’,
‘Email_address’,
‘Username’,
‘passwd’,
);

// checking posted data by user
$errors = array();
foreach($_POST AS $key => $value)
{

if(in_array($key, $allowedFields))
{
    $$key = $value;
     // is this a required field?
    if(in_array($key, $requiredFields) && $value == '')
    {
        $errors[] = "The field $key is required.";
    }
}

}

// checking any error there ?
if(count($errors) > 0)
{
$errorString = ‘

There was an error processing the form.

’;
$errorString .= ‘
    ’;
    foreach($errors as $error)
    {
    $errorString .= “
  • $error
  • ”;
    }
    $errorString .= ‘
’;
// display the previous form
include 'index.php';

}
else
{
// display the thank you page or your success page
header(“Location: thanks.html”);
}
[/php]

save below file as thanks.html
[php]
thanks, for putting correct detail…
[/php]

Put all three file together and run the index.php

SR

Thank you so much!!! :slight_smile: it is working when all the fields are filled in but when they are not it isn’t showing anything when i test it. Do i need to echo something out? Sorry to be a pain.

Sponsor our Newsletter | Privacy Policy | Terms of Service