Sorry, I am very new to AJAX, and I while there are tons of examples out there, it is hard for me to understand. If you could please help me out with my specific example I'd much appreciate it. The AJAX syntax is just very strange to me. Anyway, I am validating a contact form in javascript. If it every field has values in it, then it is to call a php function included in the page to send the email. Could someone please show me how to do this and EXPLAIN it? I am very confused by how AJAX works. Server/client differences? Sorry. Thank you. Here is my code (that fails not using AJAX):
<?php
require_once("mail.php");
?>
<script language='Javascript' type='text/javascript'>
function validation(){
var name = document.getElementById('name').value;
var email = document.getElementById('email').value;
var phone = document.getElementById('phone').value;
var subject = document.getElementById('subject').value;
var message = document.getElementById('message').value;
if (name == '') {
alert('Please enter your name.');
location.reload(false);
}
else {
if (email == '') {
alert('Please enter your email.');
location.reload(false);
}
else {
if (phone == '') {
alert('Please enter your phone number.');
location.reload(false);
}
else {
if (subject == '') {
alert('Please enter a subject.');
location.reload(false);
}
else {
if (message == '') {
alert('Please enter a message.');
location.reload(false);
}
else {
var send = confirm('Name: ' + name + '\r\nEmail: ' + email + '\r\nPhone: ' + phone + 'Subject: ' + subject + '\r\nMessage:\r\n' + message + '\r\nPress [Ok] to send or [Cancel] to return to the contact page.');
if (send) {
window.location='';
}
else {
location.reload(false);
}
}
}
}
}
}
}
</script>
I know the javascript works up to the part where I need to invoke the php sendmail() function. Thanks for the help and support!
-
2try jQuery sendmail plugin...Dejan Marjanović– Dejan Marjanović2012年08月25日 16:06:44 +00:00Commented Aug 25, 2012 at 16:06
-
What do you mean by "fails not using Ajax"? And please show us your ajax efforts with which we should help youBergi– Bergi2012年08月25日 16:06:56 +00:00Commented Aug 25, 2012 at 16:06
-
I have no efforts, AJAX is confusing to me, I was asking for help.eatonphil– eatonphil2012年08月25日 16:07:22 +00:00Commented Aug 25, 2012 at 16:07
1 Answer 1
Javascript is executed on client (browser), PHP on server-side. So you cannot call server-side functions from javascript; you have to send your client data (email, phone, etc...) with an HTTP get/post to the server (with AJAX, FORMS or whatever you wnat), then when your server-side PHP script receive the data you can prepare mail to be sent and call the sendmail() function.