将两个变量从php传递给ajax然后再传回php

时间:2015-06-27 02:34:01

标签: javascript php jquery mysql ajax

我是ajax的新手,所以我不太了解ajax语法。虽然我在这里尝试将变量从php传递到ajax然后再回到php。当我遇到困惑的两个变量时,我能够用一个变量完成它。我甚至不知道在谷歌上搜索什么来获得我的查询答案。所以我将简要介绍一下我的PHP代码。的 addnewbug.php

<script type="text/javascript" language="javascript" src="./javascripts/jquery.js"></script>
<script type="text/javascript" language="javascript" src="./javascripts/script.js"></script>
</head>
 <div class="margin custom">
<body bgcolor="#2e2e2e">
<div style="text-align: center; padding-top: 0px">
    <h1 style="color:white;font-size: 50px">Bughound</h1>
</div>
<div class="effect8">
    <div class="tableMargin">
        <table width="622" class="table">
            <tr class="program_row">
                <form>
                <td width="150" class="td" style="padding-right: 1.9cm">Program</td>

                    <td width="171" class="td">
                        <select id="program" class="dropdown">
                            <option></option>
                            <?php
                            require "./db.php";
                            $sql = "SELECT DISTINCT program_name FROM program";
                            $result = db($sql);
                            while ($row = $result->fetch_assoc()) {
                                $program_name = $row['program_name'];
                                echo '<option name ="' . $program_name . '">' . $program_name . '</option>';
                            }
                            ?>
                        </select>
                    </td>



                <td width="51" class="td">Release</td>
                <td width="41" >
                <div class="release" id="release">
                    <select class="release_select" id='release1'>
                    </select>
                </div>
                </td>
                <td width="103" class="td">Version</td>
                <td width="78" class="td">
                <div class="version" id="version">
                    <select class="version_select" id='program_number'>
                    </select>
                </div>
                </td>
                </form>
            </tr>
        </table>

这是我尝试使用get pass语句在选择框中进行更改的原始页面 这里是 script.js ,我用它来获取变量并将其传递给另一个php程序..

  $(function(){
$("#program").change(function(){
    $(".release_select").remove();
    $(".version_select").remove();
    if($("#program").val() !== "") {
        $.get("addnewbug1.php", {program_name: $("#program").val()})
            .done(function(data){
                $("div.release").after(data);
        });
        $.get("addnewbug_version.php", {program_name: $("#program").val()})
            .done(function(data){
                $("div.version").after(data);
        });
    }
});
$("#program_number").change(function(){
    $(".release_select").remove();
    if($("#program_number").val() !== "") {
        var val2 = $("#program_number").val();
         $.get("addnewbug2.php?program_number="+val2, {program_name: $("#program").val()})
            .done(function(data){
                $("div.release").after(data);
        });
    }
});
});

第一个函数工作正常,因为它从程序获取值并将其传递给addnewbug1.php,它获取程序名称并生成新的选择框以用于发布和版本然后在div-release和division中替换-version(或数字)

这是第一个功能完美运作的文件 - addnewbug1.php

require "./db.php";
echo "<select class='release_select' id='release1'>";
$programname = $_GET['program_name'];
$sql1 = 'SELECT DISTINCT program_release FROM program WHERE program_name="'. $programname .'"';
$result1 = db($sql1);
while ($row1 = $result1->fetch_assoc()) {
$program_release = $row1['program_release'];
echo '<option name ="' . $program_release . '">' . $program_release . '</option>';
}
 echo "</select>";
?> 

现在我在script.js的第二个更改函数中遇到错误,我需要在&#39; $中传递两个变量。得到&#39;区域。此外,program_number的.change功能无效,在更改时不会删除选择框。

$.get("addnewbug2.php?program_number="+val2, {program_name: $("#program").val()})

并且 addnewbug2.php 也是正在使用的新文件 -

 require "./db.php";
 echo "<select class='release_select' id='release1'><option></option>";
 $programname = $_GET['program_name'];
 $programnumber = $_GET['program_number'];
 $sql1 = 'SELECT DISTINCT program_release FROM program WHERE program_name="'. $programname .'" and program_number=' . $programnumber;

 $result1 = db($sql1);
 while ($row1 = $result1->fetch_assoc()) {
$program_release = $row1['program_release'];
echo '<option name ="' . $program_release . '">' . $program_release . '</option>';
 }
 echo "</select>";
 ?>

