从PHP传递两个变量,然后阿贾克斯回PHP变量、两个、PHP、阿贾克斯回

2023-09-11 22:34:56 作者:百分之百甜

我是新来的阿贾克斯,所以我不知道很多关于Ajax的语法。但我想在这里传递从PHP变量阿贾克斯,然后返回到PHP。我能够能得到它与一个变量做的时候遇到两个变量我很困惑。我甚至不知道该怎么对谷歌搜索得到答案我来查询。所以我会尽量简短,这里是我的PHP code。 addnewbug.php

 <脚本类型=文/ JavaScript的LANGUAGE =JavaScript的SRC =./的JavaScript / jquery.js和>< / SCRIPT>
<脚本类型=文/ JavaScript的LANGUAGE =JavaScript的SRC =./的JavaScript /的script.js>< / SCRIPT>
< /头>
 < D​​IV CLASS =缘自>
<身体BGCOLOR =#2e2e2e>
< D​​IV的风格=文本对齐:中心;填充顶:0px>
    < H1风格=颜色:白色;字体大小:50像素> Bughound< / H1>
< / DIV>
< D​​IV CLASS =effect8>
    < D​​IV CLASS =tableMargin>
        <表格的宽度=622级=表>
            < TR类=PROGRAM_ROW>
                <形式GT;
                < TD WIDTH =150级=TD的风格=填充右:1.9厘米>计划< / TD>

                    < TD WIDTH =171级=TD>
                        <选择ID =程序级=下拉列表中>
                            <选项>< /选项>
                            < PHP
                            要求./db.php;
                            $ SQL =SELECT DISTINCT程序名从程序;
                            $结果= DB($ SQL);
                            而($行= $ result-> FETCH_ASSOC()){
                                $ PROGRAM_NAME = $行['程序名'];
                                回声'<选项名称='。$ PROGRAM_NAME。'> 。 $ PROGRAM_NAME。 '< /选项>';
                            }
                            ?>
                        < /选择>
                    < / TD>



                < TD宽度=51级=TD>发行< / TD>
                < TD宽度=41>
                < D​​IV CLASS =释放ID =放>
                    <选择类=release_selectID ='release1'>
                    < /选择>
                < / DIV>
                < / TD>
                < TD WIDTH =103级=TD>版< / TD>
                < TD宽度=78级=TD>
                < D​​IV CLASS =版本ID =版本>
                    <选择类=version_selectID =节目_'>
                    < /选择>
                < / DIV>
                < / TD>
                < /形式GT;
            < / TR>
        < /表>
 

