954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP form

Just trying to get a very simple form working
Getting a
Parse error: syntax error, unexpected T_IS_EQUAL on line 10

<?php
$to = "";
$subject = "Contact Us";
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$embody = "Name: $name\nEmail: $email\nSuggestion: $message" ;
$sent = mail($to, $subject, $embody, $headers) ;
if($name || $email || $message) == "" {
	echo "You seem to have left something out. Please fill in all the fields and try again.<a href=\"javascript:closeWindow();\">Close</a>"; }
else
if($sent)
	{echo "Thank you $name, your mail has been sent successfully with the message of:<i>$message</i><a href=\"javascript:closeWindow();\">Close</a>"; }
else
	{echo "We encountered an error sending your mail<a href=\"javascript:closeWindow();\">Close</a>"; }
?>


I know its something with the checking if the fields are blank, but I can't find what
Thanks

Mouse1989
Newbie Poster
12 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Just trying to get a very simple form working Getting a Parse error: syntax error, unexpected T_IS_EQUAL on line 10

<?php
$to = "";
$subject = "Contact Us";
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$embody = "Name: $name\nEmail: $email\nSuggestion: $message" ;
$sent = mail($to, $subject, $embody, $headers) ;
if($name || $email || $message) == "" {
	echo "You seem to have left something out. Please fill in all the fields and try again.<a href=\"javascript:closeWindow();\">Close</a>"; }
else
if($sent)
	{echo "Thank you $name, your mail has been sent successfully with the message of:<i>$message</i><a href=\"javascript:closeWindow();\">Close</a>"; }
else
	{echo "We encountered an error sending your mail<a href=\"javascript:closeWindow();\">Close</a>"; }
?>

I know its something with the checking if the fields are blank, but I can't find what Thanks

In the above code you forgot to put

braces for the if statement, try the following

if(($name || $email || $message) == "")


Thanks & Regards
Sai Prem

saiprem
Posting Whiz in Training
210 posts since Feb 2010
Reputation Points: 16
Solved Threads: 28
 

In the above code you forgot to put

braces for the if statement, try the following

if(($name || $email || $message) == "")

That fixed the error, but now I am getting the "You seem to have left something out. P..." all the time even when everything is filled in.

Mouse1989
Newbie Poster
12 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

If you are doing it from local environment, the mail sending will not works for you. You need to set the smtp address and port number in the php.ini file

saiprem
Posting Whiz in Training
210 posts since Feb 2010
Reputation Points: 16
Solved Threads: 28
 

It all works fine and I get the form mail through to my email inbox. Everything is ok, I just wish to check the boxes are not blank

Mouse1989
Newbie Poster
12 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Try changing if($name || $email || $message) == "" to if(empty($name)||empty($mail)||empty($message)) { }

The preferred method of checking whether or not a variable is empty is with the empty() function ( <em>bool</em> <strong>empty</strong> ( <em>mixed</em> $var ) ).
The OR operator (||) doesn't work quite the way you used it above.
You also left out a closing bracket.

Jerail
Light Poster
39 posts since Feb 2010
Reputation Points: 11
Solved Threads: 5
 
<form method="post" action="contact.php" onsubmit="window.open('contact.php','popup','width=200,height=200,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false">
        Name: <input name="name" type="text" maxlength="15" value="">
        
Email:&nbsp;&nbsp;<input name="email" type="text" maxlength="50"  value="">

Suggestion:
<textarea name="message" rows="5" cols="40"  value=""></textarea>

<input type="submit" value="Submit"><input type="reset" value="Reset">
	    </form>
$to = "email ommitted";
$subject = "Contact Us";
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$embody = "Name: $name\nEmail: $email\nSuggestion: $message" ;
$sent = mail($to, $subject, $embody, $headers) ;
if(empty($name)||empty($mail)||empty($message))  {
	echo "You seem to have left something out. Please fill in all the fields and try again.<a href=\"javascript:closeWindow();\">Close</a>"; }
else 
if($sent)
	{echo "Thank you $name, your mail has been sent successfully with the message of:<i>$message</i><a href=\"javascript:closeWindow();\">Close</a>"; }
else
	{echo "We encountered an error sending your mail<a href=\"javascript:closeWindow();\">Close</a>"; }
?>


Still same problem :/ get blank error even when all boxes are filled, forgot to add the HTML form before. Possible conflict with value=""?

Mouse1989
Newbie Poster
12 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Yes you should not use value="" in your source, and more over the default will be null, it is not necessary to put that, remove that and retry again

saiprem
Posting Whiz in Training
210 posts since Feb 2010
Reputation Points: 16
Solved Threads: 28
 

Change empty($mail) to empty($email)

There's a discrepancy between your variables. $mail does not exist.
Also, you should really do your validationbefore sending the e-mail with mail(). This would avoid a lot of unnecessary blank e-mails being sent.

Also, do what saiprem said and remove the value = "" attributes from your HTML form. value isn't even a valid attribute of <textarea> .

Jerail
Light Poster
39 posts since Feb 2010
Reputation Points: 11
Solved Threads: 5
 

Thanks for the discrepancy note

How would I go about doing validation beforehand?

Mouse1989
Newbie Poster
12 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Well, you have your mail() function before the if(empty) functions. What you should really do is:

if(empty($email)...etc...) {
    echo "error";
}
else {
    mail($to,$etc);
    echo "success";
}
Jerail
Light Poster
39 posts since Feb 2010
Reputation Points: 11
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You