4 Replies - 10295 Views - Last Post: 25 June 2013 - 06:51 PM Rate Topic: -----

#1 deprosun   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 307
  • Joined: 16-November 10

How to execute php code after the form's submit button has clicked

Posted 25 June 2013 - 02:51 PM

I am trying to make a contact form where upon hitting the submit button the information provided by the user goes to the php to verify and send an email to me. I have chosen to put the Php code in the same page. I could use a different page but I think it's inefficient. So, in short, I don't know how to stop php code before the submit button is clicked. Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Tutorial</title>
<link rel="stylesheet" type="text/css" href="mywebsiteCSS.css"/>
<script type="text/javascript" src="jquery-2.0.2.min.js"></script>
<script type="text/javascript" src="dist/jquery.validate.js"></script>
<script type="text/javascript" src="jquery-ui-1.9.2.custom/js/jquery-ui-1.9.2.custom.js"></script>
</head>
<div for="type" class="error" generated="true"></div>
<body>
<form action="" method="post" id="emailform" style="display:none">
<label for="fullname" >*Full Name:</label>
<input type="text" minlength="5" onclick="disableElement()" name="fullname" id="fullname" placeholder="What is your name?" required="required"><br>

<label for="email" >*Email:</label>
<input type="text" name="email" id="email" placeholder="I want to get back to you. What is your email address?" required="required"><br>

<label for="subject" >Subject:</label>
<input type="text" name="subject" id="subject" placeholder="What is it you want to talk about?" required="required"><br>

<label for="message" >*Message:</label>
<textarea rows="4" cols="50" placeholder="Lets talk!" id= "message" name="message" required="required"></textarea>

<input type="submit" id="submit" name="cancel" value="Send Mail">
<input type="button" id="cancel" name="cancel" value="Cancel"></button>

</form>

<a href="#" class="myButton" id="contactme" name="contactme">Contact Me!</a>
<br>
</body>
</html>
<script type="text/javascript">
$(document).ready(function() {
	positionPopup();
   var validator = $("#emailform").validate(
	{
		rules: {
			fullname: {
				required: true,
				minlength: 5
			},
			email: {
				required: true,
				email: true
			},
			message: {
				required: true,
				minlength: 2
			}
		},
		messages: {
			fullname: {
				required: "",
				minlength: ""
			},
			email: "",
			message: ""
		},
		errorElement:"div",
		errorPlacement: function(error, element){
			element.css('border-color', '#FE0102');
			element.after(error);
		}
	 });
	 
	 
	//open popup
	$("#contactme").click(function(){
	$("#emailform").effect("bounce", { times:4 }, 300);
		});
 
//close popup
	$("#cancel").click(function(){
		$("#email").css('border-color','#CCC');
		$("#fullname").css('border-color','#CCC');
		$("#message").css('border-color','#CCC');
		$("#subject").css('border-color','#CCC');
		$("#email").val('');
		$("#fullname").val('');
		$("#subject").val('');
		$("#message").val('');
		$("#emailform").fadeOut(500);
		validator.resetForm();
		});
	});
 
//position the popup at the center of the page
	function positionPopup(){
	$("#emailform").css({
		left: ($(window).width() - $('#emailform').width()) / 2,
		top: ($(window).width() - $('#emailform').width()) / 7,
		position:'absolute'
	});

 
//maintain the popup at center of the page when browser resized
	$(window).bind('resize',positionPopup);
	}
</script>
<?php
$sendername = isset( $_POST['fullname'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['fullname'] ) : "";
$senderemail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$sendermessage = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
$sendersubject = isset( $_POST['subject'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['subject'] ) : "";

if($sendername && $senderemail && $sendersubject && $sendermessage){
echo "Hello, my name is" . $sendername;
?>
<br>
<?php
echo "My email is ".$senderemail;
?>
<br>
<?php
echo "My concern is ".$sendersubject;

?>
<br>
<?php
echo $sendermessage;
}

else if(strlen($sendername) == 0){
echo "Sorry! I am not getting enough information to get back at you. Try again?";
?>
<br>

<?php	
}
//mail("[email protected]",$sendersubject,$sendermessage, $sendersubject);
?>

This post has been edited by Dormilich: 25 June 2013 - 02:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to execute php code after the form's submit button has clicked

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4221
  • View blog
  • Posts: 13,362
  • Joined: 08-June 10

Re: How to execute php code after the form's submit button has clicked

Posted 25 June 2013 - 02:57 PM

View Postdeprosun, on 25 June 2013 - 11:51 PM, said:

So, in short, I don't know how to stop php code before the submit button is clicked.

thatís simple. only execute the PHP if your data are there (isset() is a good help there).


PS. there is no content allowed after the closing </html> tag. you need to put it before that.
Was This Post Helpful? 1
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14578
  • View blog
  • Posts: 58,441
  • Joined: 12-June 08

Re: How to execute php code after the form's submit button has clicked

Posted 25 June 2013 - 03:00 PM

Quote

So, in short, I don't know how to stop php code before the submit button is clicked.

Have you considered this question is answered a few sentences up with:

Quote

I have chosen to put the Php code in the same page. I could use a different page but I think it's inefficient.

.. as a reason why people put the scripts in separate files? ;)
Was This Post Helpful? 2
  • +
  • -

#4 deprosun   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 307
  • Joined: 16-November 10

Re: How to execute php code after the form's submit button has clicked

Posted 25 June 2013 - 04:24 PM

View Postmodi123_1, on 25 June 2013 - 05:00 PM, said:

Quote

So, in short, I don't know how to stop php code before the submit button is clicked.

Have you considered this question is answered a few sentences up with:

Quote

I have chosen to put the Php code in the same page. I could use a different page but I think it's inefficient.

.. as a reason why people put the scripts in separate files? ;)/>


hmm....i see :)

View PostDormilich, on 25 June 2013 - 04:57 PM, said:

View Postdeprosun, on 25 June 2013 - 11:51 PM, said:

So, in short, I don't know how to stop php code before the submit button is clicked.

thatís simple. only execute the PHP if your data are there (isset() is a good help there).


PS. there is no content allowed after the closing </html> tag. you need to put it before that.


worked! thanks :)
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,827
  • Joined: 08-August 08

Re: How to execute php code after the form's submit button has clicked

Posted 25 June 2013 - 06:51 PM

Quote

So, in short, I don't know how to stop php code before the submit button is clicked.

Ugh. Do you not realize that PHP and HTML are processed on different computers and at different times? Mixing the languages in one file doesn't change that fact, but it does obscure it. Try keeping different languages in different files.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1