Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将javascript变量传递给php存储在wordpress的数据库中?_Javascript_Php_Html_Css_Ajax - Fatal编程技术网

如何将javascript变量传递给php存储在wordpress的数据库中?

如何将javascript变量传递给php存储在wordpress的数据库中?,javascript,php,html,css,ajax,Javascript,Php,Html,Css,Ajax,我正在wordpress中建立一个星级评级系统,因此希望将评级数据存储到wordpress数据库中。 我将星级php作为模板存储在wordpress主题文件夹中 <?php /* Template Name: RatingSystem */ ?> <html>for displaying the stars</html> <css>for hover and other star rating effects</css>

我正在wordpress中建立一个星级评级系统,因此希望将评级数据存储到wordpress数据库中。 我将星级php作为模板存储在wordpress主题文件夹中

<?php 
/* Template Name: RatingSystem */   
?>

<html>for displaying the stars</html>

<css>for hover and other star rating effects</css>

<script>
var r1;
var r2;
function submitForm() {
$.ajax({
         type: "POST",
         url: 'submit-rating.php',
         data: { r1 : r1 },
         success: function(data)
          {
            alert("success!");
           }
           });
                         }
function RatingValue(rating) 
{r1=rating;}    

function RatingValue1(rating1)
{r2=rating1;}   
</script>

用来展示星星的
用于悬停和其他星级效果
var-r1;
var-r2;
函数submitForm(){
$.ajax({
类型:“POST”,
url:'submitrating.php',
数据:{r1:r1},
成功:功能(数据)
{
警惕(“成功!”);
}
});
}
功能额定值(额定值)
{r1=额定值;}
功能额定值1(额定值1)
{r2=rating1;}
提交rating.php(php文件)



但是当我回显$rating时,不会显示任何输出。为什么?还请解释如何存储在wordpress数据库中。提前感谢。

您的输出不会显示,因为JS不“知道”它应该显示它。 而不是

函数submitForm(){
$.ajax({
类型:“POST”,
url:'submitrating.php',
数据:{r1:r1},
成功:功能(数据)
{
警惕(“成功!”);
}
});                        
}
试一试

函数submitForm(){
$.ajax({
类型:“POST”,
url:'submitrating.php',
数据:{r1:r1},
成功:功能(数据)
{
警报(数据);
}
});                        
}
函数(数据)
中的
数据
是JS得到的响应。为了在div中显示它,可以在显示
警报的位置使用
$('#divID').html(数据)

--编辑:忘记了问题的第二部分

我建议您使用
WPDB
在wordpress数据库中发布数据。如果此PHP文件是模板的一部分,或者您已经关闭了该文件的模板引擎,并且需要wp_load.PHP,则这是可能的。如果这两种方法都不适用,我建议您在php文件的顶部包含这一点,以实现后者

// Include WordPress
define('WP_USE_THEMES', false);
require_once('PATH/TO/wp-load.php');

可以找到有关
WPDB
的信息以及如何使用它。

感谢您花时间回答我的问题。我将alert(“success!”)替换为alert(data),但在调用函数时它仍然不显示任何警报。为什么?我还希望在submit-rating.php中插入数据库查询(这是位于同一主题文件夹中的另一个php文件)。您还可以提供一些好的来源来了解这一切(特别是关于用于将javascript变量传递到另一个php文件的ajax。)?您的控制台中是否有任何错误(chrome中的CTRL+SHIFT+I)?我想问题可能是url,试试
/path/to/theme/submit rating.php
。至于来源,我相信总结起来相当不错。我在wordpress.org中构建了这个,所以我将“localhost/mysite/wp content/themes/zincy lite/submit rating.php”作为url。这样说正确吗,因为我还没有得到任何警告?是吗在这里,你可以发送链接给我,这样我就可以看到它吗?
// Include WordPress
define('WP_USE_THEMES', false);
require_once('PATH/TO/wp-load.php');