Forum Moderators: coopster

Message Too Old, No Replies

Process PHP form in the the same page

Process PHP form in the the same page

         

fast4u

7:01 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



Hello,

Well I ran into the problem. Maybe somebody can help me with.

I have a form that goes like this. this file is called (mail.php) it is included in the (right.inc) which is included in (index.php)

<<<<<mail.php>>>>>>


<form action="FormToEmail.php" method="post">
<table width="77" border="0" cellspacing="0" cellpadding="0" bgcolor="#ececec">
<tr>
<td width="77">name:
<input type="text" size="10" name="name"></td></tr>
<tr>
<td>email:
<input type="text" size="10" name="email"></td></tr>
<tr><td>message:
<textarea name="comments" rows="6" cols="10"></textarea>
</td></tr>
<tr><td height="24"><div align="right">
<input type="submit" value="Send">
</div></td>
</tr>
</table>
</form>

then the process code which is in the FormToEmail.php

<<<<<<<FormToEmail.php>>>>>>>>>


<?php

$my_email = "myname@example.com";

$continue = "/";

$errors = array();

if($_SERVER['REQUEST_METHOD'] == "POST"){$form_input = $_POST;}elseif($_SERVER['REQUEST_METHOD'] == "GET"){$form_input = $_GET;}else{exit;}

function recursive_array_check(&$element_value)
{

if(!is_array($element_value)){$element_value = ltrim($element_value);}
else
{

foreach($element_value as $key => $value){$element_value[$key] = recursive_array_check($value);}

}

return $element_value;

}

recursive_array_check($form_input);

if(!(isset($_SERVER['HTTP_REFERER']) &&!empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}

function recursive_array_check_blank($element_value)
{

global $set;

if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}

}

}

recursive_array_check_blank($form_input);

if(!$set){$errors[] = "You cannot send a blank form";}

function recursive_array_check2(&$element_value)
{

if(!is_array($element_value)){$element_value = strip_tags($element_value);}
else
{

foreach($element_value as $key => $value){$element_value[$key] = recursive_array_check2($value);}

}

return $element_value;

}

recursive_array_check2($form_input);

if(isset($form_input['name']) &&!empty($form_input['name']))
{

if(preg_match("`[\r\n]`",$form_input['name'])){$errors[] = "You have submitted an invalid new line character";}

if(preg_match("/[^a-z' -]/i",stripslashes($form_input['name']))){$errors[] = "You have submitted an invalid character in the name field";}

}

if(isset($form_input['email']) &&!empty($form_input['email']))
{

if(preg_match("`[\r\n]`",$form_input['email'])){$errors[] = "You have submitted an invalid new line character";}

if(!preg_match('/^([a-z][a-z0-9_.-\/\%]*@[^\s\"\)\?<>]+\.[a-z]{2,6})$/i',$form_input['email'])){$errors[] = "Email address is invalid";}

}

if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}

function build_message($request_input){if(!isset($message_output)){$message_output = "";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!is_numeric($key)){$message_output .= "\n\n".$key.": ".build_message($value);}else{$message_output .= "\n\n".build_message($value);}}}return $message_output;}

$message = build_message($form_input);

$message = $message . "\n\n-- \nThank you";

$message = stripslashes($message);

$subject = "FormToEmail Comments";

$headers = "From: " . $form_input['email'] . "\n" . "Return-Path: " . $form_input['email'] . "\n" . "Reply-To: " . $form_input['email'] . "\n";

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

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
<title>Form To Email PHP script</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#ffffff" text="#000000">

<div>
<center>
<b>Thank you <?php print stripslashes($form_input['name']);?></b>
<br>
</center>
</div>

</body>
</html>

Now my question is I would like to process everything on the page, without it going to another page. Meaning refreshing the page and instead of the form appearing a message "thank you".

Is there a way to do that.

Thank you,
-Togrul

[edited by: coopster at 1:10 pm (utc) on Jan. 22, 2007]
[edit reason] generalized email and url [/edit]

cmarshall

7:08 pm on Jan 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's what AJAX is good for.

You would need to pur some JavaScript code in there to process and submit the form. You would have another PHP file on the server that took care of sending the mail, and would respond with (probably, but there is no rule) text that would replace the form. You would use the innerHTML property to replace the form with the result.

Several AJAX sites do this. I guess it's OK, but it has one SERIOUS, HUGE problem: When you hit the Back button in a regular form, you go back to the form you set up. This is useful if you are addressing multiple people, or if there is an error.

With AJAX, you don't get this.

fast4u

7:29 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



not quite familiar with AJAX.

Well so there is no way, I can make it run in mail.php?

scriptmasterdel

8:03 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



That's what AJAX is good for.

Indeed, this seems like the only feasible solution!

However, if it is the reloading of the page that you do not want then why not process the form into another window? You can do this by putting ...

<form target="_blank">

... this on the form element of the document.

This will load the window externally and then if you wish you could make the window close once the e-mail has been sent. This may be insecure and not exactly what you are looking for but it's an idea!

Good luck!

Del

willybfriendly

8:45 pm on Jan 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now my question is I would like to process everything on the page, without it going to another page. Meaning refreshing the page and instead of the form appearing a message "thank you".

Simply set a hidden form field as a flag. ($ds - "Do Something")

Set the action to come right back to this page (<form action="http://thisSite/ThisPage.php" method="post">

Then check the flag to determine what action to take

if($ds=="")
doForm();
else if ($ds=="proceed")
doThanks();

This opens all kinds of flexibility.

$ds = "";//initialize var
if($_POST[ds]=="proceed")//check post data against known good values
$ds = $_POST[ds];//assign value to var

Then use a switch statement to use various functions and produce the html output

switch ($ds)
{
case "proceed":
doThanks();
break;
case "errors":
doErrors();
break;
default:
doForm();
}

WBF

quixote

10:27 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



To simplify willyb's advice:

Do a php include in your mail.php page to include "FormToEmail.php".

Make your Form tag point to the page "index.php" that is including "mail.php".

Modify FormToEmail.php to pick up on your Post data with $_POST['fieldname'].

I usually code in some if/else condtionals so the code is not activated unless a string has a certain value (or a value at all). Do this inside FormToEmail.php. A good suggestion from willyb.

Add an echo statement to mail.php so it has a place to display your Thank You or Oops messages.

cmarshall

4:22 am on Jan 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just FYI.

This is how I do all my contact forms. I don't use AJAX. The only thing that AJAX buys you is a smoother experience, where the window doesn't refresh.

The techniques shown you in this thread will work. I use a hidden input with a token that is used by the script to switch to a send branch. If the token is not there, I display the form.