Jump to content

form validation...


jimfog
 Share

Recommended Posts

I cannot make a form validation code work, I cannot find where is the error-here is the code: The HTML form:

<form  id="form" action="register_new.php" method="post">           Name:<span class="asterisk">*</span><input name="name" type="text" size="25" /></br>        Phone:<span class="asterisk">*</span><input name="phone" type="text" size="25" /><br>	    Username/e-mail:<span class="asterisk">*</span><input name="username/e-mail" type="text" size="25" /></br>	    Password:<span class="asterisk">*</span><input name="password" type="text" size="25" /></br>	    <input name="register date" type="hidden"  /></br>	    <input type="button" value="Submit"  /></form>

The php code that resides in register_new.php:

<?php   require_once 'output_functions.php';require_once 'User_ath_fns.php';$username=$_POST['username/e-mail'];$name=$_POST['name'];$phone=$_POST['phone'];$password=$_POST['password'];if(!isset($username)){echo 'test';}?>

I proceed to validate first that the username is filled in-I did not go to check the rest, I am just doing my tests for now. I cannot find the error above.

Link to comment
Share on other sites

$username is always set because you declared it a few lines back. $_POST['username/e-mail'] will always be set as long as the form was sent, and otherwise PHP will throw a warning because you're trying to assign an unset value to another variable. Form fields that were not filled in arrive to the server-side as a set variable with an empty string as a value.

Link to comment
Share on other sites

How am I going to test if the fields of the form are filled? I am confused.

Link to comment
Share on other sites

if($_POST['username/email' !== ''){  echo $_POST['username/email'];}; //orforeach($_POST as $key => $value){  if($value != ''){    echo $value;  }}

Link to comment
Share on other sites

if($_POST['username/email' !== ''){  echo $_POST['username/email'];}; //orforeach($_POST as $key => $value){  if($value != ''){	echo $value;  }}

Ok, I understand.But in order to make things clearer, I had the impression that in order to check if the fields of a form are field, someone might as use also isset. Am I wrong?
Link to comment
Share on other sites

You really only use isset to tell if the form was submitted at all. For checkboxes you use isset to tell if they checked the box, because if they didn't the browser won't submit anything for it. For normal fields you can just use a string comparison or the empty function, which will check for non-false values.

Link to comment
Share on other sites

You really only use isset to tell if the form was submitted at all. For checkboxes you use isset to tell if they checked the box, because if they didn't the browser won't submit anything for it. For normal fields you can just use a string comparison or the empty function, which will check for non-false values.
Ok I got you, despite what are you saying nothing was happening...until I took a closer look and realized that there is an "issue" with the form: Instead of using <input type="button" value="Submit" /> I should have used <input name="submit" type="submit" /> The first does NOT submit the form in contrast with the second which DOES. So, the problem was that the form was not submitted at all.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...