Link to home
Start Free TrialLog in
Avatar of tekgrl
tekgrl

asked on

PHP form -- thank you message display on same page

Hi all, I need a very simple PHP contact form (email address only) that displays the thank you message (or form failed) on the same page as the form. In other words, no redirect page.

Extra points if the form validates.

Thank you!!
Avatar of daniel_smith
daniel_smith
Flag of United States of America image

I didnt test this, but just used a sort form I had from another project.

Should get the point tho.
<html>
<head>
<script Language="JavaScript">
<!-- 
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.form.email
	var ccnum=document.form.cc_num
	var ccv=document.form.ccv
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}

	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	
	
	return true
 }
</script>

</head>
<body>
<?
if $_POST['form_Submit']=1
{

	echo "Thank you for submitting your email!";
	eXit();


}
else
{

	echo "<form action=formpage.php metho=post name=form onSubmit=return ValidateForm()>";
	echo "Email Address:<input type=text name=email maxlength=25 />";
	echo "<input type=hidden name=form_submit value=1>";
	echo "<input type=submit value=Submit/>";
	
</form>
}
</body>

Open in new window

Avatar of Kalpan
Please refer attached code...make changes as you needed
<?
// file: index.php

if($_POST['submit']){
 if($_POST['email'] != ""){
   echo $_POST['email'];
   echo "Thank you for contacting us"; 
 }else{
   echo "Error no email entered";
}
}else{ ?>
<html>
<head><title></title></head>
<body>
<form action="index.php" method="post">
<input type="text" name="email" value=""/>
</form>
</body>
</html>
<?}?>

Open in new window

Never rely on JavaScript to do form validation. JavaScript can be bypassed completely, so dangerous (code injection) data can be submitted to your server.
Avatar of tekgrl
tekgrl

ASKER

Neither of these worked. Nothing showed up on the first example and no input button for the second one...
While I have no issues using Javascript, here is one without, much like kalmax's version. But allows the form to display if no email address was submitted.

<?php
 if($_POST['email'] != ""){
   echo $_POST['email'];
   echo "  Thank you for your email"; 
 }else{
 	if($_POST['submitted'] !=""){
    	echo "Error no email entered";
 	}
 	echo "<html><head></head><body>";
	echo "<form action=form_page.php method=post>";
	echo "Email Address:<input type=text name=email />";
	echo "<input type=hidden value=submitted name=submitted>";
	echo "<input type=submit value=Submit>";
	echo "</form></body>";
}
?>

Open in new window

Avatar of tekgrl

ASKER

Hey Daniel, maybe I'm missing something. The form action is "form_page.php". Where is that page? I'm hoping it sends an email notification to someone.
Name that code as a page called form_page.php
So that it submits to itself. You can call it whatever you want. just be sure to fix the action to whatever you named it.
Avatar of tekgrl

ASKER

I see. Yes, that works, but I need the form to send an email to me notifying me that someone has submitted their email via the form. How do I do that?
Short of writing this entire thing for you,
You could add this to the code, i think you can figure that out, its pretty self explanatory.


<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

Open in new window

Avatar of tekgrl

ASKER

I would love for you to write the entire thing for me! Alas...

Here is what I came up with. Not getting the email alert though.


<?php
 // Contact subject  
$subject = 'Somebody signed up';
// Details  
$message= $_POST['email'];  
// Mail of sender  
$mail_from= $_POST['email'];   
// From   
$header="from: me@me.com";  
// Enter your email address  
$to ='me@me.com';  
  
$send_contact=mail($to,$subject,$message,$header);  

if($_POST['email'] != ""){
   echo "Thank you for your email"; 
 }else{
 	if($_POST['submitted'] !=""){
    	echo "Error no email entered";
 	}
 	echo "<html><head></head><body>";
	echo "<form action=footer2.php method=post>";
	echo "Email Address:<input type=text name=email />";
	echo "<input type=hidden value=submitted name=submitted>";
	echo "<input type=submit value=Submit>";
	echo "</form></body>";
}
?>

Open in new window

