Avatar of gwarcher
gwarcher
 asked on

PHP Contact Form with success notification on site rather than on a popup

I put together a very simple PHP form to use with my company's website.  So far it is successful, but I am not sure how to make it so that the success or failure string is called to the body of the website rather than in a seperate blank site.

Thanks for the help as always!
Form:
 
   <form action="contact.php" method="get" target="_blank">   
                                <h3>Please send us your questions and comments.</h3>
                                <div class="style1">Name: </div>
                                <div><input type="text" size="50" maxlenth="65" name="your_name" /></div>
                                <div class="style1">Email:</div>
                                <div><input type="text" size="50" maxlength="650" name="your_email" /></div>
                                <div class="style1">Message:</div>
                                <div><textarea name="message" cols="50" rows="10"></textarea></div>
                                <div><input type="reset" value="[ CLEAR ]" /></div>
                                <div><input type="submit" value="[ SUBMIT ]"  /></div>
                            
                                </form>
 
Contact.php
 
 
<?php
$subject="from ".$_GET['your_name'];
$headers= "From: ".$_GET['your_email']."\n";
 $headers.='Content-type: text/html; charset=iso-8859-1';
mail("info@mycompany.com", $subject,  "
<html>
<head>
 <title>Website Contact Form</title>
</head>
<body>
 
<br>
  ".$_GET['message']."
</body>
</html>" , $headers);
echo ("Your message was successfully sent!");
?>
<script>
	//window.open()
</script>

Open in new window

PHPScripting Languages

Avatar of undefined
Last Comment
iDeej

8/22/2022 - Mon
iDeej

Ok I hope I'm not misunderstanding you but I'm thinking you want the string that says "Your message was successfully sent!" to appear on the same page rather than open a new browser window to display your page.

You could set up you page to test if the from was submitted. If form is submitted display "Your message was successfully sent" at the top of the page and the rest of the page below. If form is not submitted then just display the regular page.

You would need to give you submit button a name, "submit" would do.

If (isset($_POST['submit']) {
  // The submit button was pressed in the form.
} else {
  // The form has not yet been submitted so just display the form as it would usually appear.
}

The action attribute for the form would be the same name of the page the form is on. I don't think you'd need the target attribute.

<form action="contact.php" method="POST">
gwarcher

ASKER
I think I'm following your answer here (very new to php).

To get the output that I want on the page at the if/then statement would I do a print statement?

Sorry, just need a little more clarification.
ASKER CERTIFIED SOLUTION
iDeej

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61