I will start out by saying that I have no experience with PHP at all.. sorry.
However, since I do some asp.net etc I thought I would throw myself into it..

I have come across a problem though.
I've found a mail form using jQuery.

What it's supposed to do when I hit the 'submit' button is this:

function closeForm(){
                $("#messageSent").show("slow");
                 setTimeout('$("#messageSent").hide();$("#contactForm").slideUp("slow")', 2000);

But ever since I tied some PHP to the thing to actually make it work and send mail. It will automatically redirect me to a blank page.. the PHP document address. Instead of calling closeForm.

Alternatively I can do a header() and send it back to the front page, but that's just so far from satisfying...

I'm thinking there's just something basic about how PHP operates that I don't know about..
But let me know if you need to see more of the code behind, and I'll gladly supply it.

Thanks in advance
Maria

Recommended Answers

All 6 Replies

yes, i think this function would have been removed from the submit button's clickor else some other redirection to the form after this function etc.can you post the relevant code here

I sure can! Hope you can look past the danish text in there, otherwise let me know, and I'll translate it to english ;)
The html for the form:

<form method="post" name="myemailform" action="Scripts/form-to-email.php" >
                    <fieldset>
                        <label for="name">Navn *</label>
                        <input type="text" name="name">
                        <label for="email">Email adresse *</label>
                        <input type="text" name="email">
                        <label for="message">Din besked *</label>
                        <textarea id="Message" name="message"></textarea>
                        <input id="sendMail" type="submit" name="submit" value="Send" onClick="closeForm()">
                        <span id="messageSent">Din besked er sendt!</span>
                   </fieldset>
                   </form>

And the php:

<?php

$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];



$email_from = 'admin@xxx.dk';
$email_subject = "Ny besked fra xxx.dk";
$email_body = "Ny besked fra: $name.\n".
	"E-mail: $visitor_email.\n".
    "Besked:\n $message.\n".
    
$to = "admin@xxx.dk";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send email!
mail($to,$email_subject,$email_body,$headers);
?>

The PHP above should be bounded inside the if() like below -

<?php
if(isset($_POST['submit']) && $_POST['submit'] =='send')
{
	$name = $_POST['name'];
	$visitor_email = $_POST['email'];
	$message = $_POST['message'];
	
	$email_from = 'admin@xxx.dk';
	$email_subject = "Ny besked fra xxx.dk";
	$email_body = "Ny besked fra: $name.\n".
		"E-mail: $visitor_email.\n".
		"Besked:\n $message.\n".
		
	$to = "admin@xxx.dk";
	$headers = "From: $email_from \r\n";
	$headers .= "Reply-To: $visitor_email \r\n";
	//Send email!
	mail($to,$email_subject,$email_body,$headers);
}
?>

let me explain the flow too -
once the function closeForm() gets executed, the page gets redirected to the script specified in the action of the form i.e. to the Scripts/form-to-email.php

Is there no way though that I can prevent it from redirecting?

Thanks for pointing out the missing condition.
edit: when adding the if, it wont send mails.. :)

Is there no way though that I can prevent it from redirecting?

Thanks for pointing out the missing condition.
edit: when adding the if, it wont send mails.. :)

I cant see any problem with the above code not getting executed , once the form is posted with the submit button's click, then the value for the submit button will be set, and that's what the line isset() checks.
If not used the that PHP code will get executed on every call to that page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.