your not doing the sendmail in the thank you  area.
put it after line 15

I am not sure what your doing with the $mail_from or $send_contact either

use the codefor the send mail i posted
Avatar of tekgrl

ASKER

Here's what I have... Still no email notification.

Also, I don't understand how I'm going to get the submitted email address with this code.
<?php
 if($_POST['email'] != ""){
   echo $_POST['email'];
   echo "  Thank you for your email"; 
 }else{
 	if($_POST['submitted'] !=""){
    	echo "Error no email entered";
 	}
 	echo "<html><head></head><body>";
	echo "<form action=footer2.php method=post>";
	echo "Email Address:<input type=text name=email />";
	echo "<input type=hidden value=submitted name=submitted>";
	echo "<input type=submit value=Submit>";
	echo "</form></body>";

}

$to      = 'me@me.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

Open in new window

your not going to, you have to CALL the email portion after the email is captures in the return $_POST in line 2

move the code for the email after line 2.....
Avatar of tekgrl

ASKER

How do I call the email portion?
as I said, MOVE THE CODE FOR THE EMAIL TO LINE 3
thats it....

I would also suggest a PHP manual to get some basics of PHP
Avatar of tekgrl

ASKER

Okay...I'm really confused. I've attached what I have. It's not working. You could put us both out of our misery and just post a little code. I have better things to do today, and I'm sure you do too.
<?php
 if($_POST['email'] != ""){
 $to = 'me@me.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
   echo $_POST['email'];
   echo "  Thank you for your email"; 
 }else{
 	if($_POST['submitted'] !=""){
    	echo "Error no email entered";
 	}
 	echo "<html><head></head><body>";
	echo "<form action=footer2.php method=post>";
	echo "Email Address:<input type=text name=email />";
	echo "<input type=hidden value=submitted name=submitted>";
	echo "<input type=submit value=Submit>";
	echo "</form></body>";

}
?>

Open in new window

. I copied your code and used it.  Worked like a champ

You DID change the FROM, REPLY TO and TO addresses right? (line 3,6,7)

Are you getting the THANK YOU FOR YOUR EMAIL when you submit?

You need to be more specific about whats NOT working
Avatar of tekgrl

ASKER

That is right. I am NOT getting the email. Yes, I changed those addresses to my own.
Are you getting the THANK YOU FOR YOUR EMAIL?

If not then it sounds like your not submitted to the page correctly. there isnt much more I can you, the code is right on, ive tested it just as you posted with expection of my email addresses and it works correctly.
Avatar of tekgrl

ASKER

Hmmmm. Well I guess that's it for us then. Can somebody else try it and see if it works for them too?
again i ask, are you getting the THANK YOU FOR YOUR EMAIL text when you submit the email address?
You have to help yourself some by answering questions
Avatar of tekgrl

ASKER

Yes, I get that message.
ok so the codes working, your mail part is not

if the code is exactly as you posted above with expection of YOUR email accounts in place instead of the me@me.com then its working.

make a simple PHP page with this in it nothing else
 This will just test the phpmail. be sure to replace the your@email.com with your email address
make the page, load the page and see if you get email


<?
$mail_sent = @mail('your@email.com', 'My Subject', 'test body message');

echo $mail_sent;
?>

Open in new window

Avatar of tekgrl

ASKER

I didn't get the email. What does that mean?
what did the echo display?
Avatar of tekgrl

ASKER

1
1 means the email sent, it responded as TRUE which means it sent successfully.
Avatar of tekgrl

ASKER

Okay, I guess this is my problem, not yours. Checked all my spam filter... no email. Will you please do me a favor and check my attached file once more?
footer5.php
ASKER CERTIFIED SOLUTION
Avatar of daniel_smith
daniel_smith
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
You can add the code for that in get_response.php file. Here is the code:

if(!$result = $conn->query($sql)){
die('There was an error running the query [' . $conn->error . ']');
}
else
{
echo "Thank you! We will contact you soon";
}
}
else
{
echo "Please fill Name and Email";
}
?>

Open in new window


Source: contact form in php