Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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页面_Javascript_Php_Ajax - Fatal编程技术网

将javascript变量传递到另一个php页面

将javascript变量传递到另一个php页面,javascript,php,ajax,Javascript,Php,Ajax,我有一个选择列表,在使用javascript的过程中,我得到了选择的值,我希望这些选择的值通过php文件init.php传递,这样我就可以在mysql查询中使用这些变量。 我的javascript代码如下所示: $(document).ready(function(){ var e = document.getElementById("product"); var pro = e.opt

我有一个选择列表,在使用javascript的过程中,我得到了选择的值,我希望这些选择的值通过php文件init.php传递,这样我就可以在mysql查询中使用这些变量。 我的javascript代码如下所示:

                  $(document).ready(function(){ 

                 var e = document.getElementById("product");
                  var pro = e.options[e.selectedIndex].text;
                  alert(pro);

                 });

                 $('select').change(function(){ 
                  var e = document.getElementById("city");
                  var cit = e.options[e.selectedIndex].text;
                  alert(cit);
我使用ajax将变量发送到init.php。我下面的ajax代码不起作用,有人能告诉我代码中的问题是什么吗

                       $.ajax({
                         url: 'init.php',
                         type: 'POST',
                         data: { x:'cit',y:'pro' },
                         success: function(data) {
                           console.log(data);

                              }
                            });
在init.php中,我写了:

<?php
$var1 = $_POST['y'];
$var2 = $_POST['x'];
$result = "Select amount from ". _DB_PREFIX_ ."demo_detail where product =    '". $var1 ."' and city = '" . $var2 . "' ";
//echo json_encode($result);

能否更改
url
行以包含/以确保您引用的是相对于目录根的
init.php

所以它应该是这样的:

$.ajax({
   url: '/init.php',
   type: 'POST',
   data: { x:'cit',y:'pro' },
   success: function(data) {
     console.log(data);

       }
   });

我不知道该怎么说,但AJAX有可能向错误的URL发出POST请求。

能否更改
URL
行以包含/以确保您引用的是相对于目录根的
init.php

所以它应该是这样的:

$.ajax({
   url: '/init.php',
   type: 'POST',
   data: { x:'cit',y:'pro' },
   success: function(data) {
     console.log(data);

       }
   });

我不知道该怎么说,但AJAX有可能向错误的URL发出POST请求。

您是否尝试在URL中传递绝对路径和相对路径。我的意思是 您是否尝试过使用:

url:'localhost:xxxx/myapp/init.php'


您是否尝试在url中传递绝对路径和相对路径。我的意思是 您是否尝试过使用:

url:'localhost:xxxx/myapp/init.php'


如果要在Ajax中将变量传递到数据参数中,则不必使用引号

$.ajax({
        url: 'init.php',
        type: 'POST',
        data: { x: cit, y: pro },
        success: function(data) {
             console.log(data);
        }
 });

如果要在Ajax中将变量传递到数据参数中,则不必使用引号

$.ajax({
        url: 'init.php',
        type: 'POST',
        data: { x: cit, y: pro },
        success: function(data) {
             console.log(data);
        }
 });

使用查询字符串。HTML5的会话存储也可以帮助您。

使用查询字符串。HTML5的会话存储也可以帮助您。

试着用以下代码替换您的脚本代码,看看它是否有帮助

$(document).ready(function(){ 
    $('select').change(function(){
        var e = document.getElementById("product");
        var pro = e.options[e.selectedIndex].text;
        alert(pro);

        var e = document.getElementById("city");
        var cit = e.options[e.selectedIndex].text;
        alert(cit);

        $.ajax({
            type: 'POST',
            url: 'init.php',
            data: { x:'cit',y:'pro' },
            success: function(data) {
            console.log(data);
            }
        });
    });     
});

试着用下面的代码替换脚本代码,看看它是否有不同

$(document).ready(function(){ 
    $('select').change(function(){
        var e = document.getElementById("product");
        var pro = e.options[e.selectedIndex].text;
        alert(pro);

        var e = document.getElementById("city");
        var cit = e.options[e.selectedIndex].text;
        alert(cit);

        $.ajax({
            type: 'POST',
            url: 'init.php',
            data: { x:'cit',y:'pro' },
            success: function(data) {
            console.log(data);
            }
        });
    });     
});

您是否在
pro
cit
上都收到警报?是的,我在这两个方面都收到警报。。javascript代码运行良好您是否在
pro
cit
上都收到了警报?是的,我在这两个方面都收到了警报。。javascript代码正常工作。应用程序配置不允许给定URL:应用程序设置不允许一个或多个给定URL。它必须与网站URL或画布URL匹配,或者域必须是应用程序域之一的子域。我得到了这个错误。我使用了两个url。应用程序配置不允许使用给定url:应用程序设置不允许使用一个或多个给定url。它必须与网站URL或画布URL匹配,或者域必须是应用程序域之一的子域。我得到了这个错误。我使用了这两个url。我得到这个错误未捕获引用错误:如果我删除引号,则未定义pro。您是否将cit和pro声明在与Ajax调用相同的范围内?我得到这个错误未捕获引用错误:如果我删除引号,则未定义pro。您是否将cit和pro声明在与Ajax调用相同的范围内?我得到相同的结果控制台日志中出错:应用程序配置不允许给定URL:应用程序的设置不允许一个或多个给定URL。它必须与网站URL或画布URL匹配,或者域必须是应用程序某个域的子域。请检查.htaccess文件,是否在其中,如果是,请删除所有代码并保存空白文件,然后重试后返回控制台日志中的错误:应用程序配置不允许给定URL:应用程序的设置不允许一个或多个给定URL。它必须与网站URL或画布URL匹配,或者域必须是应用程序某个域的子域。请检查.htaccess文件,如果是,请删除所有代码保存空白文件,然后尝试发回