I'm using a contact form for my website, which I validate and then email to myself, the validation is working correctly and it emails me if the user enters all the details correctly first time. However if the user enters incorrect data, then corrects it and hits send again, it won't send an email, below is the form and PHP code I have so far.
HTML code for contact form
<form action="contact.php" method="post">
<fieldset>
<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" required />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" required />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" required></textarea>
<p class="small"><span class="star">*</span> Denotes a required field </p>
<input type="submit" id="send" name="send" value="Send" />
</fieldset>
</form>
PHP code for sending the form
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
{
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
}
$send = $_POST['send'];
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "<br />";
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$message = wordwrap($message, 70, "/r/n");
$headers = "From ". $details['email'];
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
if ($send)
{
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";
}
}
else
{
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
}
EDIT
In order for the code to work on the same page, you need to set the action to action=""
Otherwise, you need to use two pages. One for your form and one for contact.php which is your handler. I suggest you use two pages, but here is a version that will work inside one page.
<?php
if(isset($_POST['send'])) {
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");
}
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "\n";
$send = $_POST['send'];
$email = $_POST['email'];
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
exit;
} // closing brace for if(isset($_POST['send'])) {
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="post">
<fieldset>
<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" ></textarea>
<p class="small"><span class="star">*</span> Denotes a required field </p>
<input type="submit" id="send" name="send" value="Send" />
</fieldset>
</form>
</body>
</html>
Original answer
This line is not properly formatted.
$message = wordwrap($message, 70, "/r/n");
change it to:
$message = wordwrap($message, 70, "\r\n");
You need to use \ instead of /
EDIT
The only way I could get your form to work, is to add a die function.
Try this now:
<?php
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");
}
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "\n";
$send = $_POST['send'];
$email = $_POST['email'];
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
exit;
?>
Related
I am creating an assessment which will send results to the teacher and the student. I can't get the results to be sent to the student, only the teacher. Here is what I have below:
index.php
<form id="form1" name="form1" method="post" action="results.php" onsubmit="return validateForm() ;">
<label>First Name
<input type="text" name="fname" id="fname" tabindex="1" />
</label>
<label>Last Name
<input type="text" name="lname" id="lname" tabindex="2" />
</label>
<label>Title
<input type="text" name="title" id="title" tabindex="3" />
</label>
<br />
<br />
<hr />
<?php
$quiz = new Quiz( $questions );
echo $quiz->renderQuiz();
?>
<br/>
<label>Enter the email addresses you would like to receive your results, separated by semicolons.</label><br/>
<input type="text" name="email_to" tabindex="4" size="80" />
<input name="Submit" type="submit" value="Submit" />
</form>
results.php
$force_email = array( "test#test.com", test1#test.com);
function create_email_string( $force_email, $user_input )
{
if (!empty( $user_input ))
{
$email_string = $user_input . "; " . implode("; " , $force_email);
}
else
{
$email_string = implode("; " , $force_email);
}
return $email_string;
}
$ip = $_SERVER['REMOTE_ADDR']; // employee's Ip address
$time = date("d/m/y : H:i:s", time()); // current timestamp
$email_string = create_email_string( $force_email, $_POST['email_to'] );
$questions_correct = array();
$results_data_page = $_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
$file_start = (strrpos($results_data_page, "/")+1);
$results_data_page = substr_replace($results_data_page, "results_data_page.php?test_name=" . rawurlencode($test_name), $file_start);
$variables = array();
$variables['fname'] = $_POST['fname'];
$variables['lname'] = $_POST['lname'];
$variables['test_name'] = $test_name;
$variables['score'] = $score;
$variables['table'] = create_results_table( $questions, $questions_correct );
$variables['results_data_page'] = $results_data_page;
$subject = $variables['fname'] . " " . $variables['lname'] . " results - " . $test_name;
$headers = "From: test#test.com\n";
$headers .= "Reply-To: test#test.com \n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";
$headers .= "Content-Transfer-Encoding: base64";
$message = getMessage( $variables );
$message = rtrim(chunk_split(base64_encode($message)));
foreach ($force_email as $to) {
mail($to, $subject, $message, $headers);
}
I have tried to create php email form. The form was basically working, but I wanted to add validation functions like I did on 'name'
However, It doesn't work when I empty 'name' and just sent an email.
Any help would be really appreciated.
htmlfile
<form action="php_mini.php" method="post">
Name:
<input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Email:
<input type="text" name="email">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Phone:
<input type="text" name ="phone">
<br><br>
Comment:
<!--<textarea name="comment"></textarea>-->
<!--<input type = "text" name = "comment">-->
<textarea name="comment" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
<input type="submit" value="Submit">
</form>
php
echo '<pre>';
print_r( $_POST );
echo '</pre>';
$headers = 'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'From: Design_customers <customers.com' . " \r\n" .
//'Reply-To: vader#deathstar.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//error msg
$nameErr = $emailErr = $phoneErr = "";
$name = $email = $comment = $phone = "";
//receiver
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
$email = $_POST['email'];
/*$message = $_POST['comment'];*/
$message = 'You got a message from a customer.:
Name: '.$_POST['name'].'
Email: '.$_POST['email'].'
Phone: '.$_POST['phone'].'
Comment: '.$_POST['comment'];
//sender
$from = 'From: Customer';
$subject = 'Customer Inquiry';
mail( $email, $subject, $message, $from );
use if(isset($_POST["name"])) to stop the error. [Notice: Undefined index: name in C........]
better you have validate this form with java Script. i have attached a snipped part of you form with javascript.
<form action="" method="post" onsubmit="return(Validate());" name="myform">
Name:
<input type="text" name="name">
<span class="error">* <div id="name_error" style="color:red;"></div> </span>
<br><br>
</form>
<script type="text/javascript">
<!--
// Form validation code will come here.
function Validate()
{
if( document.myform.name.value == "" )
{
alert( "Please provide your name!" );
name_error.textContent = "Name is Required.!";
document.myform.name.focus() ;
return false;
}
return( true );
}
//-->
</script>
I'm a beginner, and I would like to make a PHP mail form like that :
But it doesn't work, I receive the mail but there is anything in it, and I don't understand why :/
Here is the HTML code for my form :
<form method=POST action=formmail.php >
<input type=hidden name=subject value=formmail>
<p>Pseudo* :<br>
<span class="padding1"><input type="text" name="your-name" value="" size="40" aria-required="true" aria-invalid="false"></span> </p>
<p>Email* :<br>
<span class="your-email padding1"><input type="email" name="your-email" value="" size="40" aria-required="true" aria-invalid="false"></span> </p>
<p>Link of the GIF* :<br>
<span class="link-url padding1"><input type="url" name="link-url" value="" aria-invalid="false"></span> </p>
<p><input type="submit" value="Send"><img class="ajax-loader" src="http://s584101063.onlinehome.fr/wp-content/plugins/contact-form-7/images/ajax-loader.gif" alt="Envoi en cours ..." style="visibility: hidden;"></p>
</div>
</form>
And here is my PHP code :
<?php
$TO = "saintscorporation#gmail.com";
$h = "From: " . $TO;
$message = "";
while (list($key, $val) = each($HTTP_POST_VARS)) {
$message .= "$key : $val\n";
}
mail($TO, $subject, $message, $h);
Header("Location: http://gifmyday.com/index.html");
?>
Adding the headers I put in the following code should help.
<?php
$TO = "saintscorporation#gmail.com";
// The headers have been changed a bit
$h = "From: " . $TO . "\r\n";
$h .= "MIME-Version: 1.0 \r\n" .
"Content-type: text/html charset=iso-8859-1 \r\n";
$message = "";
while (list($key, $val) = each($HTTP_POST_VARS)) {
$message .= "$key : $val\n";
}
mail($TO, $subject, $message, $h);
Header("Location: http://gifmyday.com/index.html");
?>
Basically I have a page which displays to a user only when they're logged in, it has a custom contact form on it. When the form is submitted, their user role should automatically change to "Author".
I literally have no idea where to start with this, if anyone has any ideas or can help me out a little that would be amazing because I'm really struggling with this.
EDIT
This is what I currently have, all it does at the minute is send emails.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = trim(stripslashes($_POST['name']));
$email = trim(stripslashes($_POST['email']));
$subject = trim(stripslashes($_POST['subject']));
$message = trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'email#email.com';
$body = '<html><head></head><body>' .
'<b>Name:</b> ' . $name . '<br><br>' .
'<b>Email:</b> ' . $email . '<br><br>' .
'<b>Nature of Enquiry:</b> ' . $subject . '<br><br>' .
'<b>Message:</b> ' . nl2br($message) .
'</body></html>';
$headers = "From: " . strip_tags($email_from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($email_from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if( isset($_POST['checkbox']) ) {
mail($email_to, $subject, $body, $headers);
// Change user role to "author
echo '<p>You are now an author.</p>';
} else {
echo '<p>It appears you are a spambot, if this is a mistake please try again and check the "I am not a spambot" checkbox.</p>';
}
}
?>
<form method="post" action="#">
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $name; ?>" required>
<label for="email">Email Address</label>
<input type="email" id="email" name="email" value="<?php echo $email; ?>" required>
<label for="subject">Nature of Enquiry</label>
<input type="text" id="subject" name="subject" value="<?php echo $subject; ?>" required>
<label for="checkbox">I am not a spambot</label>
<input type="checkbox" id="checkbox" name="checkbox">
<label for="message">Message</label>
<textarea id="message" name="message" required><?php echo $message; ?></textarea>
<button type="submit">Submit</button>
</form>
This would be a matter of using the class WP_User and its set_role method.
$user_id = 2;
$role_name = 'author';
$user = new WP_User( $user_id );
$user->set_role( $role_name );
I'm using a contact form on my website and I'm trying to validate the user input using PHP. The problem with it is that although it will validate the input alright, if you then enter correct details and hit send, it doesn't do anything. I think this is because it's not doing the second part of the if statement after an error has been entered, but I'm not sure how to fix it. Below is the code I'm using so far.
<?php
error_reporting(E_ALL ^ E_NOTICE);
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
{
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
}
$send = $_POST['send'];
$message = "";
$email = $details['email'];
foreach ($details as $field => $detail)
$message .= ucfirst($field) . ": " . $detail . "\r\n";
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$message = wordwrap($message, 70, "\r\n");
$headers = 'From: ' .$email . "\r\n" .
'Reply-To: ' .$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
if ($send)
{
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";
}
if (!is_numeric($details['number']))
echo "<p class='error'>Please enter a valid telephone number</p>";
}
else
{
mail($to, $subject, $message, $headers);
echo "<p class='success'>Thank you for your message, you will receive a response shortly.</p>";
}
?>
<div id="contactform">
<form action="" method="post">
<fieldset>
<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" required />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" required />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" required></textarea>
<p class="small"><span class="star">*</span> Denotes a required field </p>
<input type="submit" id="send" name="send" value="Send" />
</fieldset>
</form>
I tried changing it to a while loop and just doing the validation until it is valid and then sending the email, however my attempts at this didn't work either.
Could you try change your if ($send) {} block to the following and test?
if ($send) {
$bHasError = false;
foreach ($details as $field => $detail) {
if (empty($detail) && $field!='number') {
echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";
$bHasError = true;
}
}
if (!is_numeric($details['number'])) {
echo "<p class='error'>Please enter a valid telephone number</p>";
$bHasError = true;
}
if (!$bHasError) {
mail($to, $subject, $message, $headers);
echo "<p class='success'>Thank you for your message, you will receive a response shortly.</p>";
}
}