我不知道你是否得到了我的问题。感谢您回复此问题,如果需要更多信息,请回复。

这是附加的 db.php 代码

  function db($sql){
  //Check for connection variable already set
  if(!isset($conn)){
      //Database Connectivity - ip, username, password, database name
      $conn = new mysqli("i have filled this correctly");
  }

//Check Connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn, $sql);
mysqli_close($conn);
return($result);
 }

3 个答案:

答案 0 :(得分:1)

json_encode()非常适合使用ajax传输php变量

类似

//你的ajax呼叫接收者页面 - :your_link_for_ajax.php

    $output =     json_encode(array('type'=>'success','address'=>$address,'table_record'=>$table_record));
    die($output);

你可以像

一样阅读

你的html页面上的这段代码

    $.post('your_link_for_ajax', post_data, function(response){  

                    //load json data from server and output message     
                    if(response.type == 'error')
                    {
                        output = '<div class="alert alert-danger">'+response.text+'</div>';
                    }else{
                        output = '<div class=" alert alert-success">'+response.address+'</div>';
                        $('#stateIdContact').html(response.table_record);

                    }

                }, 'json');

答案 1 :(得分:1)

如果我能正确理解,那么你最初的问题是在AJAX调用中传递多个变量。

您可以像这样构建网址:

var val2 = $("#program_number").val();
var val1 = $("#program").val();
$.get("addnewbug2.php?program_number="+val2+"&program_name="+val1)

答案 2 :(得分:0)

感谢您的回复,我使用AngularJS解决了我的问题,我在发布此问题后开始阅读。我在这个问题背后的主要动机是使用来自其他(第一个)选择框的值填充(第二个)选择框和(第三个)选择框,并且当新填充(第二个)选择框中的值完成时。然后使用(第二个)选择框更改值(第3个)选择框。最后一点反之亦然。应用唯一过滤器以选择框,而无需移至下一页。 所以这是 Addnewbug.php -

的代码简化形式
              <!DOCTYPE html>
          <html >
          <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
          <script src= "https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.4/angular-filter.js"></script>
          <body>

          <div ng-app="myApp" ng-controller="customersCtrl">

          <select ng-model="selectProgram">
          <option></option>
          <option ng-repeat="x in programes | unique: 'prognumber'" name=""{{x.progname}}"">{{ x.progname }}</option>
          </select>
          <select ng-model="selectNumber">
          <option></option>
          <option ng-repeat="x in programes | filterBy: ['progname']:selectProgram | filterBy: ['progrelease']:selectRelease | unique: 'prognumber'" name=""{{x.prognumber}}"">{{ x.prognumber }}</option>
          </select>
          <select ng-model="selectRelease">
          <option></option>
          <option ng-repeat="x in programes | filterBy: ['progname']:selectProgram | filterBy: ['prognumber']:selectNumber | unique: 'progrelease'" name=""{{x.progrelease}}"">{{ x.progrelease }}</option>
          </select> 
          </div>

          <script>
          var app = angular.module('myApp', ['angular.filter']);
          app.controller('customersCtrl', function($scope, $http) {
             $http.get("db/program_db.php")
             .success(function (response) {$scope.programes = response.records;});
          });
          </script>

          </body>
          </html>

offcourse我使用了一些其他的stackexchange问​​题来解决问题。 对于在此代码中导入的表格,我使用了 program_db -

    <?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    if(!isset($conn)){
            //Database Connectivity - ip, username, password, database name
        $conn = new mysqli("*connection parameters*");
     }
    if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
    }
    $result = mysqli_query($conn, "SELECT program_name, program_number,program_release FROM program");
    $outp = "";
    while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
        if ($outp != "") {$outp .= ",";}
        $outp .= '{"progname":"'  . $rs["program_name"] . '",';
        $outp .= '"prognumber":"'   . $rs["program_number"]        . '",';
        $outp .= '"progrelease":"'. $rs["program_release"]     . '"}';
    }
    $outp ='{"records":['.$outp.']}';
    $conn->close();

    echo($outp);
    ?>