I’m working on sending an email from my site using a JSON. I’m doing it this way because simply sending it through the <form> control seems to bypass a little DIY “Are you human” validation I’m doing:
<script type="text/javascript">
$("#contact-submit").click(function() {
if ($("#contact-captcha").text != "green") {
$("#contact-captcha").text = "Incorrect";
$("#contact-captcha").css("border-color", "#7c0707");
}
else
{
sendMail();
}
});
function sendMail() {
$.ajax({
type: "POST",
url: 'includes/contact.php',
data: { to: 'me@this.com', from: $('#contact-email').text(), subject: $('#contact-subject').val(), message: $('#contact-message').val() }
});
}
</script>
The problem is now I can’t figure out how to read the JSON data on contact.php.
I’ve seen tutorials that define the JSON in the php code and then use json_decode() to read it which is no good as I can’t be defining the JSON there.
Can anyone provide any guidance as to how to read the JSON here? I thought of using a querystring but can’t figure out how to do that here.
Any help will be greatly appreciated.
Thanks in advance!
When using
You’ll get the data in
$_POST['to'],$_POST['from'], etc.A better way (still not a JSON method), would be to use something like this:
and have
from,subject, andmessagebe thenameattributes of the input fields.If you DO want to send JSON (I don’t see any reason to do it), you can do this:
and send it using
data: { json: json }, and finally doto get the associative array back.