/    Sign up×
Community /Pin to ProfileBookmark

PHP and HTM Validation Problem

hi

i have the following code which sets up text boxes for input

<form method=”post” action=”ex20-03.php”>
<table border=”0″ width=”90%” class=”txtSt”>
<tr><td colspan=”2″ class=”txtSt2″>General Information</td>
<td colspan=”2″ class=”txtSt2″>Login Information</td></tr>
<tr><td >Name</td><td>
<input type=”text” name=”name” class=”butSt”>&nbsp;&nbsp;&nbsp;</td>
<td >User Id</td>
<td ><input type=”text” name=”user_id” class=”butSt”></td></tr>
<tr><td >Gender</td>
<td><input type=”text” name=”gender” class=”butSt”></td>
<td >Password</td>
<td ><input type=”password” name=”pass_id” class=”butSt”></td></tr>
<tr><td >Age</td>
<td><input type=”text” name=”age” class=”butSt”></td>
<td >Confirm Password</td>
<td ><input type=”password” name=”con_pass_id” class=”butSt”></td></tr>
<tr><td >Address</td>
<td><input type=”text” name=”add01″ class=”butSt”></td>
<td colspan=”2″ class=”txtSt2″>Credit Card Information</td></tr>
<tr><td >Address</td>
<td><input type=”text” name=”add02″ class=”butSt”></td>
<td >Credit Card Type</td>
<td ><input type=”text” name=”card_type” class=”butSt”></td></tr>
<tr><td >City</td>
<td><input type=”text” name=”city” class=”butSt”></td>
<td >Card Number</td>
<td ><input type=”text” name=”card_no” class=”butSt”></td></tr>
<tr><td >Country</td>
<td><input type=”text” name=”country” class=”butSt”></td>
<td >Expiry Date</td>
<td ><input type=”text” name=”expiry_date” class=”butSt”></td></tr>
<tr><td >Postal Code</td>
<td><input type=”text” name=”post_code” class=”butSt”></td>
<td colspan=”2″ class=”txtSt2″>Contact Information</td></tr>
<tr><td >Email Address</td>
<td><input type=”text” name=”email” class=”butSt”></td>
<td >Phone Number</td>
<td ><input type=”text” name=”phone_no” class=”butSt”></td></tr>
</table><br /><br />
<input type=”submit” value=”Register” class=”butSt”>
<input type=”reset” value=”Clear” class=”butSt”>
</form>

now this seems to work no problem in setting up the web-page however when i come to validate the contents it does not work

when the submit button is triggered it calls some php like bellow

<?php
if ( (trim($name)==””) || (trim($gender)==””) || (trim($email)==””) ||
(trim($age)==””) || (trim($add01)==””) ||
(trim($add02)==””) || (trim($city)==””) ||
(trim($country)==””) || (trim($post_code)==””))
{
echo “Error.. General Information is not complete!”;
exit();
}

there is more than this but this is the first part and it never gets past this

even though there is data in every text field its stops here and says that some fields ar empty

can anyone please help

thanks scott

probaly something really simple for you guys

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 06.2006 — Unless register_globals is on (which it really should not be), you'll need to reference the values from the $_POST array:

