如何在一个页面中将变量从一个php代码块传递给另一个?

时间:2015-10-11 07:53:05

标签: php database variables

我可能会收到错误,$result = mysql_query("Select setyear, YearName from tblset, tblyear where tblyear.ID=tblset.setyear group by setyear having tblset.setcours=".$res, $connection);

我想传递变量 $ res 中的select name="studcourse" class="form-control“**的值,并将其放在** {下的 $ result 变量中{1}}”。

有人能帮助我吗?

select name="studyear" class="form-control

3 个答案:

答案 0 :(得分:0)

您可以使用会话变量将一个变量传递给另一个php:

double amount = 13;
int months = 3;
int precision = 2;

double[] amountForEachMonth = new double[months];

double temp = Math.Round(amount / months, precision);

for (int i = 0 ; i < months - 1 ; i++)
    amountForEachMonth[i] = temp;

amountForEachMonth[months - 1] = amount - (temp * (months - 1)) ;    

第二个php:

<? Php 
session_start(); 
$z=3;
$_SESSION['var_name'] =$z;? >

答案 1 :(得分:0)

您需要在启动html代码之前定义<?php $res ?>

<?php 
$res = '';
if($_POST['studcource']){
$res = $POST['studcource'];
}

    <form method="post" action="<?php echo $_SERVER['$PHP_SELF'];?>">
<div class="form-group">
<label>Student Course</label>
<select name="studcourse" class="form-control" onchange="document.form.submit();">
<?php
 $result = mysql_query("Select setcours, course_desc from tblset, tbl_coursetype where tbl_coursetype.course_no=tblset.setcours group by setcours", $connection);

 if (!$result) {
 die("Database query failed: " . mysql_error());
 }
 // 4. Use returned data
 while ($row = mysql_fetch_array($result)) {  
 echo "<option value=\"{$row[0]}\">{$row[1]}</option>";  
 }  ?>
</select> 
</div>
<div class="form-group">
<label>Student Year</label> 
<select name="studyear" class="form-control">
<?php
$result = mysql_query("Select setyear, YearName from tblset, tblyear where tblyear.ID=tblset.setyear group by setyear having tblset.setcours=".$res, $connection);
 if (!$result) {
 die("Database query failed: " . mysql_error());
 }
 // 4. Use returned data
 while ($row = mysql_fetch_array($result)) {  
 echo "<option value=\"{$row[0]}\">{$row[1]}</option>";  
 }
?>
</select> 
</div>

答案 2 :(得分:0)

如果这不是您的意思,请提前道歉但我将此问题解释为您希望根据第一个下拉菜单中的选择填充第二个下拉菜单。为此(未经测试),下面的方法实现了ajax,以便将POST请求发送到随后在sql查询中使用的同一页面。然后使用sql查询的结果生成第二个下拉菜单的选项...

 function LoadDiv(url, lnk) {
            var img = new Image();
            var bcgDiv = document.getElementById("divBackground");
            var imgDiv = document.getElementById("divImage");
            var imgFull = document.getElementById("imgFull");
            var imgLoader = document.getElementById("imgLoader");
            var dl = document.getElementById("<%=DataList1.ClientID%>");
            var imgs = dl.getElementsByTagName("img");

            CurrentPage = GetImageIndex(lnk.parentNode) + 1;
            imgLoader.style.display = "block";
            img.onload = function () {
                imgFull.src = img.src;
                imgFull.style.display = "block";
                imgLoader.style.display = "none";
            };
            img.src = url;
            Prepare_Pager(imgs.length);
            var width = document.body.clientWidth;
            if (document.body.clientHeight > document.body.scrollHeight) {
                bcgDiv.style.height = document.body.clientHeight + "px";
            }
            else {
                bcgDiv.style.height = document.body.scrollHeight + "px";
            }

            imgDiv.style.left = (width - 650) / 2 + "px";
            imgDiv.style.top = "20px";
            bcgDiv.style.width = "100%";

            bcgDiv.style.display = "block";
            imgDiv.style.display = "block";
            return false;
        }