Jump to content

Ajax Form


artstarved

Recommended Posts

Hey there,

I was using this CSS 3 and jQuery form tutorial to build a form for my website.

http://youhack.me/2010/07/22/create-a-fancy-contact-form-with-css-3-and-jquery/

(the PHP is near the bottom)

 

It uses Ajax and Php to do the backend of the form, but in the tutorial there is not a $to. The form doesn't work because it has nowhere to go.

Can someone please help me modify this code 'so I can include a send to email address:

 

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['web'];
$body = $_POST['text'];

if (!empty($name) & !empty($email) && !empty($body)) {
    $body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
    $send = mail($email, 'Contact Form Submission', $body, "From: {$email}");
    if ($send) {
        echo 'true'; //if everything is ok,always return true , else ajax submission won't work
    }

}

?>

Link to comment
https://forums.phpfreaks.com/topic/219937-ajax-form/
Share on other sites

I'm sorry, that is not very clear. Basically there is now way to define a recipient for this email once it builds. That way when the script is executing the email does not actually send to anywhere(any specified email address).

 

Any idea of how I might define a recipient and get this email to send?

Link to comment
https://forums.phpfreaks.com/topic/219937-ajax-form/#findComment-1140278
Share on other sites

I'm certainly no AJAX expert, but if you're sending it to the same address all the time, you should be able to replace the $email variable in the mail() function call with your email address, or define $email as your address rather than assigning the value of $_POST['email'] to it.

Link to comment
https://forums.phpfreaks.com/topic/219937-ajax-form/#findComment-1140342
Share on other sites

Thanks for the quick reply. I am understanding what you are saying, as it is right now the code sends the email back to address that you input.

 

I have tried adding:

 

<?php
$to = $_POST['[email protected]'];
$name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['web'];
$body = $_POST['text'];

if (!empty($name) & !empty($email) && !empty($body)) {
    $body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
    $send = mail($email, 'Contact Form Submission', $body, "From: {$email}");
    if ($send) {
        echo 'true'; //if everything is ok,always return true , else ajax submission won't work
    }

}

?>

 

But this doesn't fix the problem. It still behaves the same way.

Then I tried this:

 

<?php
$to = "[email protected]";
$name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['web'];
$body = $_POST['text'];

if (!empty($name) & !empty($email) && !empty($body)) {
    $body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
    $send = mail($email, 'Contact Form Submission', $body, "From: {$email}");
    if ($send) {
        echo 'true'; //if everything is ok,always return true , else ajax submission won't work
    }

}

?>

 

But this doesn't fix the problem. It still behaves the same way.

Lastly I tried what you just suggested:

 

<?php

$name = $_POST['name'];
$email = $_POST['[email protected]'];
$web = $_POST['web'];
$body = $_POST['text'];

if (!empty($name) & !empty($email) && !empty($body)) {
    $body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
    $send = mail($email, 'Contact Form Submission', $body, "From: {$email}");
    if ($send) {
        echo 'true'; //if everything is ok,always return true , else ajax submission won't work
    }

}

?>

 

This just breaks the function all together and it won't send the email all or complete the script.

 

 

I feel like this is a no-brainer for someone who actually knows and uses PHP regularly.

Any other ideas?

Link to comment
https://forums.phpfreaks.com/topic/219937-ajax-form/#findComment-1140361
Share on other sites

The first parameter in the mail() function is the address to which the email will be sent. With that in mind,

$to = '[email protected]';
//[sNIP]
$send = mail($to, 'Contact Form Submission', $body, "From: {$email}");

 

Also, so you have the email address the user entered in the form inserted in the body of the email:

$body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}\n\nEmail Address: $email";

Link to comment
https://forums.phpfreaks.com/topic/219937-ajax-form/#findComment-1140368
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.