为什么我无法在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>
I see two problems here:
- The form is submitting both 'back' and 'go' variables, regardless of which one was clicked.
- 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.)