dongqu2863 2014-09-24 19:47
浏览 26

了解PHP POST以及如何提交多个命令

I have a growing amount of code in my page. Individually, I have an okay understanding of how all the various pieces work, but I don't understand how they function TOGETHER. The first bit of code I have pulls a name from an SQL database, then passes it into another table, sorting that table by the name I've chosen:

<form method="post" enctype="multipart/form-data">
<table id="" summary="PAGE HEADER" style='width:20%'>
<tr> 
  <th>
  <?php
    include('./db.php');
    $PM = mysqli_query($con, "SELECT DISTINCT PMName FROM report ORDER BY PMName ASC");
  ?> 
    <b style="font-family: Candara, Calibri, Segoe, Optima, Arial, sans-serif;">Choose PM: </b>
    <br> 
    <span class="custom-dropdown custom-dropdown--red">    
    <Select class="custom-dropdown__select custom-dropdown__select--red" name="PMName" onChange="submit(this.form)"<> 
  <?php
    while ($row = mysqli_fetch_row($PM)) {
    $selected = array_key_exists('PMName', $_POST) && $_POST['PMName'] == $row[0] ? ' selected' : '';
    $sel = ($table === $row[0]) ? "id='sel' selected" : "";
    printf(" <option value='%s' %s>%s</option>
", $row[0], $selected, $row[0]);
    }
  ?>  
  </th>
</span>
</tr>
</select>    
</form>

Because the results are huge, I've built in a sort of "previous page, next page" deal so the user can display only 10 results at a time.

<?php
if (isset($_POST['next'])) {
$postedLimit = (isset($_POST['next']) ? (int) $_POST['next'] : 0);
$nextLimit = $postedLimit + 10;
session_start();
$_SESSION['page'] = ((int) $nextLimit / 10)+1;
$result = mysqli_query($con, "SELECT * FROM report WHERE PMName = '$PMSelection' AND REGNSB <> 0.000 ORDER BY RegNSB DESC Limit $nextLimit,$LimitItems");
}
?>

<table id="box-table-a" stlye="widgth:20%">
<form method="POST" action="">
<div class="container">
<div class="right">Page <?= $_SESSION['page'] ?> of <?= $totalPages ?>   
<input type="submit" name="next" value="next" onclick="this.value=<?php echo $nextLimit; ?>">
</div></form></div></table>

My issue is that when I click this "next" button, this query is run:

$result = mysqli_query($con, "SELECT * FROM report WHERE PMName = '$PMSelection' AND REGNSB <> 0.000 ORDER BY RegNSB DESC Limit $nextLimit,$LimitItems");

but, (forgive my non-understanding of how this whole thing works...) $PMSelection is now empty, and is not passing any value to the SQL query, so whatever name I had the list filtered by goes away. I come from the world of VBA, where once I define something, I can use it forever... why $PMSelection HAS a value at one point and then DOESN'T have a value later on is very confusing to me....

My questions are these:

  1. Why is $PMSelection, once defined and previously used, not still available later on when I try to use it?
  2. How do I make it so that $PMSelection is HELD somewhere/somehow so I can access it in this second bit of code?
  • 写回答

2条回答 默认 最新

  • doushanlv5184 2014-09-24 19:55
    关注

    On your first page where you have the variable $PMSelection filled put in into your $_SESSION like this:

    if (!empty($_POST['PMName'])) {
        $_SESSION['PMSelection'] = $_POST['PMName'];
    }
    

    And the second page, if it is called later, can get this value back like this:

    $PMSelection = $_SESSION['PMSelection'];
    

    You can do this with all your data.

    Another way

    If the <form> element of the first page would send it's data to the second page on submitting it, then you would get all input field data in the $_POST array when second page is called. But you have to change your <form> tag like this:

    <form action="second_page.php" method="post" enctype="multipart/form-data">
    

    And if there is no input element with this data in your form, and the data is not sensitive you can make a hidden input field and put your data in it, like this:

    <input type="hidden" name="PMSelection" value="{value}" /><!-- single data -->
    
    <input type="hidden" name="PMSelection[]" value="{value1}" /><!-- array data -->
    <input type="hidden" name="PMSelection[]" value="{value2}" />
    <input type="hidden" name="PMSelection[]" value="{value2}" />
    [...]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答