Contact form not working - php

i can't make this contact form work, not sure what it is, will much appreciate your help. here is the code:
<?php
//Process Contact
if (isset ($_POST['send'])) {
//Variables
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
//Check all the inputs
if ($name!='' && $email!='' && $message!='') {
// then Html
$contenido = '<html><body>';
$contenido .= '<h2>Contact from</h2>';
$contenido .= '<p>Sent: '. date("D M Y").'</p>';
$contenido .= '<p>Name: <strong>'.$name.'</strong>';
$contenido .= '<p>Email: <strong>'.$email.'</strong>';
$contenido .= '<p>Message: <strong>'.$message.'</strong>';
$contenido .= '<hr />';
$contenido .= '</body></html>';
// If the forms are full, it shows the message
mail ("mj#marijoing.com", "Mother-Well", $contenido, "From: $email\nContent-Type:text/html; charset=iso-8859-1\nContent-Transfer-Encoding: 8bit");
$flag='MessageSuccess';
$mensaje='<div class="MessageSuccess">Your message has been sent, we will contact you shortly .<br/><strong>Thank you!</strong> </div>';
} else {
//If there's a from to fill...
$flag='err';
$mensaje='<div class="MessageError">All the information in the entry form are required. Please, try again</div>';
}
}
?>
And the HTML:
<? echo $mensaje; /*Status form */ ?>
<? if ($flag!='MessageSuccess') { ?>
<form action="contact.php" method="post">
<input <? if (isset ($flag) && $_POST['name']=='') { echo 'class="MessageError"';}?> type="text" value="<? echo $_POST['name'];?>" maxlength="40" /><br />
<input <? if (isset ($flag) && $_POST['email']=='') { echo 'class="MessageError"';} ?> type="text" value="<? echo $_POST['email'];?>" maxlength="40" /><br />
<textarea <? if (isset ($flag) && $_POST['message']=='') { echo 'class="MessageError"';} ?> name="message" rows="4"><? echo $_POST['message'];?></textarea><br />
<input type="submit" value="CONTACT US" name="send" />
</form>
<? } ?>
Hope somebody can help me, thanks in advance :)

