简体   繁体   English

在Wordpress主题中使用AJAX进行SQL查询

[英]SQL query using AJAX in Wordpress theme

I'm attempting to use AJAX to insert data into a database, but am unable to connect to the .php file on the server end. 我正在尝试使用AJAX将数据插入数据库中,但是无法连接到服务器端的.php文件。 I'm thinking this is due to the fact that this is all run on Wordpress, so my normal url path to the .php file might not be correct. 我在想这是由于所有这些都在Wordpress上运行,因此我正常的.php文件的url路径可能不正确。

Here is my setup: 这是我的设置:

Template file: 模板文件:

$('#newsletter-register').submit(function(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

    ajaxRequest.onreadystatechange = function(){

        if(ajaxRequest.readyState == 4){
            alert(ajaxRequest.responseText);
        }
    }

    ajaxRequest.open("GET", "register_newsletter.php", true);
    ajaxRequest.send(null);

});

'#newsletter-register' is form that the user will use to submit his/her information. “#newsletter-register”是用户将用来提交其信息的表格。 The alert when the ajaxRequest is at readyState 4 is thrown correctly, but will an empty value. 正确抛出ajaxRequest处于readyState 4时的警报,但警报值为空。

My php is a simple echo returning a string to signal that the connection is correct (presently I'm just trying to see if the files are correctly calling eachother). 我的php是一个简单的回显,返回一个字符串以表示连接正确(目前,我只是在尝试查看文件是否正确地相互调用)。

I'm thinking it must be the url path to register_newsletter.php as I've tried placing the code within the template file, outside, etc. 我想它必须是register_newsletter.php的url路径,因为我尝试将代码放在模板文件中,外部等。

My javascript file is located in /theme/assets/js/code.js where as my template files (including the register_newsletter.php file) in /theme. 我的javascript文件位于/theme/assets/js/code.js中,作为我的模板文件(包括register_newsletter.php文件)位于/ theme中。

Any ideas? 有任何想法吗?

I had this type of issue yesterday. 我昨天有这类问题。 In my situation the page would only be accessed from 在我的情况下,只能从

www.site.com/aFolder/ 

The script was stored in my 该脚本存储在我的

'/wp-content/themes/themeName/processForm.php'

folder. 夹。

and the URL I used was: 我使用的网址是:

../wp-content/themes/themeName/processForm.php

My JS was simply in the head of the main header.php file, but note that the URL I had to use was relative to the URL the page the script was accessed from. 我的JS只是在header.php主文件的开头,但是请注意,我必须使用的URL相对于从中访问脚本的页面的URL。 Does that shed any light on things? 这对事情有什么启示吗?

EDIT: 编辑:

As troubleshooting steps, try ensuring you can access your php file from it's absolute URL, and have a look in firebug to see what link is being called when you fire the ajax request to see if it matches up with what you expect. 作为故障排除步骤,请尝试确保您可以从其绝对URL访问您的php文件,并在firebug中查看当您触发ajax请求以查看其是否符合您的期望时正在调用什么链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM