Link to home
Start Free TrialLog in
Avatar of Refael
RefaelFlag for United States of America

asked on

Prevent page refresh after form submission

Hello Experts,

This is a one page website with a contact form.
Here below are the scripts in their order. I am searching for a away to replace the "header" location after the email has been processed by simply echo a message on the page without refreshing the page. The page is using tabs so I need the page not to refresh otherwise the tab will get closed.

// on the very top of the page

<?php if (($_SERVER["REQUEST_METHOD"] == "POST") && (!empty($_POST["submit"]))) {require('inc/process.php');}?>

Open in new window


// form action

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="contactform" id="contactform">

Open in new window


// that's the ned part of the process.php file where if everything went well it will dreict to the replay.php

$result = mail($to, $subject, $message, $headers);
		if ($result) {
			header('Location: reply.php');			
		} else {
			die ("An error occurred while trying to send an email to $toOwner or please contact the administrator.");

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain 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
Avatar of Refael

ASKER

Hello Marco Gasi
Thank you. That's the only way? It is not possible to handle this inside the process.php file where it sends the header location? I am also afraid doing that because JavaScript can be disabled and that's the right place for hackers to play.

I am guessing that sendmail.php', //change to your script path this where the process.php file is coming in, right?
Yes, that's the only way: php is a server language so you must reload the page to process data if you don't want to use javascript.

About hackers, any form is a good place to play for them, so the security can't be granted just avoiding javascript. About security you have to sanitize any value comes from the client within your php code, javascript or not javascript :)

The sendmail.php must be replaced by the pth to the script which actually does the dirty job, that is the script which sends the email: if you called it process.php use this.
I just realized the the form action is PHP_SELF. First don't use PHP_SELF, just drop action attribute and you'll get the same result. Second, if you want to use Ajax put sendin email codde in another file like sendmail.php or something else.

Anyway, you can avoid javascript if the sending mail code is directly in your page where the form is.
Then you can do something like:
<?php
if (isset($_POST['send']))
{
 //   get values for email here
    ...
$result = mail($to, $subject, $message, $headers);
$response = '';
if ($result) {
	$response = "Message successfully sent!";	
} else {
	$response = "An error occurred while trying to send an email to $toOwner or please contact the administrator.";
}

....
//then the form
// form action
<div id="response" lang="es"><?php echo $response; ?></div>
<form method="post" name="contactform" id="contactform">
	<label for="email">Email</label>
	<input id="email" type="text" placeholder="email" />
	<label for="name">Name</label>
	<input id="name" type="text" placeholder="Name" />
	<textarea id="message" placeholder="Message"></textarea>
	<button id="send" lang="es"><i class="icon-comment"></i> Envía </button>
</form>

Open in new window


Change the style for the response div to display: block; If there is a response it will be prontyed there.
SOLUTION
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
Avatar of Refael

ASKER

Thank you Marco Gasi . I would probably come back once I will try this out. At the moment the project needs to be online yesterday :-) But I understand the concept now.

Ray.... This time you went beyond the scope? could not relate your suggested solution to the problem i was trying to resolve. Yet anyway, always, thank you.