This is by no means the only possible error, but you do
<? if ($flag!='MessageSuccess') { ?>
<form action="contact.php" method="post">
<input <? if (isset ($flag)
In other words, you are assuming the $flag variable exists in the outer loop, then test for its existence in the inner loop. That makes no sense.
Further I would recommend using
<?php
instead of
<?
at all times. Not sure if that makes a difference, but it's certainly more conventional. See https://softwareengineering.stackexchange.com/a/151694 for a description of short tags, and when they are OK to use (did you enable them in your build?)

Related

Why won't my contact form send?

I have used this contact form before, I just changed out the information. For some reason now though it won't send. I gone over the html and php but I can't see the error. The only difference is that now I'm using godaddy, but other than that I'm not seeing what the error is. HELP!
HTML:
<div id="MainForm">
<form method="post" action="ContactForm-Handler.php" name="Contact Form" target="_parent" id="ContactForm" title="Contact Form">
<h1>Contact Form</h1>
<p>Please fill out information below.</p>
<p>
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" /><br /><br />
<label for="Email">Email: </label>
<input type="text" name="Email" id="Email" /><br /><br />
<label for="QuestionType">Regarding: </label>
<select name="QuestionType" id="QuestionType">
<option value="Products">Products</option>
<option value="Pricing">Pricing</option>
<option value="Terms">Terms</option>
<option value="Other Information">Other</option>
</select>
<br /><br />
<label for="MessageBox">Message: </label>
<Br /><br />
<textarea name="MessageBox" cols="50" rows="10">Enter Question/Message Here</textarea>
</p>
<p><input name="Send" type="submit" id="Send" onmouseup="ThankYou.html" value="Submit" />
</p>
</form>
</div>
PHP:
<?php
$errors = '';
$myemail = 'myemail#gmail.com';
$name = $_POST['Name'];
$email = $_POST['Email'];
$message = $_POST['MessageBox'];
$question = $_POST['QuestionType'];
if(!empty($_POST['Products'])) {
foreach($_POST['Products'] as $products) {
echo $check;
}
}
if(!empty($_POST['Pricing'])) {
foreach($_POST['Pricing'] as $pricing) {
echo $check;
}
}
if(!empty($_POST['Terms'])) {
foreach($_POST['Terms'] as $terms) {
echo $check;
}
}
if(!empty($_POST['Other Information'])) {
foreach($_POST['Other Information'] as $other) {
echo $check;
}
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact Form Submission";
$email_body = "$name needs some information regarding $question \n \n".
"$message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: ThankYou.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>qp Contact Form</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
if(!empty($_POST['Products'])) {
foreach($_POST['Products'] as $products) {
echo $check;
}
}
if(!empty($_POST['Pricing'])) {
foreach($_POST['Pricing'] as $pricing) {
echo $check;
}
}
if(!empty($_POST['Terms'])) {
foreach($_POST['Terms'] as $terms) {
echo $check;
}
}
if(!empty($_POST['Other Information'])) {
foreach($_POST['Other Information'] as $other) {
echo $check;
}
}
Remove this part of your code because here you're checking with the value of the <option> tag.
You should use name of your <select> box instead.
like this :
//we use name attributes. not value attributes. otherwise $_POST will have Undefined Index error.
if(!empty($_POST['QuestionType'])){
foreach($_POST['QuestionType'] as $QuestionType){
echo $check;
}
}
And yeah, use <?php error_reporting(E_ALL); ?> at the top of your php page to enable php to show you errors.

Echo other content after form validation and submission

How to clear page content and show other content after I validate and submit a form?
<?php
if(isset($_POST['Confirm'])) {
$to = "email#email.com";
$error = 0;
$first_name = $_POST['first_name'];
//Validation things
if(trim($first_name) == '') {$error = 1; $first_namerr = 1;}
$msg ="
First Name:$first_name
-------------------------
";
$sub ="Contact";
$from = "From: Support form";
#mail($to, $sub, $msg, $from);
//How to clear actual content and echo other ?
}
}
?>
<form name="contact" id="contact" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<label for="first_name" class="inner_text"><?php if ($error != 0){ if ($first_namerr == 1) {print("<font style='color: Red;'>");} }?>First Name<?php if ($error != 0){if ($first_namerr == 1) {print("</font>");}} ?></label>
<input id="first_name" name="first_name" size="30" type="text" value="<? echo $first_name; ?>" /><?php if ($error != 0){ if ($first_namerr == 1) {print('<img src="images/error.gif">');} }?>
<input type="submit" id="Confirm" name="Confirm" value="Confirm" />
</form>
I'm beginner in PHP, so please be explicit if you want to give an answer!
You could use an if/else statement to control the content being displayed - e.g:
<?php
if (isset($_POST['Confirm'])) {
// Your mail code
?>
Thank you, your message has been sent! <!-- This content is shown after form submission -->
<?php } else { ?>
<!-- Display your email form -->
<?php }; ?>

Make errors show on top of a form

How do I make error show on top of form so that if $user->success == true, it wont show my form then. Removing that last else would help, but then form shows after success. One way is to redirect that. Maybe tehre
if (isset($_POST["submit"]))
{
if ($_POST["formid"] == $_SESSION["formid"])
{
$_SESSION["formid"] = '';
$User->signin($_POST['username'], $_POST['password']);
}
else
$User->CheckUser();
if ($User->success == true) {
include ('in.php');
}
if ($User->error)
echo "<p>" . $User->error . "</p>";
else
echo 'Don\'t process form';
$_SESSION["formid"] = md5(rand(0,10000000));
} else {
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
Username:
<input id="username" name="username" type="text" /><br />
Password:
<input id="password" name="password" type="password" /><br />
<input type="hidden" name="formid" value="<?php echo $_SESSION["formid"]; ?>" />
<input type="submit" name="submit" />
<br />
Register
</form>
<?php }?>
Perhaps the simplest approach is to just create a variable $show_form to use to determine whether form is to be shown,
$show_form = true;
if(isset($_POST['submit'])) {
// do your form processing here.
// If you decide everything is good and you don't want to show the form,
// just add this line:
$show_form = false;
} // don't use else here
if (true === $show_form) {
?>
<form>...</form>
<?
}
?>
add this code before your form tag
<?php if (isset($User->error) AND $User->error)?>
<p><?php echo $User->error?></p>
<?php?>

PHP contact form isn't working when first installed

I just installed a new PHP contact form but it isn't working. I already tried to fix it but I can't figure out how to fix it. Can somebody take a look at it and give me a fix?
The PHP code:
$to = 'email#hotmail.com';
$subject = 'Contact';
$contact_submitted = 'Your message has been sent.';
function email_is_valid($email) {
return preg_match('/^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$email);
}
if (!email_is_valid($to)) {
echo '<p style="color: red;">You must set-up a valid (to) email address before this contact page will work.</p>';
}
if (isset($_POST['contact_submitted'])) {
$return = "\r";
$youremail = trim(htmlspecialchars($_POST['your_email']));
$yourname = stripslashes(strip_tags($_POST['your_name']));
$yourmessage = stripslashes(strip_tags($_POST['your_message']));
$contact_name = "Name: ".$yourname;
$message_text = "Message: ".$yourmessage;
$user_answer = trim(htmlspecialchars($_POST['user_answer']));
$answer = trim(htmlspecialchars($_POST['answer']));
$message = $contact_name . $return . $message_text;
$headers = "From: ".$youremail;
if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yourmessage != "" && substr(md5($user_answer),5,10) === $answer) {
mail($to,$subject,$message,$headers);
$yourname = '';
$youremail = '';
$yourmessage = '';
echo '<p style="color: blue;">'.$contact_submitted.'</p>';
}
else echo '<p style="color: red;">Please enter your name, a valid email address, your message and the answer to the simple maths question before sending your message.</p>';
}
$number_1 = rand(1, 9);
$number_2 = rand(1, 9);
$answer = substr(md5($number_1+$number_2),5,10);
?>
<form id="contact" action="contact.php" method="post">
<div class="form_settings">
<p><span>Name</span><input class="contact" type="text" name="your_name" value="<?php echo $yourname; ?>" /></p>
<p><span>Email Address</span><input class="contact" type="text" name="your_email" value="<?php echo $youremail; ?>" /></p>
<p><span>Message</span><textarea class="contact textarea" rows="5" cols="50" name="your_message"><?php echo $yourmessage; ?></textarea></p>
<p style="line-height: 1.7em;">To help prevent spam, please enter the answer to this question:</p>
<p><span><?php echo $number_1; ?> + <?php echo $number_2; ?> = ?</span><input type="text" name="user_answer" /><input type="hidden" name="answer" value="<?php echo $answer; ?>" /></p>
<p style="padding-top: 15px"><span> </span><input class="submit" type="submit" name="contact_submitted" value="send" /></p>
</div>
</form>`
Can somebody help me? The error I get: http://i50.tinypic.com/2hfpe7m.jpg.
The fact that PHP commands are showing in your page clearly indicate that PHP is either not installed, or not properly configure. Make sure that it is up and running, and that your file has a .php extension.
Also your source code is missing the opening <?php. It might only be missing from your question, but make sure it is present in your file.
You're missing the opening PHP tag.
Simply add:
<?php
At the top of the script.

POST checkbox value to email?

I have a simple example of check box's which remember what has been selected after the form has been submitted and there is an error.
That part works great... but, I would like to post the resultant 'checked' boxes to my forms mail function.
What I have now only reports if its checked or unchecked I would prefer to have the checked box's 'value' without the unchecked box even registering.
<?php
$CB_1 = 'unchecked';
$CB_2 = 'unchecked';
$CB_3 = 'unchecked';
$CB_4 = 'unchecked';
$CB_5 = 'unchecked';
if (isset($_POST['submit'])) {
if (isset($_POST['CB_1'])) {$CB_1 = $_POST['CB_1'];
if ($CB_1 == 'item_01') {$CB_1 = 'checked';}
}
if (isset($_POST['CB_2'])) {$CB_2 = $_POST['CB_2'];
if ($CB_2 == 'item_02') {$CB_2 = 'checked';}
}
if (isset($_POST['CB_3'])) {$CB_3 = $_POST['CB_3'];
if ($CB_3 == 'item_03') {$CB_3 = 'checked';}
}
if (isset($_POST['CB_4'])) {$CB_4 = $_POST['CB_4'];
if ($CB_4 == 'item_04') {$CB_4 = 'checked';}
}
if (isset($_POST['CB_5'])) {$CB_5 = $_POST['CB_5'];
if ($CB_5 == 'item_05') {$CB_5 = 'checked';}
}
}
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
$error .= "E-mail address not valid.";
}
} else {
$error .= "E-mail address is required.";
}
if (empty($error)) {
$from = 'From: '. #TEST .' <'. $email .'>';
$to = "someone#company.com";
$subject = "CHECKBOX TEST";
$content = "
checkbox selections:
check box 01: $CB_1
check box 02: $CB_2
check box 03: $CB_3
check box 04: $CB_4
check box 05: $CB_5
";
$success = mail($to,$subject,$content,$from);
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<?php if (!empty($error)) echo $error ?>
<br><br><br>
e-mail: <input type="text" name="email" value="<?php if (isset ($_POST {'email'})) { echo $_POST['email']; } ?>" />
<P>
<input type="checkbox" name="CB_1" value="item_01" <?PHP echo $CB_1; ?> /> Item 01
<input type="checkbox" name="CB_2" value="item_02" <?PHP echo $CB_2; ?> /> Item 02
<input type="checkbox" name="CB_3" value="item_03" <?PHP echo $CB_3; ?> /> Item 03
<input type="checkbox" name="CB_4" value="item_04" <?PHP echo $CB_4; ?> /> Item 04
<input type="checkbox" name="CB_5" value="item_05" <?PHP echo $CB_5; ?> /> Item 05
<P>
<input type="submit" name="submit" value="Submit"></input>
</form>
I know this may not necessarily answer the question but a way you could make your code more efficiant is do this.
for($i=1; $i<=5;$i++)
{
if(isset($_POST['submit'])&& isset($_POST['CB_'.$i]) && $CB_.$i=='item_0'.$i)
{
$CB_.$i = $_POST['CB_'.$i];
$CB_.$i = 'checked';
}
}
If you fix it that way it may also make it easier for us to debug.
okay, I found a way to do what I wanted but I'm fairly sure it's not the way someone who has real experience with PHP would do it. Any suggestions?
redelman431, thanks for your suggestion but it was really too advanced for my skill level.
<?php
if(isset($_POST['item_01'])) {$item_01 = 'Item 01';}
if(isset($_POST['item_02'])) {$item_02 = 'Item 02';}
if(isset($_POST['item_03'])) {$item_03 = 'Item 03';}
if(isset($_POST['item_04'])) {$item_04 = 'Item 04';}
if(isset($_POST['item_05'])) {$item_05 = 'Item 05';}
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
$error .= "E-mail address not valid.";
}
} else {
$error .= "E-mail address is required.";
}
if (empty($error)) {
$from = 'From: '. #TEST .' <'. $email .'>';
$to = "someone#company.com";
$subject = "CHECKBOX TEST";
$content = "
checkbox selections:
checked box's: $item_01 $item_02 $item_03 $item_04 $item_05
";
$success = mail($to,$subject,$content,$from);
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<span style="color: red;"><?php if (!empty($error)) echo $error ?></span>
<br><br><br>
e-mail: <input type="text" name="email" value="<?php if (isset ($_POST {'email'})) { echo $_POST['email']; } ?>" />
<P>
<input type="checkbox" name="item_01" value="Item 01"
<?php if(isset($_POST['item_01'])) { echo 'checked'; } ?> /> Item 01
<input type="checkbox" name="item_02" value="Item 02"
<?php if(isset($_POST['item_02'])) { echo 'checked'; } ?> /> Item 02
<input type="checkbox" name="item_03" value="Item 03"
<?php if(isset($_POST['item_03'])) { echo 'checked'; } ?> /> Item 03
<input type="checkbox" name="item_04" value="Item 04"
<?php if(isset($_POST['item_04'])) { echo 'checked'; } ?> /> Item 04
<input type="checkbox" name="item_05" value="Item 05"
<?php if(isset($_POST['item_05'])) { echo 'checked'; } ?> /> Item 05
<P>
<input type="submit" name="submit" value="Submit"></input>
</form>
I found what I was looking for I suppose in the way of an array which I think 'redelman431' was trying to get me to do but it didn't make any sense to me at the time.
I then stumbled onto another array by 'David BĂ©langer' that for some reason made more sense to me so I used it into my check box's and it works fabulously.
the new problem is that I cant get the check box's that have been checked to remember that if there is an error elsewhere on the page which is a disaster as my final form has a ton of them.
Any ideas?
<?php
# Default Vars
$_group_01 = '';
if(isset($group_01) === TRUE){
# Is Array ?
if(is_array($group_01) === TRUE){
# Count
$c = count($group_01);
# Loop
for($i=0; $i < $c; $i++){
$_group_01.= (isset($group_01[$i]) === TRUE ? $group_01[$i] : '').($i == ($c-1) ? '' : ($i == $c-2 ? ' and ' : ', '));
}
}
}
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
$error .= "E-mail address not valid.";
}
} else {
$error .= "E-mail address is required.";
}
if (empty($error)) {
$from = 'From: '. TEST .' <'. $email .'>';
$to = "someone#company.com";
$subject = "CHECKBOX TEST";
$content = "
checkbox selections:
Items Checked: $_group_01
";
$success = mail($to,$subject,$content,$from);
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<span style="color: red;"><?php if (!empty($error)) echo $error ?></span>
<br><br><br>
e-mail: <input type="text" name="email" value="<?php if (isset ($_POST {'email'})) { echo $_POST['email']; } ?>" />
<P>
<input type="checkbox" name="group_01[]" value="Item 01"
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 01
<input type="checkbox" name="group_01[]" value="Item 02"
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 02
<input type="checkbox" name="group_01[]" value="Item 03"
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 03
<input type="checkbox" name="group_01[]" value="Item 04"
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 04
<input type="checkbox" name="group_01[]" value="Item 05"
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 05
<P>
<input type="submit" name="submit" value="Submit"></input>
</form>

Categories