Contact form with check boxes having troubles [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need help with add check boxes to my contact form I have this as 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 my php part of the code
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'example#example.com';
$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 help#helpme.com');
window.location = 'contact.html';
</script>
<?php
}
?>
I need help adding groups of check boxes so I can check one box from each of the groups and it would send an email with a list of what check boxes where picked from each group.

Use this code
<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>
Check boxes: <input type="checkbox" name="abc[]" value="Car">Car<br/>
<input type="checkbox" name="abc[]" value="Bus">Bus<br/>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
// check boxess
$check_box_values = "Check box value: ";
if(isset($_POST['abc'])){
foreach($_POST['abc'] as $value){
$check_box_values .= $value;
$check_box_values .= ', ';
}
}
// end check boxess
$mail_to = 'example#example.com';
$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."\n".$check_box_values;
$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 help#helpme.com');
window.location = 'contact.html';
</script>
<?php
}
?>

Related

Cannot POST PHP file for contact form?

So I have published my website, but am making minor improvements to it as time progresses. Just a note: I am impatient and this is making me want to obliterate myself. Help!
So the question is the title. I have a contact form on my website which uses HTML and PHP.
Here is the form on HTML:
<form action="contact.php" method="post">
Name<br>
<input type="text" name="cf_name"><br>
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 for it:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'amir#arshak.co.uk';
$subject = 'ALERT! Somebody has viewed your website!'.$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('Thanks. I have recieved your message. I may reply to the given e-mail.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Sorry, your message failed to send. Send an email to amir#arshak.co.uk');
window.location = 'contact.html';
</script>
<?php
}
?>
I can't figure out what is wrong with the code!
If the problem is that the email won't send...
You are allowing the from and reply-to addresses to be set to whatever is entered into the form.
A lot of mail servers will only allow an email to be sent where the from address matches the email address used to authenticate with the server.
This could possibly be causing your issue although it's hard to say without much more information.

Contact thank you page

Currently My contact form goes to another page when submitted with an alert that says thankyou for contacting us but, what I want it to do is say thankyou for contact us as a pop up or text on the screen, anywhere to say thank you but just on the same page as the contact form
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = ' dsaas#asadsd.asdasd';
$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_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon#template-help.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>
<form action="contact.php" style="height:auto;" class="form" method="post">
<h1>Your name</h1>
<input type="text" class="form textbox" name="cf_name"><br>
<h1>Your e-mail</h1>
<input type="text" class="form textbox" name="cf_email"><br>
<h1>Message</h1>
<textarea class="form textbox" name="cf_message"></textarea><br>
<input type="submit" value="Send">
I have tried replacing the if statement to
if ($mail_status) { ?>
<div><h1>thank you for submitting the contcat form</h1> </div>
<?php
}
But this still went to another page
You need to validate your data before sending.
if(isset($_POST['submit']){
$field_name = isset($_POST['cf_name'])? $_POST['cf_name']:'';
$field_email = isset($_POST['cf_email'])? $_POST['cf_email']:'';
$field_message = isset($_POST['cf_message'])? $_POST['cf_message']:'';
if(empty($field_name){
$error = 'Field Name is blank';
}elseif(empty($field_email) || filter_var($field_email, FILTER_VALIDATE_EMAIL)){
$error = 'Email is invalid';
}elseif(empty($field_message)){
$error = 'Message is blank';
}else{
$mail_to = ' dsaas#asadsd.asdasd';
$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.');
document.getElementById("myForm").reset();//reset the form
</script>
<?php }else{ ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon#template-help.com');
//document.getElementById("myForm").reset();//Reset the form
</script>
<?php }
}
}
Since you don't want to go to another, you can post in the same page by specifying the action like below or you don't necessarily need to do.
<form action="<?php echo $_SERVER['PHP_SELF'];?>"
style="height:auto;" class="form" id="myForm" method="post">
<h1>Your name</h1>
<input type="text" class="form textbox" name="cf_name"><br>
<h1>Your e-mail</h1>
<input type="text" class="form textbox" name="cf_email"><br>
<h1>Message</h1>
<textarea class="form textbox" name="cf_message"></textarea><br>
<input type="submit" value="Send">
<div class="error">
<?php if(!empty($error)){ echo $error; } ?>
</div>
Hope this may help.

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.

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

I am not getting all info from my contact form.From: no email & body-no email and telephone,but i can get message [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Here is my contact form:
<form id="contact-form" action="contact.php" method="post">
<fieldset>
<label><span class="text-form">Full Name:</span>
<input type="text" name="cf_name">
</label>
<label><span class="text-form">E-mail:</span>
<input type="text" name="cf_email">
</label>
<label><span class="text-form">Telephone:</span>
<input type="text" name="cf_phone">
</label>
<div class="wrapper">
<div class="text-form">Message:</div>
<div class="extra-wrap">
<textarea name="cf_message"></textarea>
</div>
<div class="buttons"><a class="button-2" href="#" onClick="document.getElementById('contact-form').reset()">Clear</a>
<a class="button-2" href="#" onClick="document.getElementById('contact-form').submit()">Send</a></div>
</fieldset>
</form>
</div>
I have changed several items in the code at first i was getting no emails then i was getting email with no content and now it missing the From: email address and in the body of email no email address and no phone number but i do get the message.Im stumped
And here is my contact.php
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_email = $_POST['cf_phone'];
$field_message = $_POST['cf_message'];
$mail_to = '2waylogistics#gmail.com';
$subject = '2waylogistics contact form '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message = 'Email: '.$field_email."\n";
$body_message .= 'phone: '.$field_phone."\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 = 'thank_you.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to info#2waylogistics.com');
window.location = 'contacts.html';
</script>
<?
php
}
?>
There were 3 issues with your code.
1) A missing dot in
$body_message = 'Email: '.$field_email."\n";
^
2) Seperating <? from php (which would have thrown a parse error)
<?
php
}
?>
3) And what j08691 posted in an answer.
Now try:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_phone = $_POST['cf_phone'];
$field_message = $_POST['cf_message'];
$mail_to = '2waylogistics#gmail.com';
$subject = '2waylogistics contact form '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'phone: '.$field_phone."\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 = 'thank_you.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to info#2waylogistics.com');
window.location = 'contacts.html';
</script>
<?php
}
?>
You re-used the same variable for phone and email:
$field_email = $_POST['cf_email'];
$field_email = $_POST['cf_phone'];
Try:
$field_email = $_POST['cf_email'];
$field_phone = $_POST['cf_phone'];

Categories