简体   繁体   English

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

[英]Passing javascript variables to another php page

I have a select list where in using javascript i get the selected values i want this selected values to pass through php file init.php so that i can use those variables in mysql query. 我有一个选择列表,在其中我可以使用javascript获取选定的值,我希望该选定的值通过php文件init.php传递,以便我可以在mysql查询中使用这些变量。 my javascript code is as follows: 我的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);

I have used ajax to send variables to init.php. 我已经使用ajax将变量发送到init.php。 my ajax code below is not working,can anyone tell whats the issue in this code: 我下面的ajax代码无法正常工作,任何人都可以告诉此代码什么问题:

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

                              }
                            });

and in init.php i have written : 在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);

Can you alter the url line to include the / to make sure that you're referring init.php relative to the root of your directory? 您是否可以更改url行以包含/来确保相对于目录根引用的是init.php

So it should look like this: 所以它应该看起来像这样:

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

       }
   });

I don't know enough to say for sure but there's a chance that AJAX is making a POST request to the wrong URL. 我不太清楚可以肯定地说,但是AJAX有可能向错误的URL发出POST请求。

Have you tried to pass absolute as well as relative path in url. 您是否尝试过在URL中传递绝对路径以及相对路径。 What I mean is have you tried using: 我的意思是您是否尝试过使用:

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

or 要么

url:'/init.php'

If you're passing variables into the data parameter in Ajax, they don't have to be in quotes. 如果要将变量传递到Ajax的data参数中,则不必在引号中添加变量。

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

Use Query String. 使用查询字符串。 HTML5's Session Storage can also help you. HTML5的会话存储也可以为您提供帮助。

Try to replace your script code with following and see if it makes a difference 尝试将脚本代码替换为以下代码,看看是否有区别

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

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

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