在Javascript中调用Php函数

在Javascript中调用Php函数

本文介绍了在Javascript中调用Php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究几天,似乎找不到能让我做我需要做的事。

I have been researching for days now and cant seem to find anything that allows me what I need to do.

我想做的是从Javascript调用另一个文件中的Php函数。
我的联系表单上有一个验证码,还有一个Javascript文件,用于检查表单并在用户错过任何内容时提醒用户。我的问题是我无法检查验证码。

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>

这是我调用Javascript和Php的链接

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');

这是我点击href时调用的Javascript。它检查名称,电子邮件和消息,然后在必要时在另一个文本字段上显示错误消息。

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.

问题是我不能在不使用某些Php的情况下检查验证码。

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);
        }

这是我在另一个检查验证码是否有效的文件中的Php脚本,如果是,请执行电子邮件。它是在链接的onClick事件上调用的。

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.

所以...有人可以告诉我如何在Javascript中调用支票吗?这将允许我检查真/假值并确定是否显示有关验证码的警告信息。

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.

如果没有,是否还有其他方法可以做我需要做的事情?在Php中显示警告可能吗?
如果解决方案是使用像Ajax这样的其他东西,请发布所需的代码,我也研究了Ajax和其他一些东西,但似乎无法使它工作。

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.

推荐答案

需要知道这一点:

Javascript(除非指定)是客户端 语言,意味着它在客户端的浏览器上运行后,页面已经编译并从服务器发送。这意味着代码中没有PHP / ASP,只有HTML& Javascript。

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是服务器端语言,只能在服务器端运行。

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

所以要回答你的问题,不,你不能直接获得Javascript来调用PHP(服务器端)函数,但你可以发布到PHP页面,然后它将执行你喜欢的任何函数。

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.

这篇关于在Javascript中调用Php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:26