Link to home
Start Free TrialLog in
Avatar of mewebs
mewebs

asked on

PHP form not providing success Thank You message

I have a form that has everything working except the $success Thank you message.

*** problem 1
The form has a honeypot field that if it is filled out, a different message shows, but
does not send the form. The success message shows correctly on send.php,
but the message is still sent.
** I do not want the message to be sent, I want it to die on the same page contact.html
in the success DIV

*** problem 2
The success message does show up on the send.php page and it sends the message.
** I want the success message to be on the same page contact.html

Is there a way to add the success to show on the same contact.html page in the
success DIV  with the send.php?
or does it need to be done with the js on the send.js page?

I would prefer the php incase bots disable javascript.

All I am trying to achieve is a simple php form that highlights the
required input boxes in red border and use the honeypot to help reduce spam.


Thanks


Here is the code:

contact.html
<!DOCTYPE HTML>
<html>
<head>
<title>Hello</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript" src="send.js"></script>

<style type="text/css">
label{float:left;width:150px;margin:2% 0;}
input,textarea{width:200px;border:1px solid #ccc;font:16px/26px sans;color:#222;margin:2% 0;}
.required{display:none;color:red;}
</style>



</head>
<body>
<p></p>
<!--form-->
<div id="success"></div>
<div id="required"></div>
<form id="myform" name="form" action="send.php" method="post">
<div class="required">* red fields are required</div>
<div><label>Name</label><input id="name" name="name" class="req" type="text" onblur="toUpper(this.value);" /></div>
<div><label>Email</label><input id="email" name="email" type="email" /></div>
<div><label>Phone</label><input id="phone" name="phone" type="tel" class="req" /></div>
<div><label>Confirm</label><input id="confirm_email" name="confirm_email" type="email" class="req2" /></div>
<div><label>Message</label><textarea id="message" name="message" type="text"></textarea></div>
<div><input type="submit" id="submit" /></div>
</form>
</body>
</html>

Open in new window


send.php
<?php
// Clean up the input values
foreach($_POST as $key => $value) {
  if(ini_get('magic_quotes_gpc'))
    $_POST[$key] = stripslashes($_POST[$key]);
 
  $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$confirm = $_POST["confirm_email"];
$phone = $_POST["phone"];
$message = $_POST["message"];

// Send the email *********** enter your email address and message info *******************
$to = "email@myemail.com"; 
$subject = "Website message: $name";
$message = "From:\n$name\n\nEmail:\n$email\n\nPhone:\n$phone\n\nMessage:\n$message";
$headers = "From: $email";

mail($to, $subject, $message, $headers);


if(!empty($_POST["confirm_email"])) {
    $response = array(
        "success" => true,
        "content" => "<span class='success'><li>Thank you!</li></span>"
    );
    die(json_encode($response));
}
// Die with a success message
$response = array(
    "success" => true,
    "content" => "<span class='success'><li>Thank you! Your message has been sent.</li></span>"
);
die(json_encode($response));

?>

Open in new window


send.js
/*--validation--*/
$(function() {
            function validateform() {
                var valid = true;
                $(".req").css("border","1px solid green");
                $(".req").each(function() {
                    if($(this).val() == "" ||  $(this).val().replace(/\s/g, '').length == 0) {
                        $(this).css("border","1px solid red");$(".required").css("display","block");
                        valid = false;
                    }
                });
                return valid;
                }
            
                $("#submit").click(function() {
                $('#myform').submit(validateform);
                    $('$name').submit();
                });
            
        });

Open in new window

Avatar of Jagadishwor Dulal
Jagadishwor Dulal
Flag of Nepal image

You don't have performed any ajax post method to submit your form just using form validation you can not send message from the same page. And see your code which only print the message in the die condition

if(!empty($_POST["confirm_email"])) {
    $response = array(
        "success" => true,
        "content" => "Thank you!"
    );
    die(json_encode($response));
}

Open in new window

Avatar of mewebs
mewebs

ASKER

do you know the best way to fix it?
I would suggest if you are not going my ajax.

remove or comment line no 31 i.e die(json_encode($response));

My solution

if(!empty($_POST["confirm_email"])) {
    $msg ="hank you! Your message has been sent.";
//remove the rest lines
}
$url ="http://yourdesiredlocation&msg=$msg";
header("location:$url");exit;



create an html page and put the below string between body tag

<span class='success'><li><?php echo $_REQUEST['msg'];?></li></span>


This would avoid duplicate post of your value and will display your message and even you would get common message page where you could display your any messages

remove the below line
// Die with a success message
$response = array(
    "success" => true,
    "content" => "<span class='success'><li>Thank you! Your message has been sent.</li></span>"
);
die(json_encode($response));
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mewebs

ASKER

@Ray

Thanks

I will look into that. It is confusing at the moment, but I know it has to be good.
You always provide the best.

I hate to ask, but can you look at my existing and help me with that?

I will look at the other, but I do not know if my Brain can take it at the moment.
It's fried.
Avatar of mewebs

ASKER

Oh yeah.

I know, my CSS will hide the honeypot, I was just leaving it showing for now.

thanks
Avatar of mewebs

ASKER

I fixed it to send to a thanks/spam page successfully using the honeypot.

Still trying to get it to echo a message on the same page rather than going to another page.

send.php
<?php
// Clean up the input values
foreach($_POST as $key => $value) {
  if(ini_get('magic_quotes_gpc'))
    $_POST[$key] = stripslashes($_POST[$key]);
 
  $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

if(!empty($_POST["confirm_email"])) {
header("location:spam.php");exit;
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$confirm = $_POST["confirm_email"];
$phone = $_POST["phone"];
$message = $_POST["message"];

// Send the email *********** enter your email address and message info *******************
$to = "myemail@myemail.com"; 
$subject = "Website message: $name";
$message = "From:\n$name\n\nEmail:\n$email\n\nPhone:\n$phone\n\nMessage:\n$message";
$headers = "From: $email";

mail($to, $subject, $message, $headers);

header("location:thanks.php");exit;

?>

Open in new window

Avatar of mewebs

ASKER

It looks like I am going to have to do this with the Javascript File.
Does not look like I can do it with PHP alone.

Any thoughts on the send.js markup?
Is you want to display it on same page just refresh the page with same url and with one more parameter which will be checked if present than to display the message.

I don't think so its needed .just to display the message
Avatar of mewebs

ASKER

@insoftservice

I'm not following you, do you have an example?
replace header("location:thanks.php");exit;

with header("location:send.php?msg=THX");exit;

in send.php page

if(isset($_REQUEST['msg']) && $_REQUEST['ms']=='THX' )
{
  echo "Thank you";
}
else
{
 mail($to, $subject, $message, $headers);
}
Avatar of mewebs

ASKER

getting webpage error = Redirect loop

This is the code

send.php
<?php
// Clean up the input values
foreach($_POST as $key => $value) {
  if(ini_get('magic_quotes_gpc'))
    $_POST[$key] = stripslashes($_POST[$key]);
 
  $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Honeypot don't send
if(!empty($_POST["confirm_email"])) {
header("location:spam.php");exit;
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$confirm = $_POST["confirm_email"];
$phone = $_POST["phone"];
$message = $_POST["message"];

// Send the email *********** enter your email address and message info *******************
$to = "myemail@myemail.com"; 
$subject = "Website message: $name";
$message = "From:\n$name\n\nEmail:\n$email\n\nPhone:\n$phone\n\nMessage:\n$message";
$headers = "From: $email";

mail($to, $subject, $message, $headers);

header("location:send.php?msg=THX");exit;

if(isset($_REQUEST['msg']) && $_REQUEST['ms']=='THX' )
{
  echo "Thank you";
}
else
{
 mail($to, $subject, $message, $headers);
}


?>

Open in new window

I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".