PHP Problem

Caporegime
Joined
28 Jan 2003
Posts
39,881
Location
England
So I am having a real problem getting PHP to work.

I have the below code:

<?php
if(isset($_POST['submit_form'])){
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['comments'])){
// Code to execute if everything is fine
}else{
$error="incomplete";
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php if(isset($error)){echo "<p>Please complete all of the required fields</p>";} ?>

<form method="post" action="contact.php">
Name*:<br /><input type="text" name="name" style="<?php if(isset($_POST['submit_form']) && !isset($_POST['name'])){echo "background:#b12828;";} ?>" /><br />
Email Address*:<br /><input type="text" name="email" style="<?php if(isset($_POST['submit_form']) && !isset($_POST['email'])){echo "background:#b12828;";} ?>" /><br />
Phone Number:<br /><input type="text" name="phone" style="<?php if(isset($_POST['submit_form']) && !isset($_POST['phone'])){echo "background:#b12828;";} ?>" /><br />
Comments*:<br /><textarea name="comments" style="<?php if(isset($_POST['submit_form']) && !isset($_POST['comments'])){echo "background:#b12828;";} ?>"></textarea><br />
<input type="submit" value="Submit" />
<input type="hidden" name="submit_form" value="y" />
</form>
</body>
</html>

Which I am just using a test here (http://digital-blacksmith.co.uk/set/contact.php) but the bloody thing will just not work, it doesn't give me anything.

Have I done some something obviously wrong with my code? I cannot see it and it's driving me insane.
 

aln

aln

Associate
Joined
7 Sep 2009
Posts
2,076
Location
West Lothian, Scotland.
So I am having a real problem getting PHP to work.

I have the below code:



Which I am just using a test here (http://digital-blacksmith.co.uk/set/contact.php) but the bloody thing will just not work, it doesn't give me anything.

Have I done some something obviously wrong with my code? I cannot see it and it's driving me insane.

So what are the steps you've tried to figure out what's going wrong?


You should figure out how to debug this yourself, but...

I imagine the problem is because isset (according to php documentation) only returns false in the event of NULL, where as "" != NULL.

P.S. code layout is awful. :p
 
Last edited:
Associate
Joined
5 Jun 2004
Posts
1,338
Location
Hythe, Hants
PHP:
Name*:<br /><input type="text" name="name" style="<?php if(isset($_POST['submit_form']) && !isset($_POST['name'])){echo "background:#b12828;";} ?>" /><br />

And all the other fields would look nicer if you used shorthand if else statements too
 
Caporegime
OP
Joined
28 Jan 2003
Posts
39,881
Location
England
It's not my code was just a sample validation form I was trying out but it just wouldn't work. I just couldn't see what was wrong with it at all.
 

aln

aln

Associate
Joined
7 Sep 2009
Posts
2,076
Location
West Lothian, Scotland.
It's not my code was just a sample validation form I was trying out but it just wouldn't work. I just couldn't see what was wrong with it at all.

Aye, but that's not my point. If you're going to code, you need to learn how to debug; it's a pretty simple process. Takes all of about 20 seconds to realise your post data is being passed through so there is something wrong with the if statements.

Obviously experience (and reading your post data) tellls me that your empty fields aren't the same as the NULL you're checking for, and you may have took a bit longer to cotton on, but I'm willing to bet you never tried echoing crap in those if statements and using var_dump to see what the hell was happening.

You'd have learned more struggling on with a script that looks like this:-

PHP:
<?php
if(isset($_POST['submit_form'])){
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['comments'])){
// Code to execute if everything is fine
echo 1;
}else{
$error="incomplete";
echo 2;
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php if(isset($error)){echo "<p>Please complete all of the required fields</p>";} ?>

<form method="post" action="contact.php">
Name*:<br /><input type="text" name="name" style="<?php if(isset($_POST['submit_form']) && !isset($_POST['name'])){echo "background:#b12828;";}else{ var_dump($_POST); } ?>" /><br />
Email Address*:<br /><input type="text" name="email" style="<?php if(isset($_POST['submit_form']) && !isset($_POST['email'])){echo "background:#b12828;";} ?>" /><br />
Phone Number:<br /><input type="text" name="phone" style="<?php if(isset($_POST['submit_form']) && !isset($_POST['phone'])){echo "background:#b12828;";} ?>" /><br />
Comments*:<br /><textarea name="comments" style="<?php if(isset($_POST['submit_form']) && !isset($_POST['comments'])){echo "background:#b12828;";} ?>"></textarea><br />
<input type="submit" value="Submit" />
<input type="hidden" name="submit_form" value="y" />
</form>
</body>
</html>

You wont necessarily be debugging like that in a project that actually has some resonable debugging built in, but for quick and dirty / learning it'll do the job. Not that I'm having a go, just trying to teach the man how to fish.
 
Last edited:
Caporegime
OP
Joined
28 Jan 2003
Posts
39,881
Location
England
I appreciate the advice. Truth be known it's the first piece of php I have tried to use so I was trying it out to try and work back how it worked. Then it didn't work and I was thrown.
 
Associate
Joined
25 Jan 2009
Posts
1,342
Location
London
Code:
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['comments']))

This condition is never met as all these are set within the post data passed as they are part of the form as empty strings, so you should really check for empty strings and null as well.
 
Associate
Joined
3 May 2013
Posts
6
Location
Southampton
Not sure if this is still an issue as it's been a while since original post, but as others have said, isset() doesn't account for the empty string. It would be better to use empty(), so the validation would be:
PHP:
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments'])) {
// Code to execute if everything is fine
printf("Thanks for the comments, %s!", $_POST['name']);
} else {
$error="incomplete";
}
I'd also advise against using <input type="hidden"> to check if the form has been submitted. It doesn't really add anything, and can just be manipulated by the user in the JavaScript console anyway. Others might know better though.
 
Last edited:
Back
Top Bottom