Reputation: 8951
I've been trying to implement a PHP contact for for a website, which seems admittedly like something I shouldn't be stuck on, but I keep running into trouble. I've been putting my HTML and PHP in separate files which I figure is good practice, so my setup looks like this:
HTML (My index.php file)
<head>
<?php include '../scripts/site.php' ?>
</head>
<form action='../scripts/site.php' method='post'>
<input type=text class="formfields" placeholder="Name" name="name">
<br><br>
<input type=email class="formfields" placeholder="Email" name="contact_email"> <br><br>
<textarea type=text placeholder="Tell Us About Your Project" name="message"></textarea>
<br><br>
<input class="submit" type="submit" value="Send">
</form>
PHP (site.php file in my scripts directory)
<?php
if(isset($_POST['name']) && isset($_POST['contact_email']) && isset($_POST['message'])) {
echo $name = $_POST['name'];
echo $contact_email = $_POST['contact_email'];
echo $message = $_POST['message'];
if(!empty($name) && !empty($contact_email) && !empty($message){
$to = '[email protected]';
$subject = 'contact email';
$body = $message;
$headers = 'From: ' .$contact_email;
mail($to, $subject, $body, $headers);
if(mail($to, $subject, $body, $headers)){
echo 'Thanks for contacting us!';
}
else{
echo 'Message could not be sent';
}
}
else{
echo 'All fields are required';
}
}
?>
I feel like I'm probably making a structural error in my code when trying to get the HTML and PHP to communicate with each other. I think the mailer itself is fine, but I could be wrong.
I'm trying this on a live site, so it's not a locahost/host issue. (And yes, I realize the form isn't remotely secure, which I plan on fixing as soon as I get it to mail properly.)
Upvotes: 2
Views: 417
Reputation: 324
Remove the =
from the if statements. It should be (!empty())
instead of (!=empty())
Also, you never closed your ()
in the second if statement.
if((!empty($name)) && (!empty($contact_email)) && (!empty($message))){
The above line is what would fix a lot of your issues. Add that to your script.
Upvotes: 2
Reputation: 492
In PHP, the use of NOT EQUAL
when using isset or empty, is actually the following;
if(!empty()){
if(!isset()){
Your usage is correct for comparing values such as in the following case;
if($var1 != $var2){
There may be some additional helpful information here; http://php.net/manual/en/language.operators.comparison.php but that should help you in the meantime.
Upvotes: 0
Reputation: 2676
You don't have to include your PHP code if you're referring to it in the action attribute of the form.
Also, if you could be more specific about the errors you're seeing, I'll be better able to help you.
Brushing up you current code and removing any possible bugs will lead you here. Try this instead and let me know if there are any more errors.
<?php
if(isset($_POST['name']) && isset($_POST['contact_email']) && isset($_POST['message'])) {
$name = $_POST['name'];
$contact_email = $_POST['contact_email'];
$message = $_POST['message'];
if(!empty($name) && !empty($contact_email) && !empty($message){
$to = '[email protected]';
$subject = 'contact email';
$body = $message;
$headers = 'From: ' .$contact_email;
mail($to, $subject, $body, $headers);
if(mail($to, $subject, $body, $headers)){
echo 'Thanks for contacting us!';
}
else{
echo 'Message could not be sent';
}
}
else{
echo 'All fields are required';
}
}
?>
Upvotes: 0