如何将php var传递给同一页面中的下一个查询?

时间:2018-08-13 09:01:33

标签: javascript php

我了解了但我无法解决问题,dd.php有两个从mysql表检索的下拉列表框,当从第一个drop dwon框中选择时将相对值加载到第二个drop dwon框(与country和状态下降dwon框),我想添加第三个msyql query3从第二个下降dwon框获取值$ subcate作为mysql的条件,选择query3并回显整个表行,请帮助我如何做到这一点,这是php代码和我添加的query3:

    <?php
require 'config.php';  // Database connection
//////// End of connecting to database ////////
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Multiple drop down list box</title>
<script src="js/jquery-3.3.1.min.js" ></script>

<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;
}
    $('.subcat').click(function(){
        var subcat = $(this).attr('id');
       // to check whether value passing or not 
       console.log(subcat);
        $.ajax({
            type: 'post',
            data: {id: subcat}, //Pass the id
        });
    });
</script>
</head>

<body>
<?Php

@$cat=$_GET['cat']; // Use this line or below line if register_global is off
if(strlen($cat) > 0 and !is_numeric($cat)){ // to check if $cat is numeric data or not. 
echo "Data Error";
exit;
}



///////// Getting the data from Mysql table for first list box//////////
$quer2="SELECT DISTINCT category,cat_id FROM category order by category"; 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0){
$quer="SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"; 
}else{$quer="SELECT DISTINCT subcategory FROM subcategory order by subcategory"; } 
////////// end of query for second subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example  action=dd-check.php////
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
foreach ($dbo->query($quer2) as $noticia2) {
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
echo "<select name='subcat' id='subcat'><option value=''>Select one</option>";
$subcat = '';
foreach ($dbo->query($quer) as $noticia) {
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";

////This is my third query3 ////
$subcat=$_POST['subcat'];
$query3 = $dbo->prepare("SELECT * FROM subcategory  WHERE subcategory=' .$subcat.'");
$query3->execute([$subcat]); 
while ($row = $query3->fetch(PDO::FETCH_ASSOC))
{
  $cat = $row['cat_id'];
  $name = $row['name'];
  $from = $row['subcategory'];
  $user = $row['user'];
  $passw= $row['passw'];
  $server = $row['server'];
  $auth = $row['auth'];
  $secure = $row['secure'];  
  $port = $row['port'];
}

//////////////////  This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>
<br><br>
<a href=dd.php>Reset and start again</a>
<br><br>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

感谢您阅读有关session()的回复,并补充了我新增的php页面:

<?php
session_start();
require 'config.php';  // Database connection
//////// End of connecting to database ////////
?>

然后在第二个下拉菜单的开头添加了

//////////        Starting of second drop downlist /////////
$_SESSION["subcat"]=$subcat;
echo "<select name='subcat' id='subcat'><option value=''>Select one</option>";
$subcat = '';
foreach ($dbo->query($quer) as $noticia) {
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";

在我的query3之前的最后一个中,需要添加$ subcat作为条件:

////This is my third query3 need the $subcat as condition ////
session_start();
$var = $_SESSION["subcat"];
$subcat=$_GET['subcat'];
$query3 = $dbo->prepare("SELECT * FROM subcategory  WHERE subcategory=' .$var. '");
$query3->execute([$subcat]); 
while ($row = $query3->fetch(PDO::FETCH_ASSOC))
{
  $cat = $row['cat_id'];
  $name = $row['name'];
  $from = $row['subcategory'];
  $user = $row['user'];
}
echo $_SESSION["subcat"];

但是query3无法获得$ subcat值。