Issue with a PHP Online Order Form - php

I've created a contact form for a website with multiple fields. The clear button works perfectly, but when I click the Submit button, I inadvertently download the .php file associated with the webpage, instead of sending an email with the order form to the specified address. I'm really new to php here, so any help would be greatly appreciated.
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_quantity = $_POST['cf_quantity'];
$field_which = $_POST['cf_which'];
$field_size = $_POST['cf_size'];
$field_school = $_POST['cf_school'];
$field_college = $_POST['cf_college'];
$field_message = $_POST['cf_message'];
$mail_to = 'xxx#gmail.com';
$subject = 'TShirt Order - xxx.Org '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail Address: '.$field_email."\n";
$body_message .= 'Quantity of Shirts: '.$field_quantity."\n";
$body_message .= 'Shirt Style(s): '.field_which."\n";
$body_message .= 'Size(s): '.field_size."\n";
$body_message .= 'School: '.field_school."\n";
$body_message .= 'College: '.field_college."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for placing an order with xxx.Org. We will contact you shortly.');
window.location = 'contact_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to xxx#gmail.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>
the html code is:
<form action="contact.php" method="post">
Full Name:
<input type="text" name="cf_name">
<br />
<br />
Email Address:
<input type="text" name="cf_email">
<br />
<br />
Quantity of TShirts:
<input type="numeric" name="cf_quantity">
<br />
<br />
Which Style(s)?
<input type="text" name="cf_which">
<br />
<br />
Size(s):
<input type="text" name="cf_size">
<br />
<br />
Name of School:
<input type="text" name="cf_school">
<br />
<br />
Name of College:
<input type="text" name="cf_college">
<br />
<br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>

Are you completely sure the server accepts php? Seams to me the server doesn't understand it should use the php interpeter and instead of activating the php commands it just lets you download the file.
If you're not sure try and make a file with a simple in it, call that and see if it works.
best regards,
Mark

Related

Contact Form Not Working - php file is returning the "else" failed response

I have tried looking in similar questions posted here, but cannot find an answer (got about 1900 questions popping up).
I have a contact form on a page at a site I am creating on the webpage hosting site
https://www.one.com/en/
Here is the form on the page contact/contact.shtml
<form action="contact/mail.php" method="post"><br>
Your name<br>
<input type="text" name="cf_name"><br>
Your e-mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
Here is the content of the contact/mail.php file
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'clashton#gmail.com';
$subject = ' Contact Form '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.shtml';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to clashton#gmail.com');
window.location = 'contact.shtml';
</script>
<?php
}
?>
When I try to send a message a window pops up containing the error message "Message failed. Please, send an email to clashton#gmail.com". In other words the "else" action is happening.
If it seems that this question has a trivial answer, please keep in mind that I am completely new at this.
Thanks for any help.

Simple Contact form PHP fail [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 7 years ago.
I'm completely new to HTML and PHP and am trying to build a simple contact form, so that users can send me a message on the contact page, which then gets directed to my email. Any ideas what I'm doing wrong here:
contact.html:
<section id="secondary">
<form action="contact.php" method="post" class="contactform">
Your Name<br>
<input type="text" maxlength="100" name="cf_name" style="width: 300px" class="contactboxes"><br>
Your E-mail<br>
<input type="text" maxlength="100" name="cf_email" style="width: 300px" class="contactboxes"><br>
The Reason for Contact<br>
<textarea name="cf_message" maxlength="500" style="width: 300px" class="contactboxes"></textarea><br>
<input type="submit" value="Send" class="sendclearbutton">
<input type="reset" value="Clear" class="sendclearbutton">
</form>
</section>
contact.php:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'sharan#hotmail.co.uk';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to sharan#hotmail.co.uk.');
window.location = 'contact.html';
</script>
<?php
}
?>
It looks fine to me, as Lance said, the only reason I can think that it wont be working is that you don't have PHP installed. Download Apache or whatever suits your computer best or use web hosting online. Do say if other code is working, though.

