Reputation: 8245
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: '[email protected]', 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!
Upvotes: 0
Views: 143
Reputation: 2291
If using JSON is not a must then you can do it this way.
Use the jquery code that you have used for sending the data to contacts.php and following PHP code.
In Contact.php
if(isset($_POST['to']) && $_POST['to'] != ''){
$filterTO = $_POST['to'];
//filter value and get filtered value back in $filterTO here and use it further
}
You can also use other variables in same way.
Note: It is higly recommended to filter variables before use. I have shown only the part which was problem for you. This is not entire standard process.
Upvotes: 0
Reputation: 3457
In contacts.php
print_r($_POST)
or use Firebug and check in console during submit, you can easily find whether you data is sent properly (or whether it is in JSON.. so on)
(I'm not sure how you are doing your "Human verification" check, but, checking the field value $("#contact-captcha").text != "green" not a right way(Ease to tamper and bypass it since its in form itself). You need to create a hash in server side using a 'random' text and session it (or send to form) and user has to enter the text and send that along as POST to server. And finally verify by hash the user enter text and compare with one you have created it. Ignore this if you're doing it correctly. ^^")
Upvotes: 0
Reputation: 222118
When using
data: { to: '[email protected]', from: $('#contact-email').text(), subject: $('#contact-subject').val(), message: $('#contact-message').val() }
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:
var serialized = $("form").serialize();
serialized.to = "[email protected]";
$.ajax { ... data: serialized ... }
and have from
, subject
, and message
be the name
attributes of the input fields.
From: <input type="text" name="from"/>
If you DO want to send JSON (I don't see any reason to do it), you can do this:
json = JSON.stringify({ to: '[email protected]', from: $('#contact-email').text(), subject: $('#contact-subject').val(), message: $('#contact-message').val() });
and send it using data: { json: json }
, and finally do
json_decode($_POST['json']);
to get the associative array back.
Upvotes: 2