1

I have a form that is positioned on the page with HTML, if the user completes the form then they are thanked with a PHP message. Because the form is positioned with <form id="formIn"> CSS the PHP text is now in the wrong position, does anyone have an idea how this can be positioned so that the PHP echo text is nest to the form? I have tried to include the code in PHP, i.e.

<div id=\"text\">

But no joy.

Code used so far is:

<?php 
echo "* - All sections must be complete"."<br><br>";

$contact_name = $_POST['contact_name'];
$contact_name_slashes = htmlentities(addslashes($contact_name));
$contact_email = $_POST['contact_email'];
$contact_email_slashes = htmlentities(addslashes($contact_email)); 
$contact_text = $_POST['contact_text'];
$contact_text_slashes = htmlentities(addslashes($contact_text)); 

if (isset ($_POST['contact_name']) && isset ($_POST['contact_email']) && isset ($_POST['contact_text'])) 

{

if (!empty($contact_name) && !empty($contact_email) && !empty($contact_text)){


$to = '';
$subject = "Contact Form Submitted";
$body = $contact_name."\n".$contact_text;
$headers = 'From: '.$contact_email;

if (@mail($to,$subject,$body,$headers))

{
echo "Thank you for contacting us.";
}else{
echo 'Error, please try again later';
}

echo "";
}else{

echo "All sections must be completed.";
}
1
  • 2
    That one smidgen of HTML you included in your question is insufficient. Please post a complete code example. Commented Nov 12, 2013 at 17:06

2 Answers 2

1

A good way for doing this is to create a div for displaying messages in the form page and return back from the php script to the form page including form.html?error=email or form.html?success to the url. Then with javascript you can identify when this happens and display a message or another.

Comment if you need some code examples.

EDIT: Imagine your php script detects that email field is not filled, so it would return to the form webpage with a variable in the url:

<?php
if(!isset($_POST["email"]){
    header("location:yourformfile.html?error=email")
}
?>

Then, in your form webpage you have to add some javascript that catches that variable in the URL and displays a message in the page:
Javascript:

function GetVars(variable){
    var query = window.location.search.substring(1);  //Gets the part after '?'
    data = query.split("=");  //Separates the var from the data
    if (typeof data[0] == 'undefined') {
        return false; // It means there isn't variable in the url and no message has to be shown
    }else{
        if(data[0] != variable){
            return false; // It means there isn't the var you are looking for.
        }else{
            return data[1];  //The function returns the data and we can display a message
        }
    }
}

In this method we have that data[0] is the var name and data[1] is the var data. You should implement the method like this:

if(GetVars("error") == "email"){
    //display message 'email error'
}else if(GetVars("error") == "phone"){
    //dislpay message 'phone error'
}else{
    // Display message 'Success!' or nothing
}

Finally, for displaying the message I would recommend creating a div with HTML and CSS and animate it to appear and disappear with jQuery. Or just displaying an alert alert("Email Error");

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, examples would be great. Is this similar to header(location:set.html)
Here you have your examples @yeha. Any doubts just comment.
0

Just create a <div> before or after your form and put IF SUBMIT condition on it. Like,

<?php
if(isset($_POST['submit']))
{ ?>

<div>
   <!-- Your HTML message  -->
</div>

<?php } ?>

<form>

</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.