duanbi7204 2018-11-08 18:46
浏览 43
已采纳

为什么我无法在PHP中传递变量

I got a function.php which is in inc/function. This function contains a table. This table is generated in an MySQL statement.

My idea is quite easy - the user is able to go through the table with a next button and a back button.

Here is my code:

<div class="container-fluid">
        <form action="fda.php" method="post">

  <div class="row">
    <div class="col-md-6 col-md-offset-3">
                <p>Bitte beantworten Sie die folgenden Fragen:</p>
        <table class="table table-striped">
            <thead>
                  <tr>
                    <th>Nr.</th>
                    <th width="250">Frage</th>
<?php tbl_description() ?>

                      
                  </tr>
                </thead>
            <tbody>
                <?php tbl_showPage() ?>
            </tbody>
        </table>


    </div> 
    <div class="col-md-6 col-md-offset-3 text-center">
    <form action="functions.php" method="post">
    <input type="submit" name="back" value="Zurück" id="back" class="btn btn-default">
    <input type="submit" name="go" value="Weiter" id="go" class="btn btn-default">
    
        <!--<input type="submit" value="FDA auswerten" class="btn btn-default">-->
    </div>    
  </div>
                                                                                         </form>
</div>

this is the part from the fda.php file! Everything is working cool except the increase of the mysql statement which is in the function.php. While clicking on next the statement should be "refreshed" and show me the 2nd page of the table. Its like an questionaire.

This is the code of the function php.

function tbl_showPage(){


    global $mysqlhost, $mysqluser, $mysqlpwd, $mysqldb;


    $connection=mysqli_connect($mysqlhost, $mysqluser, $mysqlpwd) or die ("Verbindungsversuch fehlgeschlagen");
    mysqli_select_db($connection, $mysqldb) or die("Konnte die Datenbank nicht waehlen.");

    if(isset($_POST['go']))
    {
        $page++;

    }
    elseif($_POST['back'])
    {
        $page--;
    }

    //if(isset($page)){
    if($page<1)
    {
        $sql_tbl_questions = "SELECT * FROM `questions` where istAktiv='1' && dimension='1'";
    }
    elseif($page>9)
    {
        $sql_tbl_questions = "SELECT * FROM `questions` where istAktiv='1' && dimension='9'";
    }
    else
    {
        $sql_tbl_questions = "SELECT * FROM `questions` where istAktiv='1' && dimension=$page";
    }
    //}
    $quest_query = mysqli_query($connection, $sql_tbl_questions) or die("Anfrage nicht erfolgreich");
    $i = 0;

    while ($question = mysqli_fetch_array($quest_query)) {
        $i++;
        echo '<tr>';
        echo '<th>'.$i.'</th>';
        echo '<th>'.$question['question'].'</th>';
        echo '<th class="text-center"><input type="radio" name="'.$question['dimension'].'_question_'.$question['id'].'" value="0" required></th>';
        echo '<th class="text-center"><input type="radio" name="'.$question['dimension'].'_question_'.$question['id'].'" value="2.5"></th>';
        echo '<th class="text-center"><input type="radio" name="'.$question['dimension'].'_question_'.$question['id'].'" value="5"></th>';
        echo '<th class="text-center"><input type="radio" name="'.$question['dimension'].'_question_'.$question['id'].'" value="7.5"></th>';
        echo '<th class="text-center"><input type="radio" name="'.$question['dimension'].'_question_'.$question['id'].'" value="10"></th>';
        echo '</tr>';
        $dimensions = $question['dimension'];
    }
    echo '<tr>';
        echo '<th></th>';
        echo '<th>Kommentar/Ihre Anmerkung</th>';
        echo '<th class="text-center" colspan=5><textarea rows=3 cols=50 name="Kommentar"></textarea></th>';
        echo '</tr>';



    echo '<input type="hidden" name="gesamt" value="'.$i.'">';
    echo '<input type="hidden" name="dimensions" value="'.$dimensions.'">';
    echo '<input type="hidden" name="size" value="'.$_POST['size'].'">';    
    echo '<input type="hidden" name="branche" value="'.$_POST['branche'].'">';




}

Maybe I am missing something? I really hope someone can help me!

</div>
  • 写回答

1条回答 默认 最新

  • dongwei1921 2018-11-08 19:23
    关注

    I see two problems here:

    1. The form is submitting both 'back' and 'go' variables, regardless of which one was clicked.
    2. The PHP script is not tracking the $page variable across page loads. (If it is not tracked, it will re-initialize to 0 every time the page is loaded.)

    Try having your form tell the PHP script which page to load:

    <form action="functions.php" method="post">
      <button type="submit" name="page" value="<?php echo $page - 1; ?>">Zurück</button>
      <button type="submit" name="page value="<?php echo $page + 1; ?>">Weiter</button>
    </form>
    

    Then in your PHP, you would just check for the value of $_POST['page']

    if(!empty($_POST['page'])) {
        $page = intval($_POST['page']);
    } else {
        $page = 1;
    }
    

    You will also need to make the $page variable visible from the fda.php script into the function.php script. You could do that using global $page; next to your mysql variables. (That's not an optimal or clean way of doing things, but it would be the easiest addition to your code.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测