Possible Duplicate:
Reference: Why does the PHP code in my Javascript not work?
I have been researching for days now and cant seem to find anything that allows me what I need to do.
What I am trying to do, is call a Php function in another file from Javascript. I have a captcha on my contact form, and a Javascript file that checks the form and alerts the user if they have missed anything. My problem is I cant check the captcha.
<a href ="#!/Email_Check" class="button1" onclick="$(this).closest('form').submit()"onmouseover="" style="cursor: pointer;"><span></span><strong>Send</strong></a>
That is my link that calls the Javascript and the Php
var nameStr = safe_string(document.getElementById('cf_name').value);
var emailStr = safe_string(document.getElementById('cf_email').value);
var messageStr = safe_string(document.getElementById('cf_message').value);
if(nameStr == "")
document.getElementById('cf_nameCheck').value = "*Enter your name*";
else
document.getElementById('cf_nameCheck').value = "";
if(emailStr == "" || emailStr.indexOf("@") == -1 || emailStr.indexOf(".") == -1)
document.getElementById('cf_emailCheck').value = "*Enter a valid email*";
else
document.getElementById('cf_emailCheck').value = "";
if(messageStr == "")
document.getElementById('cf_messageCheck').value = "*Enter your message*";
else
document.getElementById('cf_messageCheck').value = "";
document.getElementById('cf_name').value = nameStr;
document.getElementById('cf_email').value = emailStr;
document.getElementById('cf_message').value = messageStr;
if(document.getElementById('cf_nameCheck').value == "")
if(document.getElementById('cf_emailCheck').value == "")
if(document.getElementById('cf_messageCheck').value == "")
open_page('#Email_Succesful');
That is my Javascript which is called when the href is clicked. It checks the name,email and message and then displays an error message on another text field if neccesary.
The issue is I cannot check the captcha without using some Php.
include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
$securimage = new Securimage();
if ($securimage->check($_POST['captcha_code']))
{
$mail_status = mail($mail_to, $subject, $body_message, $headers);
}
That is my Php script in another file that checks if the captcha is valid, and if so, performs the email. It is called on the onClick event of the link.
So... can anybody tell me how I would be able to call the check in the Javascript? That would allow me to check a true/false value and determine whether to show a warning message or not about the captcha.
If not, is there any other way I can do what I need to do? Show the warning in the Php maybe? If the solution is to use some other thing like Ajax, please post the code required as well, ive looked into Ajax and a few other things as well but cant seem to get it working either.