Email form showing ,PHP Content instead of executing and sending email

I'm trying to get a 'contact us email form' to work on my website but when I press the 'send' button, it doesnt send the email and it shows the entire contents of the .php file thats supposed to make it send? Can anybody help me please? my html page i called 'Contact_Us.html' and the PHP file is called 'contact.php".
Here is the HTML
<form action="contact.php" method="post">
Your name<br>
<input type="text" name="cf_name"><br>
Your e-mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
and here is the .PHP
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'user#domain.com';
$subject = 'Message from a visitor on The New Moston Club Wesbite '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you, we have received your message. We will contact you shortly!');
window.location = 'Contact_Us.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to thenewmostonclub#outlook.com sorry for any inconvenience');
window.location = 'Contact_Us.html';
</script>
<?php
}
?>
does it show in your browsers URL bar c:/user/.../yourscript.php if yes then try localhost/yourscript.php

php tick box array for email form

i'm new to stackoverflow but after trying a number of way i can't get my head around this email form. The form works fine but i've tried to add 4 optional tick boxes so the client can tick so we know which package they're interested in. I think i've got the front end setup properly:
<p><label for="author">Full Name:</label><br />
<input type="text" name="cf_name" class="details" tabindex="1" /></p>
<p><label for="telephone">Telephone:</label><br />
<input type="text" name="cf_telephone" class="details" tabindex="2" /></p>
<p><label for="email">E-mail:</label><br />
<input type="text" name="cf_email" class="details" tabindex="3" /></p>
<p><label for="package">Package Type:</label><br />
<input type="checkbox" name="cf_package[]" class="package" value="Business English Classes" tabindex="4" /> Business English Classes<br />
<input type="checkbox" name="cf_package[]" class="package" value="English Conversation Classes" tabindex="4" /> English Conversation Classes<br />
<input type="checkbox" name="cf_package[]" class="package" value="Exam Preparation Classes" tabindex="4" /> Exam Preparation Classes<br />
<input type="checkbox" name="cf_package[]" class="package" value="Writing Skills Classes" tabindex="4" /> Writing Skills Classes
</p>
<p><label for="message">Questions/Comments:</label><br />
<textarea type="text" name="cf_message" class="questions" tabindex="8"></textarea></p>
<p style="padding-top:10px;"><input type="submit" name="submit" value="Submit" class="button" tabindex="9" /> <input type="reset" name="submit" value="Clear" class="button" tabindex="10" /></p>
My backend script is as follows:
<code><?php
// get all values from form and remove spaces before/after values
$field_name = trim($_POST['cf_name']);
$field_telephone = trim($_POST['cf_telephone']);
$field_email = trim($_POST['cf_email']);
$field_package = trim($_POST['cf_package']);
$field_message = trim($_POST['cf_message']);
$mail_to = 'theenglishbeehive#gmail.com';
$subject = 'Enquiry from '.$field_name;
// check if user input own e-mail -> generate headers and send mail
if ($field_email && $field_message)
{
// generate body of message
$body_message = "From: ".$field_name."\n";
$body_message .= "Telephone: ".$field_telephone."\n";
$body_message .= "E-mail: ".$field_email."\n";
$body_message .= "Package Type: ".$field_package."\n";
$body_message .= "Questions/Comments: ".$field_message;
$headers = "From: ".$field_email."\n";
$headers .= "Reply-To: ".$field_email."\n";
$headers .= "Return-Path: ".$field_email."\n";
$headers .= "X-Priority: 3 (Normal)\n";
// send email
$mail_status = mail($mail_to, $subject, $body_message, $headers);
}
// success ?
if ($mail_status)
{
?>
<script language="javascript" type="text/javascript">
window.location = 'enquiry-sent.php';
</script>
<?php
}
else
{
?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to theenglishbeehive#gmail.com');
window.location = 'index.php';
</script>
<?php
}
?>
</code>
Hopefully i've got this showing properly on the website too.
The selected 'package' will be sent to the server as an array, not as a string, because you're using the array notation ([]) and using checkboxes, people can select multiple packages.
You might want to convert the array to a comma-separated string and show which packageS the user selected:
$field_package = implode(', ', $_POST['cf_package']);
and then:
$body_message .= "Package Type(s): ".$field_package."\n";
If only a single option should be allowed to be selected, you might consider using a radio-button in stead of checkboxes.

