PDA

View Full Version : Resolved PHP Form response



XManBG
08-24-2011, 06:45 PM
Hello guys,

trying to organize a simple PHP-send-form-input-over-eMail-form, I've found one PHP example (http://www.w3schools.com/php/php_secure_mail.asp) on the w3schools site how to do it. The example is for one whole page to reload and react when something was written into the input fields and submit is pushed.

My question is: how can I change the PHP part to replace the content of one specific DIV only, without reloading the whole page. The same DIV, wherein that PHP form is placed. With other words, I'd like on submit to remove the form and on its place to show only "Thank you", or "Invalid input" with back button to show the form again. All the rest page content, shall remain in place and not be reloaded.

Thanks for your help.

Cheers

JShor
08-24-2011, 08:08 PM
This is accomplished with AJAX. If you're going to use AJAX, I recommend doing it the jQuery way. Here's a quick form I wrote with the code from the w3schools site.



<html>
<body>
<?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);

//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}

if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed

//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("someone@example.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
}


?>
<script type="text/javascript">
function sendForm() {
var dataSend = "?subject=" + $("#subject").val() + "&message=" + $("#message").val() + "&email=" + $("#email").val();

$.post("<?php echo $_SERVER[PHP_SELF]; ?>", dataSend, function(data) {
$("#responseDiv").html(data);
});

</script>

<div id="responseDiv"></div>
<form method='post' action='<?php echo $_SERVER[PHP_SELF]; ?>' onsubmit="sendForm()" id="myForm">
Email: <input name='email' id="email" type='text' /><br />
Subject: <input name='subject' id="subject" type='text' /><br />
Message:<br />
<textarea name='message' id="message" rows='15' cols='40'>
</textarea><br />
<input type='submit' id="submit" name="submit" />
</form>

</body>
</html>

XManBG
08-25-2011, 08:00 PM
Thanks Josh, for your reply. The code you've suggested assumes again, that the <form>-page and the result-page, are the same. That's not what I'd like to use. But I'm also not sure, if my request is possible. I'll try to systematize it:

1) One HTML page named contacts.html is loaded, where one div containing form is ready to be filled out.

<div id="result"><form id="myForm">...something...</form></div>

2) The user enters data into the fields and pushes button Submit.

3) The page contacts.html remains in place.

4) Using PHP (and without loading new page or reloading the same page) the input data shall be checked and sent to an email. Maybe you mean here, that I have to use AJAX instead, but if so, I have not understand how... What I mean here is, that I'd like to call a function on the form action, instead to load a new page (or reload the PHP page).

5) The PHP (or AJAX) shall send one sentence in return "Thank you!" or "Invalid input!", written into the same <div id="result">, where the <form> was before.

That's all.

XManBG
08-25-2011, 10:24 PM
Thanks again. I've found a resolution after studying the $.post of AJAX. Here is how I've done it, assuming the <form> is placed inside a <div id="result">:



function sendForm() {

//.... some if-s for checking field content and return with "false" if not correct ...

$.post("mailform.php", { email : "someones@email.com" , subject : "Important subject" , message : "Some words about your concern..." })
.success(function(){
//...what to do if success;
$("#result").empty().append("Thank you!");
//...
})
.error(function(){
//...and what to do if failure;
$("#result").empty().append("Our apologies, there was an unexpected error. Our server is using Windows. :D");
//...
});

};




mailform.php has only sending purpose:

<?php

$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;

mail("ouremail@email.com", $subject, $message, "From:" . $email);

?>