0

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.

Community
  • 1
  • 1
Scott Modra
  • 29
  • 1
  • 3

4 Answers4

8

Need to know this:

Javascript (unless specified) is a "client-side" language, meaning that it runs on the client's browser AFTER page has been compiled and sent from the server. This means that there is no PHP/ASP in the code, only HTML & Javascript.

PHP, ASP, JSP are server-side languages which only run on the server-side.

So to answer your question, no you can't directly get the Javascript to call a PHP (server-side) function, but you can post to a PHP page which will then in turn carry out whatever function you like.

Afsar
  • 3,104
  • 2
  • 25
  • 35
4

Try this (Ajax Request to a php document)

Niklas
  • 23,674
  • 33
  • 131
  • 170
0

Technology exists to abstract away the client-server border. If you have ever used the Java RMI, you will be right at home with one of those:

dualed
  • 10,262
  • 1
  • 26
  • 29
0

To call any php function from javascript, you need to send an Ajax request which in turn can always return the computed value you are expecting from the function.

SHANK
  • 2,978
  • 23
  • 31