PHP mail form not working

I am making a feedback form for my website using php, I thought that it was working, but then it took an arrow to the code.
Anyways, the form has 4 fields: "name", "email", "subject", and "message".
I get the "Message failed" alert if all four fields have content.
Seeing as that is the case, I tested with content in 3 of the 4 fields, which gives me 4 combinations:
combo 1 - "name", "email", "subject"
combo 2 - "name", "email", "message"
combo 3 - "name", "subject", "message"
combo 4 - "email", "subject", "message"
The results were as follows:
combo 1 - "Thank you for your message."
combo 2 - "Thank you for your message."
combo 3 - "Message failed."
combo 4 - "Thank you for your message."
Here is the html code I am using:
<form action="contact.php" method="post">
<input type="hidden" name="page" value="contact" />
<input type="hidden" name="req" value="submit" />
Your Name: <input type="text" name="name" />
<br/>
Your Email: <input type="text" name="email" />
<br/>
Subject: <input type="text" name="subject" size="69" />
<br/>
Message:
<br/>
<textarea cols="63" rows="8" name="message"></textarea>
</br>
<input type="submit" value="Send" />
<input type="reset" value="Clear" />
</form>
And here is contact.php:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
$mail_to = 'email#my_website.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '."\n";
$body_message .= $field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for your message.');
window.location = './contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = './contact.html';
</script>
<?php
}
?>
Is there anything wrong with the code? I don't understand why it is doing this.
Great answer from Anton but I wanted to dig a little bit more into this and expand his answer.
If you want to make sure someone sent the email, require it hardcoded with php (and html5 would be cool too, but this can be avoid so you still need php). I also deleted the javascript bit as it felt irrelevant there. This is what I would try and since it's short I'd do it all in the same page. There are many subtle changes:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
//If all required fields are filled
if (!empty($field_name)&&!empty($field_email)&&!empty($field_message))
{
$mail_to = 'email#my_website.com';
$subject = 'Message from a site visitor: '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '."\n";
$body_message .= $field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
echo "Thank you for your message.";
}
//If not all required fields are filled display form again.
else
{ ?>
Please fill all the required fields.<br>
<form action="" method="post">
<input type="hidden" name="page" value="contact" />
<input type="hidden" name="req" value="submit" />
<?php //The php bits are to retrieve the valid fields ?>
Your Name*: <input required type="text" value="<?php echo $field_name; ?>" name="name"/>
<br/>
Your Email*: <input required type="email" value="<?php echo $field_email; ?>" name="email" />
<br/>
Subject: <input type="text" name="subject" value="<?php echo $field_subject; ?>" size="69" />
<br/>
Message*:
<br/>
<textarea required cols="63" rows="8" name="message"><?php echo $field_message; ?></textarea>
</br>
<input type="submit" value="Send" />
<input type="reset" value="Clear" />
</form>
<?php
}
?>
Otherwise, if you don't care about who sends it, you could just put something like this
$field_name = $_POST['name'];
$field_email = $_POST['email'];
if (empty($field_email)) $field_email="anonymous#MYPAGE.com";
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
Last thing, I hope this is just a pseudo code as it doesn't comply almost any html rule. You need < html > tags, < body > etc...
Just tested and worked great (;
The problem is probably this:
Quote from http://php.net/manual/en/function.mail.php
When sending mail, the mail must contain a From header. This can be
set with the additional_headers parameter, or a default can be set in
php.ini.
Failing to do this will result in an error message similar to Warning:
mail(): "sendmail_from" not set in php.ini or custom "From:" header
missing. The From header sets also Return-Path under Windows.
In other words, if you do not have a header containing a proper "From: adress#email.com" an error will occur.

Categories