从阿贾克斯传递值的字preSS到PHP [英] Pass values from ajax to php in wordpress

查看:87
本文介绍了从阿贾克斯传递值的字preSS到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过传递值构成我的JS网页在Word preSS PHP页面发送邮件时,我做了,直到AJAX部分

Am trying to send mail by passing the values form my JS page to PHP page in Wordpress, I made until the AJAX section

jQuery.ajax({
    type: "POST",
    url:"contact.php",
    data: "frm_adrs=" + frm_adrs + "&to_adrs=" + to_adrs + "&sub=" + sub + "&number=" + number  +"&zip=" + zip + "&message=" + message,
    success: function(data) {
        //...
    }

在PHP的页面

if (isset($_GET["frm_adrs"]))
{
    $frm_adrs = $_GET["frm_adrs"];
    $to_adrs = $_GET["to_adrs"];

现在的问题是,AJAX是无法找到contact.php..我没有开发任何插件,因此而不是ajaxurl我需要添加一个静态的URL发送一封电子邮件。

Now the problem is the AJAX is not able to find the "contact.php" .. I am not developing any plugin, hence instead of ajaxurl I need to add a static url to send an email..

在此先感谢

推荐答案

首先,您指定了相对目录在你的jQuery code文件contact.php。这意味着你必须从一个URL,说明同一个目录contact.php的预期位置执行code。例如,在下面的网址执行你的code将有各自的作用;

Firstly, you have specified a relative directory to the file "contact.php" in your jQuery code. This means you must be executing the code from a URL stating the same directory as the anticipated location of contact.php. For instance, executing your code on the following URLs would have the respective effect;

/wordpress/index.php  => /wordpress/contact.php
/wordpress/contact    => /wordpress/contact/contact.php

所以,你需要确认你的 contact.php 文件位于同一目录下生成请求的文件中。

So you need to verify that your contact.php file is located within the same directory as the file generating the request.

正如评论指出宰,你是通过jQuery AJAX的POST方法发送数据,但你的PHP脚本预期(监听)GET方法。这将是问题,因为你的后台脚本将不能跨preT的数据会被发送给它。

As pointed out by Jai in a comment, you are sending data via jQuery AJAX in the POST method, but your php script is anticipating (listening for) the GET method. This will be problematic as your backend script will not interpret the data you are sending to it.

如果要发送的数据作为POST请求,那么你应该使用 $ _ POST 来检索它,否则,如果你发送的数据作为一个GET请求,使用 $ _ GET 进行检索。您可以通过使用检索数据的比较模糊的方法 $ _ REQUEST ,但是这通常不是做事情的最好方法。

If you are sending the data as a POST request, then you should use $_POST to retrieve it, otherwise if you're sending the data as a GET request, use $_GET to retrieve it. You can use a more ambiguous method of retrieving the data by using $_REQUEST, however this is not usually the best way of doing things.

您可能需要使用EN codeURIComponent使用非字母数字字符的某些字段(例如,您的信息变量),这将确保数据正确传输您的前端和后端code之间。

You may want to use encodeURIComponent for certain fields using non-alphanumeric characters (for instance, your message variable) this will ensure the data is transmitted correctly between your front and back end code.

此外,你可能要检查出OWASP十大名单脚本很容易受到CSRF攻击,并且可以作为邮件中继。检查它 rel="nofollow">

Furthermore, you might want to check out the OWASP top 10 list as your script is vulnerable to CSRF attacks, and can be used as an email relay. Check it here

最后,它是用在需要的BOT过滤/用户验证没有previous表单数据的形式某种形式的CAPTCHA验证普遍的做法。用你的脚本作为中继此prevents机器人发出恶意或垃圾邮件。

Lastly, it is common practice to use some form of CAPTCHA verification on data forms requiring no previous form of bot filtering / user validation. This prevents bots using your script as a relay to send out malicious or spam emails.

这篇关于从阿贾克斯传递值的字preSS到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