[code=php]
if ( (trim($_POST['name'])=="") || (trim($_POST['gender'])=="") ||
[/code]

If that's not the case, or if you've copied the $_POST array vales to scalar variables, then the only other thing is to double-check that you don't have a spelling error somewhere, as comparing an uninitialized variable to "" will always return true.
Copy linkTweet thisAlerts:
@themartyJun 06.2006 — my guess is that is has something to do with [url=http://www.php.net/register_globals]register_globals[/url] and/or [url=http://nl2.php.net/manual/en/language.variables.predefined.php#language.variables.superglobals]superglobals[/url]
Copy linkTweet thisAlerts:
@NogDogJun 06.2006 — PS: a cleaner way to check the required values in the $_POST array might be:

[code=php]
$required = array('name', 'gender', 'email', 'age', 'add01', 'add02', 'city', 'country', 'post_code');
foreach ($required as $ix)
{
if(empty(trim($_POST[$ix])))
{
echo "Error.. General Information is not complete!";
exit();
}
}
[/code]

Also, should "add02" be required? (Usually this is an optional field for things like apartment numbers.)
Copy linkTweet thisAlerts:
@themartyJun 06.2006 — Beware: [url=http://www.php.net/empty]empty()[/url] also returns True if the value is [b]0[/b].

[code=php]if (trim($_POST['some_field']) == "")[/code]is therefor better (imho)
Copy linkTweet thisAlerts:
@sinkyauthorJun 06.2006 — Beware: [url=http://www.php.net/empty]empty()[/url] also returns True if the value is [b]0[/b].

[code=php]if (trim($_POST['some_field']) == "")[/code]is therefor better (imho)[/QUOTE]



i tried this but is now comes up with a parse error


really dont know why my original code doesnt work

thanks for the help so far guys

im sure we'll crack this
Copy linkTweet thisAlerts:
@themartyJun 07.2006 — i tried this but is now comes up with a parse error[/quote]
You should give a bit more code. The error is not always on the line php indicates it is.

really dont know why my original code doesnt work[/quote]
Because you're using old-fashioned code. It only works if the register_globals setting is switched on, which is often not the case. (read the two links i posted earlier)
Copy linkTweet thisAlerts:
@NogDogJun 07.2006 — When you can't figure out why something like that is working, it's often useful to add some temporary debug code. For instance, you might want to echo out each of the variables be checked in your if statement to see what values they have. Then if only one of them is coming up empty, you've narrowed down the problem and know where to focus your attention.
Copy linkTweet thisAlerts:
@sinkyauthorJun 07.2006 — here is the new problem code
[code=php]
<?php


if ( (trim($_POST['name']) == "") || (trim($_POST['gender']) == "") || (trim($_POST['email']) == "") ||
(trim($_POST['age']) == "") || (trim($_POST['add01']) == "") ||
(trim($_POST['add02']) == "") || (trim($_POST['city']) == "") ||
(trim($_POST['country']) == "") || (trim($_POST['post_code']) == "") )
{
echo "Error.. General Information is not complete!";
exit();
}
else if ( (trim($_POST['user_id']) == "") || (trim($_POST['pass_id']) == "") ||
(trim($con_pass_id)=="") )
{
echo "Error.. Login Information is not complete!";
exit();
}
else if ( (trim($_POST['card_type']) == "") || (trim($_POST['card_no']) == "") ||
(trim($_POST['expiry_date']) == ""))
{
echo "Error.. Credit Card Information is not complete!";
exit();
}
else if (trim($_POST['phone_no'] == "")
{
echo "Error.. Contact Information is not complete!";
exit();
}
else if (trim($_POST['con_pass_id'] != (trim($_POST['pass_id']) ) {
echo "Error.. Two Passwords do not matched!";
exit();
}
else
{

echo "Registration Successful! <br /> Enjoy Your Shopping";
exit();
}
?>
[/code]


the error i get is

Parse error: parse error, unexpected '{' in line 32


thanks for the help so far guys
Copy linkTweet thisAlerts:
@themartyJun 07.2006 — Parse error: parse error, unexpected '{' in line 32[/quote]
So, what does this error tell you? That on line 32 PHP encounters a { where it didn't expect one. If it expected something else, you probably didn't close a certain delimiter. So, first step is to use color-coding:

[code=php]if (
(trim($_POST['name']) == "") || (trim($_POST['gender']) == "") || (trim($_POST['email']) == "") ||
(trim($_POST['age']) == "") || (trim($_POST['add01']) == "") || (trim($_POST['add02']) == "") ||
(trim($_POST['city']) == "") || (trim($_POST['country']) == "") || (trim($_POST['post_code']) == "")
)
{
echo "Error.. General Information is not complete!";
exit();
}
else if ( (trim($_POST['user_id']) == "") || (trim($_POST['pass_id']) == "") || (trim($con_pass_id)=="") )
{
echo "Error.. Login Information is not complete!";
exit();
}
else if ( (trim($_POST['card_type']) == "") || (trim($_POST['card_no']) == "") || (trim($_POST['expiry_date']) == ""))
{
echo "Error.. Credit Card Information is not complete!";
exit();
}
else if (trim($_POST['phone_no'] == "")
{
echo "Error.. Contact Information is not complete!";
exit();
}
else if (trim($_POST['con_pass_id'] != (trim($_POST['pass_id']) )
{
echo "Error.. Two Passwords do not matched!";
exit();
}
else
{
echo "Registration Successful! <br /> Enjoy Your Shopping";
exit();
}[/code]


If you forgot a double quote, for example, with colorcoding you would see this immediately. At first glance this appears not to be the case though

As you can see, i also [i]indented[/i] your code. This is a very important practice that helps debugging a lot. Furthermore, consistancy in your code is important too. In your code for example, you sometimes put the { on the same line as the (else)if and sometimes on the line below it. That makes it very hard to read and track down errors. Better choose one or the other.

A final point of critic: don't use so many [i]exit[/i]s. If you keep using it this way, you'll come to a point where you have a very big piece of code and a blank screen and no way of finding out what went wrong where. It's much better to force your code down the right path by using things like if/else structures.

Now, how to find this bug. As i mentioned before: the most likely mistake is that you forgot to 'close ' something. So, start by the line directly above it. Nothing supsicious there. The line above that: there's double quotes ... but they seem to be ok too. On the line above that there's some single quotes and also quite a lot of (). The single quotes seem to be ok, so count the amount of ( and compare that with the amount of ).

etc. etc.

that's how you debug
Copy linkTweet thisAlerts:
@NogDogJun 07.2006 — Parentheses problem: This line...
[code=php]else if (trim($_POST['con_pass_id'] != (trim($_POST['pass_id']) ) {[/code]
...should be...
[code=php]else if (trim($_POST['con_pass_id']) != trim($_POST['pass_id']) ) {[/code]
×

Success!

Help @sinky spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...