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.
Related
I have a script and it runs very well, but I want know how to echo an error div if recaptcha isn't confirmed. In this script if recaptcha wasn't confirmed the page will reload and nothing will be send to my mail, but I don't know how to display an error that tells the user: "You must verificate that you aren't a robot".
Can you help me?
CODE:
<?php
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"../support/style/contactform.css\">\n";
$emailpattern="^[^# ]+#[^# ]+\.[^# \.]+$";
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<form method="post" id="contactformall">
<p>Nome</p>
<input type="text" name="name" class="contactformimput"/>
<p>Email (obbligatorio)</p>
<input type="text" name="email" class="contactformimput"/>
<p>Numero</p>
<input type="text" name="number" class="contactformimput"/>
<p>Messaggio (obbligatorio)</p>
<input type="text" name="message" onkeyup="adjust_textarea(this)" class="contactformimput" id="contactformtext">
<div class="g-recaptcha" data-sitekey="____key____"></div>
<div id="divcontactbutton">
<input type="reset" name="send" value="Resetta" class="button" id="resetmessage"/>
<input type="submit" name="send" value="Invia Messaggio" class="button" id="sendmessage"/>
</div>
</form>';
exit;
}
$responsejson=file_get_contents("google.com/recaptcha/api/…);
$response = json_decode($responsejson);
if($response->success==false) { echo "<div class=\"emailerror\" id=\"emailnoninviata\"><div><span>•</span> Email non inviata</div></div>";
**/////THIS part DON'T RUN**
}else
{
if (isset($_POST['send'])) {
if(!ereg($emailpattern,$_POST['email'])) {
$emailerror = true;
echo "<div class=\"emailerror\"><div><span>6</span> Email non valida</div></div>";
} if ($_POST['message'] == "") {
$emailerror = true;
echo "<div class=\"emailerror\"><div><span>6</span> Inserisci un messaggio</div></div>";
} elseif ($_POST['message'] != "" and ereg($emailpattern,$_POST['email'])){
$emailerror = false;
};
if ($emailerror == true) {
echo '<form method="post">
<p>Nome</p>
<input type="text" name="name" class="contactformimput"/>
<p>Email*</p>
<input type="text" name="email" class="contactformimput"/>
<p>Numero</p>
<input type="text" name="number" class="contactformimput"/>
<p>Messaggio*</p>
<input type="text" name="message" onkeyup="adjust_textarea(this)" class="contactformimput" id="contactformtext">
<div class="g-recaptcha" data-sitekey="___KEY___"></div>
<div id="divcontactbutton">
<input type="reset" name="send" value="Resetta" class="button" id="resetmessage"/>
<input type="submit" name="send" value="Invia Messaggio" class="button" id="sendmessage"/>
</div>
</form>';
}
if (isset($_POST['send']) and $emailerror == false) {
$to = "mail#gmail.com";
$subject = "B&B";
$user_name = 'Name: ' . $_POST['name'] . "\n";
$user_email = 'Email: ' . $_POST['email'] . "\n";
$user_ip = 'IP: ' . $_SERVER['REMOTE_ADDR'] . "\n";
$user_message = 'Message: ' . $_POST['message'];
$message = $user_name . $user_email . $user_ip . $user_message;
$success = mail($to, $subject, $message);
echo "<div class=\"emailerror\" id=\"emailinviata\"><div> <span>5</span> Email inviata correttamente</div></div>";}
}
}
?>
You are mixing javascript code with php code. Javascripts uses '.' to access class vars/functions, php uses '->'.
Please read this page in order to use Recaptcha with php: https://developers.google.com/recaptcha/old/docs/php
The recaptcha documentation shows that this API request should return JSON. You will need to decode the JSON response to refer to its contents. So do this after you use file_get_contents() to get the response:
$response = json_decode($response);
and then you should be able to check for success with a little adjustment to your syntax.
if($response->success==false)
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 }; ?>
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?)
This question already has an answer here:
How to validate form in php
(1 answer)
Closed 8 years ago.
Anyone please help me on how to validate a form? I have the code, it is working ok..but I found that after I hit the submit button & php shows the errors, there is an input field, asking the user to answer the math Ques..when I enter the answer, then hit submit button again, it just says mail successfully sent, but other fields are still empty. Any ideas how to fix it. Also, the php value in the answerbox input tags, is not saving the text in box, it disappears. by the way, I got some help from other users, i'm a noob in php, so if you don't mind please explain.
Thanks for your time!
<form name="contact" action="formtest.php" method="post">
<label for="YourName">Your Name:</label>
<input type="text" name="name" class="required" value="<?= isset($_POST['name']) ? $_POST['name'] : '' ?>" />
<label for="YourEmail">Your Email:</label>
<input type="text" name="email" class="required" value="<?= isset($_POST['email']) ? $_POST['email'] : '' ?>"/>
<label for="Subject">Subject:</label>
<input type="text" name="subject" class="required" value="<?= isset($_POST['subject']) ? $_POST['subject'] : '' ?>" />
<label for="YourMessage">Your Message:</label>
<textarea name="message" class="required"><?= isset($_POST['message']) ? $_POST['message'] : '' ?></textarea>
<p class="c3">10 + 5 =<input type="text" name="answerbox" id="answerbox" "<?= isset($_POST['answerbox']) ? $_POST['answerbox'] : '' ?>"/></p>
<fieldset>
<input type="submit" name="submit" id="submit" value="Send" class="required"/>
<input type="reset" id="reset" value="Reset"/>
</fieldset>
<?php
if(isset($_POST['submit'])){
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$subject = trim($_POST["subject"]);
$message = trim($_POST["message"]);
$answerbox = trim($_POST["answerbox"]);
if(empty($_POST['name'])){
print "<div class='formerrors'><li>Please type your name.</li></div>";
}
else {
if (ctype_alpha($name) === false) {
print "<div class='formerrors'><li>Your name only should be in letters!</li></div>";
}
}
if(empty($_POST['email'])){
print "<div class='formerrors'><li>You've forgot to type your email address.</li></div>";
} else{
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
print "<div class='formerrors'><li>Your email is not valid, please check.</li></div>";
}
}
if(empty($_POST['subject'])){
print "<div class='formerrors'><li>Please type a subject.</li></div>";
}
if(empty($_POST['message'])){
print "<div class='formerrors'><li>You've forgot to type your message.</li></div>";
}
if(empty($_POST['answerbox'])){
print "<div class='formerrors'><li>Please answer the math question.</li></div>";
}
else {
if($answerbox != 15){
print"<div class='formerrors'><li>Answer is not correct.</li></div>";
}
else{
$headers = 'From: '.$email. "\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail('me#mymail.me',$subject,$message,$headers);
print "<div class='formerrors'><li>mail succesuffully sent</li></div>";
}
}
}
?>
</form>
Try placing your php code above the html as it is php setting the values of the variables, and you are calling them before they are set.
It is best practice to have php code at the top of the file, and then html below it, or even have the php on a separate file, but include it at the top.
Also I find it better to create an array of errors, and then if the array count is 0, then perform the action (send email) else call the error of arrays within the html, not the php. Then you can choose where to display it on the page, and style it accordingly.
Change your form action to
<?php $PHP_SELF; ?>
So it has to be
<form name="contact" action="<?php $PHP_SELF; ?>" method="post">
For the other issue you must place your code above the form.
Link to website: http://www.leonardpfautsch.com/contact.php
How do I make my contact form validated only using PHP? I want to be able to have error messages directly under the text field that has an error. For each text field, I do not want multiple errors to show up at once. If you submit the form with nothing in the fields, you see that under name and email two errors show up for each. I want the errors to show up only once due to some type of specifications. Right now I think I am on the right track. However, the code below does not have the email being sent. I am very new to PHP. If anybody could help me, I would really appreciate it.
<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))){
$errors = array($name_error_1, $name_error_2, $email_error_1, $email_error_2, $subject_error, $message_error);
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if ($name === '') {
$name_error_1 = '<div style="color:red;"> Name is a required field! </div>';
}
if ($email === '') {
$email_error_1 = '<div style="color:red;"> Email is a required field! </div>';
}
if ($subject === '') {
$subject_error = '<div style="color:red;"> Subject is a required field! </div>';
}
if ($message === '') {
$message_error = '<div style="color:red;"> Message is a required field! </div>';
}
if (isset($email) && (filter_var($email, FILTER_VALIDATE_EMAIL) === false)){
$email_error_2 = '<div style="color:red;"> The email address must be real! </div>';
}
if (ctype_alpha($name) === false) {
$name_error_2 = '<div style="color:red;"> Your name must only contain letters! </div>';
}
/*Main way that mail works*/
if (empty($errors) === true) {
/*Where_mail_goes_to, Subject, Body_text, Who_email_is_from*/
mail('email_address', $subject, "From " . $name . "\r\r" . $message, 'From: ' . $email);
/*Shows up in the URL if the message has been sent*/
header('Location: contact.php?sent');
exit();
}
} //end of main if
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>" >
<span class="label">Name</span><br/>
<?php if (isset($name_error_1)) { echo $name_error_1; } ?>
<?php if (isset($name_error_2)) { echo $name_error_2; } ?>
<input type="text" class="textfield" name="name" size="50" maxlength="50" <?php if (isset($_POST['name']) === true) { echo 'value="', strip_tags($_POST['name']), '"'; } ?> > <br/>
<span class="label">Email</span><br/>
<?php if (isset($email_error_1)) { echo $email_error_1; } ?>
<?php if (isset($email_error_2)) { echo $email_error_2; } ?>
<input type="text" class="textfield" name="email" size="50" maxlength="50" <?php if (isset($_POST['email']) === true) { echo 'value="', strip_tags($_POST['email']), '"'; } ?> > <br/>
<span class="label">Subject</span><br/>
<?php if (isset($subject_error)) { echo $subject_error; } ?>
<input type="text" class="textfield" name="subject" size="50" maxlength="50" <?php if (isset($_POST['subject']) === true) { echo 'value="', strip_tags($_POST['subject']), '"'; } ?> > <br/>
<span class="label">Message</span><br/>
<?php if (isset($message_error)) { echo $message_error; } ?>
<textarea rows="5" cols="50" name="message" id="textarea" maxlength="500"><?php if (isset($_POST['message']) === true){ echo $_POST['message'];}?></textarea><br/>
<input type="submit" value="Send" id="submit" name="action">
</form>
You could create an array of errors for each field and display just the first error added to it.
<?php
$email_errors = array();
if ($email == '')
{
$email_errors[] = 'First error';
}
if (more_email_checks($email) == false)
{
$email_errors[] = 'Second error';
}
?>
...
<span class="label">Email</span><br />
<?php echo array_shift($email_errors); ?>
To know whether to send e-mails or not, you could do something like this:
$errors_found = 0;
if (check_email($email) == false)
{
$email_error = 'Error message';
$errors_found++;
}
...
if ($errors_found == 0)
{
mail(...);
}
You can do it by using the elseif check
<span class="label">Email</span><br/>
<?php if (isset($email_error_1))
{
echo $email_error_1;
}
elseif(isset($email_error_2)) {
echo $email_error_2;
} ?>
Also move this line after the last validation check
if (ctype_alpha($name) === false) {
$name_error_2 = '<div style="color:red;"> Your name must only contain letters! </div>';
}
$errors = array($name_error_1, $name_error_2, $email_error_1, $email_error_2, $subject_error, $message_error);
you can by this code for name
<?
$message = "<div style = 'color :red ' /> ;
if (isset(name == '' ) {
echo $message
}
?>
this is name php vaildation but yo can create js
Change your email validation to:
<span class="label">Email</span><br/>
<?php if (isset($email_error_1))
{
echo $email_error_1;
}else if(isset($email_error_2)) {
echo $email_error_2;
} ?>
same if else can be applied to all the fields with multiple validation conditions.
and then move your error array just above the email condition check:
<?php
$errors = array($name_error_1, $name_error_2, $email_error_1, $email_error_2, $subject_error, $message_error);
//and change your mail function as:
$to = 'email_address';
$headers = $headers .= 'From: $name <$email>';
mail($to, $subject, $message, $headers);
?>
Also perform a check on the control, if you have filled the form completely then it should come to the mail function, I mean just check the if condition, in case you have some issue with the condition, try to put an echo inside if statement(which is responsible for sending email), and if that echo statement executes then mail should work.
:)