这是原来的页面,我试图让使用get传递语句改变选择框 这里的的script.js 我现在用得到变量,并把它传递到另一个php程序。

  $(函数(){
$(#程序)。改变(函数(){
    $(。release_select)删除();
    $(。version_select)删除();
    如果($(#程序)。VAL()!==){
        $获得(addnewbug1.php,{程序名:$(#程序)VAL()})
            .done(功能(数据){
                $(div.release)之后(数据)。
        });
        $获得(addnewbug_version.php,{程序名:$(#程序)VAL()})
            .done(功能(数据){
                $(div.version)之后(数据)。
        });
    }
});
$(#节目序)。改变(函数(){
    $(。release_select)删除();
    如果($(#节目序)。VAL()!==){
        。VAR将val2 = $(#节目序)VAL();
         $获得(addnewbug2.php节目_ =?+ val2中,{程序名:$(#程序)VAL()})
            .done(功能(数据){
                $(div.release)之后(数据)。
        });
    }
});
});
 

第一个功能是工作的罚款,因为它需要的值从程序,并把它传递给addnewbug1.php这需要程序名和产生被替换DIV释放和分裂的新选择框发布和版本的它,然后-version(或数量)

thinkphp怎么可以在M方法里面传一个变量

这是该文件,其中第一个功能很好地工作 - addnewbug1.php

 规定./db.php;
回声<选择类='release_select'ID ='release1'>中;
$ PROGRAMNAME = $ _GET ['程序名'];
$ SQL1 =SELECT DISTINCT program_release FROM程序WHERE程序名='$ PROGRAMNAME。';
$ RESULT1 = DB($ SQL1);
而($ ROW1 = $ result1-> FETCH_ASSOC()){
$ program_release = $ ROW1 ['program_release'];
回声'<选项名称=,$ program_release。'> 。 $ program_release。 '< /选项>';
}
 回声< /选择&g​​t;中;
?>
 

我现在的script.js,我需要传递$不用彷徨区域两个变量的第二个变化函数有错误。另外,.change温控功能的节目序不能正常工作,它不会删除改变它的选择框。的

  $得到(addnewbug2.php节目_ =?+ val2中,{程序名:$(#程序)VAL()})
 

和 addnewbug2.php 也正被用于它的新文件 -

 规定./db.php;
 回声<选择类='release_select'ID ='release1'><选项>< /选项>中;
 $ PROGRAMNAME = $ _GET ['程序名'];
 $ programnumber = $ _GET [节目_'];
 $ SQL1 =SELECT DISTINCT program_release FROM程序WHERE程序名='。$ PROGRAMNAME。'和节目_ ='。 $ programnumber;

 $ RESULT1 = DB($ SQL1);
 而($ ROW1 = $ result1-> FETCH_ASSOC()){
$ program_release = $ ROW1 ['program_release'];
回声'<选项名称=,$ program_release。'> 。 $ program_release。 '< /选项>';
 }
 回声< /选择&g​​t;中;
 ?>
 

我不知道你会得到我的问题或没有。感谢您回答这个问题,请回复,如果需要更多的信息。

这是addidtional db.php中 code

 功能分贝($ SQL){
  //检查连接变量已经设置
  如果(!使用isset($康恩)){
      //数据库连接 -  IP,用户名,密码,数据库名
      $康恩=新的mysqli(我已经正确地填补了这一);
  }

//检查连接
如果($ conn-> connect_error){
    死亡(连接失败:$ conn-> connect_error。);
}
$结果= mysqli_query($康恩,$ SQL);
mysqli_close($康恩);
回报($结果);
 }
 

解决方案

感谢您的回答,我用AngularJS,我开始张贴这个问题后阅读解决我的问题。这背后的问题,我的主要摩托是填充(第2)选择框,(3)选择框使用值从其他(1日)选择框,当新的填充值(第二)选择框已完成。然后,使用(第2次)的选择框更改值(3)选择框。反之为最后一个点。应用独特的过滤器选择框,并没有移动到下一个页面。 所以这里的code简化形式 Addnewbug.php -

 <!DOCTYPE HTML>
          < HTML>
          &所述;脚本的src =htt​​p://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
          &所述;脚本的src =htt​​ps://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.4/angular-filter.js"></script>
          &LT;身体GT;

          &LT; D​​IV NG-应用程序=对myAppNG控制器=customersCtrl&GT;

          &LT;选择NG模型=selectProgram&GT;
          &LT;选项&GT;&LT; /选项&GT;
          &LT;选项NG重复=x合programes |独特之处:prognumber'NAME ={{x.progname}}&GT; {{x.progname}}&LT; /选项&GT;
          &LT; /选择&GT;
          &LT;选择NG模型=selectNumber&GT;
          &LT;选项&GT;&LT; /选项&GT;
          &LT;选项NG重复=x合programes | filterBy:['的progname']:selectProgram | filterBy:['progrelease']:selectRelease |独特:prognumber'NAME ={{x.prognumber}} &GT; {{x.prognumber}}&LT; /选项&GT;
          &LT; /选择&GT;
          &LT;选择NG模型=selectRelease&GT;
          &LT;选项&GT;&LT; /选项&GT;
          &LT;选项NG重复=x合programes | filterBy:['的progname']:selectProgram | filterBy:['prognumber']:selectNumber |独特:progrelease'NAME ={{x.progrelease}} &GT; {{x.progrelease}}&LT; /选项&GT;
          &LT; /选择&GT;
          &LT; / DIV&GT;

          &LT;脚本&GT;
          VAR应用= angular.module('对myApp',['angular.filter']);
          app.controller('customersCtrl',函数($范围,$ HTTP){
             $ http.get(DB / program_db.php)
             .success(函数(响应){$ scope.programes = response.records;});
          });
          &LT; / SCRIPT&GT;

          &LT; /身体GT;
          &LT; / HTML&GT;
 

offcourse我用一些其他stackexchange问​​题得到它的权利。 而对于正被导入这个$ C $词表用这个 program_db -

 &LT; PHP
    标题(访问控制 - 允许 - 产地:*);
    标题(内容类型:应用程序/ JSON;字符集= UTF-8);
    如果(!使用isset($康恩)){
            //数据库连接 -  IP,用户名,密码,数据库名
        $康恩=新的mysqli(*连接参数*);
     }
    如果($ conn-&GT; connect_error){
         死亡(连接失败:$ conn-&GT; connect_error。);
    }
    $结果= mysqli_query($康恩,选择程序名,节目序,program_release从方案);
    $ OUTP =;
    而($ RS = $ result-&GT; fetch_array(MYSQLI_ASSOC)){
        如果($ OUTP =!){$ OUTP =。,;}
        。$ OUTP ='{的progname:'。$ RS [程序名]。,;
        。$ OUTP ='prognumber:'。$ RS [节目_。;
        。$ OUTP ='progrelease:'。$ RS [program_release。'}';
    }
    $ OUTP ='{记载:['。$ OUTP']};
    $ conn-&GT;关闭();

    回声($ OUTP);
    ?&GT;
 

I'm new to ajax so I don't know much about ajax syntax. though i am trying here to pass variable from php to ajax and then back to php. I was able able to get it done with one variable when it came to two variables I was confused. I don't even know what to search on google to get an answer my to query. So I will be brief here's my php code. 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>

This is the original page where I'm trying to make changes in select box using the get passing statement here's the script.js which I am using to get variable and pass it to another php program..

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

The first function is working fine as it takes values from the program and pass it to addnewbug1.php which takes the program name and generate the new select boxes for release and version of it then which is replaced in div-release and division-version(or number)

this is the file where the first function work perfectly - 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>";
?> 

Now I am having error in the second change function of script.js where I need to pass two variables in '$.get' area. Also the .change fucntion for program_number is not working and it won't delete the select box on changing it.

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

and the addnewbug2.php is also the new file which is being used by it is -

 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>";
 ?>

I don't know if you get my question or not. Thank you for replying to this question and please reply if require more info.

this is the addidtional db.php code

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

解决方案

Thank you for replying, i solved my problem using AngularJS, which i started reading after posting this question. My main moto behind this question was to populate a (2nd)select box and (3rd)select box using value from other (1st) select box and when the values in newly populate (2nd) select box was done. Then change values (3rd)select box using the (2nd) select box. Vice versa for last point. Apply unique filter to select boxes and without moving to next page. So here's the code simplified form of 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 i used some other stackexchange questions to get it right. And for the table which is being imported in this code i used this 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);
    ?>