php not following functions structure [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Having trouble with functions structure not following (PHP)
Problem : Every time I am submitting the answerSubmit button, instead of displaying the echo student answers in the the StudentAnswers() function, it goes back up to the pickSession() function.
I have a demo here you can use to see what is happening: DEMO
When you open demo, select an assessment from assessment (session) drop down menu. You will see another 2 drop down menus students and questions drop down menu appear. Now if you click on the answerSubmit button (Get Students Answers button), then it should display echo stating student answers, but instead you can see it goes back to only displaying the assessment drop down menu. Why is this?
Below is the code for the demo:
function PickSession()
{
//mysqli code retreiving data for options in assessments drop down menu goes here ....
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
<strong>Asessments:</strong>
<select name="session" id="sessionsDrop">
<option value="">Please Select</option>
<?php
while ( $sessionqrystmt->fetch() ) {
$sv = $dbSessionId;
if(isset($_POST["session"]) && $sv == $_POST["session"])
echo "<option selected='selected' value='$sv'>" .
$dbSessionName .
" - " .
date('d-m-Y',strtotime($dbSessionDate)) .
" - " .
date('H:i',strtotime($dbSessionTime)) .
"</option>" . PHP_EOL;
else
echo "<option value='$sv'>" .
$dbSessionName .
" - " .
date('d-m-Y',strtotime($dbSessionDate)) .
" - " .
date('H:i',strtotime($dbSessionTime)) .
"</option>" . PHP_EOL;
}
?>
</select>
</p>
<input id="sessionSubmit" type="submit" value="Submit Assessments" name="sessionSubmit" />
</form>
<?php
}
function SessionIsSubmitted()
{
if(isset($_POST['sessionSubmit'])) // we have subbmited the second form
{
if(empty($_POST["session"])) // We picked the "Please select" option
{ ?>
Please Select an Assessment
<?php
return false;
}
else // All is ok
{
return true;
}
}
return false;
}
function ShowAssessment()
{
//mysqli code retreiving data for options in students drop down menu goes here ....
if($studentnum == 0){ ?>
There are no Students who have currently taken this Assessment
<?php } else {
//mysqli code retreiving data for options in questions drop down menu goes here ....
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
<strong>Student:</strong>
<select name="student" id="studentsDrop">
<option value="All">All</option>
<?php
while ( $currentstudentstmt->fetch() ) {
$stu = $dbStudentId;
if(isset($_POST["student"]) && $stu == $_POST["student"])
echo "<option selected='selected' value='$stu'>" .
$dbStudentAlias . " - " . $dbStudentForename . " " .
$dbStudentSurname . "</option>" . PHP_EOL;
else
echo "<option value='$stu'>" . $dbStudentAlias . " - " .
$dbStudentForename . " " . $dbStudentSurname .
"</option>" . PHP_EOL;
}
?>
</select>
</p>
<p>
<strong>Question:</strong>
<select name="question" id="questionsDrop">
<option value="All">All</option>
<?php
while ( $questionsstmt->fetch() ) {
$ques = $dbQuestionId;
if(isset($_POST["question"]) && $ques == $_POST["question"])
echo "<option selected='selected' value='$ques'>" .
$dbQuestionNo . "</option>" . PHP_EOL;
else
echo "<option value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<input id="answerSubmit" type="submit" value="Get Student's Answers" name="answerSubmit" />
</form>
<?php
}
}
function StudentAnswersIsSubmitted()
{
if(isset($_POST['answerSubmit'])) // we have subbmited the second form
{
return false;
}
return false;
}
function StudentAnswers()
{
echo "student answers";
}
?>
...
<body>
<?php
PickSession(); // Show the thing to pick session
if(SessionIsSubmitted()) // When session is picked
{
ShowAssessment(); // Show students and questions information
if(StudentAnswersIsSubmitted()) // Student Answers button is submitted
{
StudentAnswers();
}
}
?>
</body>
UPDATE:
function PickSession()
{
//mysqli code retreiving data for options in assessments drop down menu goes here ....
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
<strong>Asessments:</strong>
<select name="session" id="sessionsDrop">
<option value="">Please Select</option>
<?php
while ( $sessionqrystmt->fetch() ) {
$sv = $dbSessionId;
if(isset($_POST["session"]) && $sv == $_POST["session"])
echo "<option selected='selected' value='$sv'>" . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "</option>" . PHP_EOL;
else
echo "<option value='$sv'>" . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<input id="sessionSubmit" type="submit" value="Submit Assessments" name="sessionSubmit" />
</form>
<?php
}
function SessionIsSubmitted()
{
if(isset($_POST["session"]) && empty($_POST["session"])) // We picked the "Please select" option
{ ?>
<div class="red">
Please Select an Assessment
</div>
<?php
return false;
}
else if(!isset($_POST["session"]))
{
return false;
}
else // All is ok
{
return true;
}
return false;
}
function ShowAssessment()
{
//mysqli code retreiving data for options in students drop down menu goes here ....
if($studentnum == 0){ ?>
There are no Students who have currently taken this Assessment
<?php } else {
//mysqli code retreiving data for options in questions drop down menu goes here ....
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
<input type="text" name="session" value="<?php echo $_POST['session']; ?>">
<strong>Student:</strong>
<select name="student" id="studentsDrop">
<option value="All">All</option>
<?php
while ( $currentstudentstmt->fetch() ) {
$stu = $dbStudentId;
if(isset($_POST["student"]) && $stu == $_POST["student"])
echo "<option selected='selected' value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
else
echo "<option value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<p>
<strong>Question:</strong>
<select name="question" id="questionsDrop">
<option value="All">All</option>
<?php
while ( $questionsstmt->fetch() ) {
$ques = $dbQuestionId;
if(isset($_POST["question"]) && $ques == $_POST["question"])
echo "<option selected='selected' value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
else
echo "<option value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<input id="answerSubmit" type="submit" value="Get Student's Answers" name="answerSubmit" />
</form>
<?php
}
}
function StudentAnswersIsSubmitted()
{
if(!isset($_POST["answerSubmit"]))
{
return false;
}
else // All is ok
{
return true;
}
return false;
}
?>
UPDATE 2:
HERE IS DEMO FOR CODE BELOW: DEMO
function PickModule()
{ ?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<?php
$moduleactive = 1;
$sql = "SELECT ModuleId, ModuleNo, ModuleName FROM Module WHERE ModuleActive = ? ORDER BY ModuleNo";
//mysqli code for modules drop down menu
?>
<strong>Module:</strong>
<select name="module" id="modulesDrop">
<option value="">Please Select</option>
<?php
while($sqlstmt->fetch()) {
$ov = $dbModuleNo . "_" . $dbModuleName . "_" . $dbModuleId;
if(isset($_POST["module"]) && $ov == $_POST["module"])
echo "<option selected='selected' value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL;
else
echo "<option value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL;
}
?>
</select>
<br>
<input id="moduleSubmit" type="submit" value="Submit Module" name="moduleSubmit" />
</form>
<?php }
function ModuleIsSubmitted()
{
if(isset($_POST["module"]) && empty($_POST["module"])) // We picked the "Please select" option
{ ?>
Please Select a Module
<?php
return false;
}
else if(!isset($_POST["module"]))
{
return false;
}
else // All is ok
{
return true;
}
return false;
}
function PickSession()
{
$dataTransfered = explode( "_" , $_POST["module"] );
$moduleNo = $dataTransfered[0];
$moduleName = $dataTransfered[1];
$moduleId = $dataTransfered[2];
//Get data from database
$sessionquery = "
SELECT s.SessionId, SessionName, SessionDate, SessionTime, ModuleId, SessionActive, Complete
FROM Session s
INNER JOIN Session_Complete sc ON sc.SessionId = s.SessionId
WHERE
(ModuleId = ? AND Complete = ?)
ORDER BY SessionName
";
$complete = 1;
//mysqli code for assessments drop down menu
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="hidden" name="module" value="<?php echo $_POST['module']; ?>">
<p>
<strong>Selected Module: </strong><?php echo $moduleNo ." - ". $moduleName; ?>
</p>
<?php if ($sessionnum == 0 ){ ?>
<div class="red">
Sorry, You have No Assessments under this Module
</div>
<?php } else { ?>
<p>
<strong>Asessments:</strong>
<select name="session" id="sessionsDrop">
<option value="">Please Select</option>
<?php
while ( $sessionqrystmt->fetch() ) {
$sv = $dbSessionId;
if($dbSessionActive == 0){
$class = 'red';
}else{
$class = 'green';
}
if(isset($_POST["session"]) && $sv == $_POST["session"])
echo "<option selected='selected' value='$sv' class='$class'>" . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "</option>" . PHP_EOL;
else
echo "<option value='$sv' class='$class'>" . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<input id="sessionSubmit" type="submit" value="Submit Assessments" name="sessionSubmit" />
</form>
<?php
}
}
function SessionIsSubmitted()
{
if(isset($_POST["session"]) && empty($_POST["session"])) // We picked the "Please select" option
{ ?>
<div class="red">
Please Select an Assessment
</div>
<?php
return false;
}
else if(!isset($_POST["session"]))
{
return false;
}
else // All is ok
{
return true;
}
return false;
}
function ShowAssessment()
{
$studentactive = 1;
$currentstudentqry = "
SELECT
st.StudentId, st.StudentAlias, st.StudentForename, st.StudentSurname
FROM
Student_Session ss
INNER JOIN
Student st ON ss.StudentId = st.StudentId
WHERE
(ss.SessionId = ? AND st.Active = ?)
ORDER BY st.StudentAlias
";
//mysqli code for students drop down menu
if($studentnum == 0){ ?>
<div class="red">
There are no Students who have currently taken this Assessment
</div>
<?php } else {
$questionsqry = "
SELECT
QuestionId, QuestionNo
FROM
Question
WHERE
(SessionId = ?)
ORDER BY QuestionNo
";
//mysqli code for questions drop down menu
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
<input type="text" name="session" value="<?php echo $_POST['session']; ?>">
<strong>Student:</strong>
<select name="student" id="studentsDrop">
<option value="All">All</option>
<?php
while ( $currentstudentstmt->fetch() ) {
$stu = $dbStudentId;
if(isset($_POST["student"]) && $stu == $_POST["student"])
echo "<option selected='selected' value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
else
echo "<option value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<p>
<strong>Question:</strong>
<select name="question" id="questionsDrop">
<option value="All">All</option>
<?php
while ( $questionsstmt->fetch() ) {
$ques = $dbQuestionId;
if(isset($_POST["question"]) && $ques == $_POST["question"])
echo "<option selected='selected' value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
else
echo "<option value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<input id="answerSubmit" type="submit" value="Get Student's Answers" name="answerSubmit" />
</form>
<?php
}
}
function StudentAnswersIsSubmitted()
{
if(!isset($_POST["answerSubmit"]))
{
return false;
}
else // All is ok
{
return true;
}
return false;
}
function StudentAnswers()
{
echo "student answers";
}
?>
.......
<?php
PickModule(); // Show the thing to pick module
if(ModuleIsSubmitted()) // When module is picked
{
PickSession(); // Show the thing to pick session
if(SessionIsSubmitted()) // When session is picked
{
ShowAssessment(); // Show students and questions information
if(StudentAnswersIsSubmitted()) // Student Answers button is submitted
{
StudentAnswers();
}
}
}
?>

When you submit the second form, the SessionIsSubmitted function doesn't pass because the second form on your page doesn't contain the information needed (sessionSubmit). The sessionSubmit is inside of the first form only and therefore does not get submitted.
You need to have something in your second form that allows you to pass the first form test

Related

How to show a selected value in a dynamic drop down in PHP

I am trying to retain the value selected in a drop down menu. Everything is working, but I don't know how to show and retain the selected value. How can I do this?
I've got this working using another way:
<?php if($_POST['selClass'] == $row1['class']) echo 'selected="selected"' ?>
but this leads to other problems, i.e. a blank option in my drop down menu.
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
You can approach this as
<?php
$selectedOption = '';
if($_POST){
$selectedOption = $_POST['selClass'];
}
?>
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($row1["class"] == $selectedOption)
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
else
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
There is two option you can choose whichever you feel ease.
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($_POST['selClass'] == $row1['class']){
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
}else{
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
}
?>
OR
<?php
$selectedClass = $_POST['selClass'];
while ($row1 = mysqli_fetch_array($rs5)) { ?>
<option value="<?php echo $row1['çlass']?>" <?php if(selectedClass == $row1['class']) { echo "selected='selected'"; }?> ><?php echo $row1['class']?></option>
<?php } ?>

how to query a value and add it to the date

I have a problem, you see I borrow a book getting the book_id and the date borrow is set to the date today, but each book has a days_id value that I need to add to the day of the borrow_date now I'm having trouble how can I get that date_id from the book_id I chosen and add it inside my borrow_date so that I can generate my due_date automatically
here is my PHP code
<?php
include 'connect.php';
$librarian_id = $_POST['librarian_id'];
$member_id = $_POST['member_id'];
$book_id = $_POST['book_id'];
$date_borrow = $_POST['date_borrow'];
$status_id = $_POST['status_id'];
$days = "SELECT days_id, book_id FROM book_setup
WHERE book_id = '$book_id' AND days_id = " . $row['book_id'] . " ";
$result = mysqli_query($conn, $days);
$due_date = date('Y-m-d', strtotime($date_borrow. ' + $result days'));
if ($librarian_id == '') {
echo "librarian is empty";
}
else if ($member_id == '') {
echo "member id is empty";
}
else if ($book_id == '') {
echo "book is empty";
}
else if ($date_borrow == '') {
echo "date borrow is empty";
}
else if ($status_id == '') {
echo "status is empty";
}
else if ($due_date == '') {
echo "due date is empty";
}
else {
$sql = " INSERT INTO borrow_book (librarian_id, member_id,
book_id, date_borrow, due_date, status_id)
VALUES
('$librarian_id', '$member_id', '$book_id',
'$date_borrow', '$due_date', '$status_id' ) ";
if ($conn->query($sql) === TRUE)
{
$message = "Borrowing book successful";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else
{
$message = "Borrowing book failed Failed";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
$conn->close();
?>
and this is my HTML code
<?php
include 'php/header.php';
require_once 'php/connect.php';
?>
<form method="POST" action="php/borrow.php">
<h1>Borrow Book</h1>
Transacted by:
<br>
<div>
<select name="librarian_id">
<option>Select Librarian
<?php
$sql = "SELECT * FROM librarian";
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['librarian_id'] ."'>"
. $row['Fname'] . " " . $row['Lname'] . "</option>";
}
?>
</option>
</select><br><br>
</div>
Select Member
<br>
<div>
<select name="member_id">
<option>Select member
<?php
$sql = "SELECT * FROM members";
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['member_id'] ."'>"
. $row['Fname'] . " " . $row['Lname'] . "</option>";
}
?>
</option>
</select><br><br>
</div>
<div>
<select name="book_id">
<option>Select a Book
<?php
$sql = "SELECT book_setup.book_id, book.book_id, book.title FROM book_setup
INNER JOIN book ON book_setup.book_id = book.book_id WHERE book_setup.status_id = '1' ";
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['book_id'] . "'>"
. $row['title'] . "</option>";
}
?>
</option>
</select><br><br>
</div>
Borrow Date:
<br><br>
<input name="date_borrow" value="<?php echo date('Y-m-d'); ?>" readonly>
<br><br>
Status:
<div>
<select name="status_id">
<option>
<?php
$sql = "SELECT * FROM borrower_status";
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['status_id'] ."'>"
. $row['status'] . "</option>";
}
?>
</option>
</select><br><br>
</div>
<br>
<br>
<button class="button" type="submit">Submit</button>
<button type="reset" value="Reset">Reset</button>
</form>
this is my database for borrowing book
and this is where I get the book that is available to borrow
Do this:
$res = mysqli_query($con, " SELECT days_id FROM book_setup WHERE book_id = '$book_id' ");
$days = mysqli_fetch_assoc($res)['days_id'];

PHP (simple): Variable always assigning to the first selection of select box form. [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 7 years ago.
I'm very new to programming. I'm trying to get variables out of a selection box, the two text input boxes work ok. When I submit the form the red cat will always appear even if other options were selected.
<form name="cat" action="" method="post">
Name of Cat: <input class="inputbox" type="text" name="name" value="" /><br />
Color: <select name="color">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="black">Black</option>
</select> <br>
Weight: <input class="inputbox" type="number" name="weight" value="" /><br />
<input type="submit" class="submit" name="submit" value="Submit" />
</form>
<br>
<br>
<?php
if ($_POST['submit']) {
$name = $_POST['name'];
$color = $_POST['color'];
$weight = $_POST['weight'];
if ($color = 'red') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/9/7/D/t/L/K/red-cat-md.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color = 'blue') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/L/z/H/T/d/d/blue-cat-md.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color = 'yellow') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/O/Y/K/K/u/r/yellow-cat-hi.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color = 'black') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://content.mycutegraphics.com/graphics/halloween/cute-halloween-black-cat.png'></a><br>" . $weight . " (kg)<br>";
} else {
//do nothing
}
}
You are using a single equals sign which is used for assigning a variable:
$variable = 'foo';
You need double equals signs when performing a conditional statement:
if ($variable == 'foo') {
Error in your condition: if ($color = 'red'), try my code below
if ($_POST['submit']) {
$name = $_POST['name'];
$color = $_POST['color'];
$weight = $_POST['weight'];
if ($color == 'red') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/9/7/D/t/L/K/red-cat-md.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color == 'blue') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/L/z/H/T/d/d/blue-cat-md.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color == 'yellow') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/O/Y/K/K/u/r/yellow-cat-hi.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color == 'black') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://content.mycutegraphics.com/graphics/halloween/cute-halloween-black-cat.png'></a><br>" . $weight . " (kg)<br>";
} else {
//do nothing
}
Please try to add this code to your so that if you have selected some option and submit then the page will show what you have selected on load
<select name="color" value="<?php echo $_POST['color']; ?>">

Option get data from DB

I have got this table in my Database:
**train_information**
train_id
country_id
user_id
train_name
tare_weight
number_of_bogies
number_of_axles
wheel_diameter_min
wheel_diameter_max
And then i have 2 .php pages (1 is only for classes)
Selector.php
<!--Selector-->
<form name='form' id='selector'>
<?php
$selector = $database->selector();
?>
<select onchange= "mySelection()" id ="selector" name="selector">
<option selected="true" disabled="disabled">Selecteer een trein</option>
<?php
foreach ($selector as $selector) {
print "<h3>" . $selector['train_id'] . "==" . $selector_id . "</h3>";
if ($selector['train_id'] == $selector_id) {
echo "<option value=" . $selector['train_id'] . " selected='selected'> " . $selector['train_name'] . "</option>";
} else {
echo "<option value=" . $selector['train_id'] . "> " . $selector['train_name'] . "</option>";
} }
?>
</select>
</form>
And then the class.php
function selector() {
$sql = "SELECT train_id, train_name FROM train_information";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
The selector shows the train names in the selection. so that is good.
But i want that when i select it, and press a button like: Select. That it select the information of that selected train and shows it on the page.
And i don't only want to show 1, but if i would like to select a other train after that, it shows both trains information on the page.
How do i do this?
You can do following
Add this Method to class.php
function select_train_details($trainId) {
$sql = "SELECT * FROM train_information where train_id = " . $trainId;
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
Add Following to selector.php
<form name='form' id='selector'>
<?php
$selector = $database->selector();
if (isset($_POST['selector'])) {
$selectorDetail = $database->select_train_details();
}
?>
<select onchange= "this.form.submit" id ="selector" name="selector">
<option selected="true" disabled="disabled">Selecteer een trein</option>
<?php
foreach ($selector as $selector) {
print "<h3>" . $selector['train_id'] . "==" . $selector_id . "</h3>";
if ($selector['train_id'] == $selector_id) {
echo "<option value=" . $selector['train_id'] . " selected='selected'> " . $selector['train_name'] . "</option>";
} else {
echo "<option value=" . $selector['train_id'] . "> " . $selector['train_name'] . "</option>";
}
}
?>
</select> </form> <?php if (isset($selectorDetail)) {
echo "<pre>";
$selectorDetail; } ?>

Waiting for UPDATE to finish before refreshing page

I have an HTML form that update a table in MySQL. Unfortunatly, my page reload with the pre-updated data from MySQL. Is there a way to tell mysqli in PHP to wait for UPDATE to complete before reloading (and doing the SELECT again) the page?
Thanks!
(edit: adding portion of code)
$mysql = mysqli_connect($host,$username,$password,$dbname);
$series_mysql = $mysql->query("SELECT Name, Path, Language FROM subtitles_config");
$series_list = array();
while($serie = mysqli_fetch_array($series_mysql))
{
array_push($series_list,array($serie["Name"],$serie["Path"],$serie["Language"]));
}
sort($series_list);
if ($series_list != Null)
{
echo "<form action='?' method='post'><table><tr><th>Nom</th><th>Sous-titres</th></tr>\n\r";
foreach ($series_list as $serie)
{
echo "<tr><td><input type=text size=40 readonly name='row[]' value='" . htmlspecialchars($serie[0], ENT_QUOTES) . "'><input type='hidden' name='row[]' value='" . htmlspecialchars($serie[1], ENT_QUOTES) . "'</td><td>";?>
<select name='row[]'>
<option value="aucun"<?php if ($serie[2] == "aucun"): ?> selected="selected"<?php endif; ?>>aucun</option>
<option value="fr"<?php if ($serie[2] == "fr"): ?> selected="selected"<?php endif; ?>>fr</option>
<option value="en"<?php if ($serie[2] == "en"): ?> selected="selected"<?php endif; ?>>en</option>
<option value="fr,en"<?php if ($serie[2] == "fr,en"): ?> selected="selected"<?php endif; ?>>fr,en</option>
</select><?php
echo "</td></tr>\n\r";
}
echo "</table><input type='submit' name='update' value='Mettre à jour' /></form>\n\r";
}
if ( isset( $_POST['update']))
{
foreach (array_chunk($_POST['row'],3) as $serie)
{
$mysql->query("UPDATE subtitles_config SET Language='" . $serie[2] . "' WHERE Path='" . addslashes($serie[1]) . "'");
}
}
<?php
$mysql = mysqli_connect($host,$username,$password,$dbname);
if ( isset( $_POST['update'])) {
foreach (array_chunk($_POST['row'],3) as $serie) {
$mysql->query("UPDATE subtitles_config SET Language='" . $serie[2] . "' WHERE Path='" . addslashes($serie[1]) . "'");
}
}
$series_mysql = $mysql->query("SELECT Name, Path, Language FROM subtitles_config");
$series_list = array();
while($serie = mysqli_fetch_array($series_mysql)) {
array_push($series_list,array($serie["Name"],$serie["Path"],$serie["Language"]));
}
sort($series_list);
if ($series_list != Null) {
echo "<form action='?' method='post'><table><tr><th>Nom</th><th>Sous-titres</th></tr>\n\r";
foreach ($series_list as $serie) {
echo "<tr><td><input type=text size=40 readonly name='row[]' value='" . htmlspecialchars($serie[0], ENT_QUOTES) . "'><input type='hidden' name='row[]' value='" . htmlspecialchars($serie[1], ENT_QUOTES) . "'</td><td>";
?>
<select name='row[]'>
<option value="aucun"<?php if ($serie[2] == "aucun"): ?> selected="selected"<?php endif; ?>>aucun</option>
<option value="fr"<?php if ($serie[2] == "fr"): ?> selected="selected"<?php endif; ?>>fr</option>
<option value="en"<?php if ($serie[2] == "en"): ?> selected="selected"<?php endif; ?>>en</option>
<option value="fr,en"<?php if ($serie[2] == "fr,en"): ?> selected="selected"<?php endif; ?>>fr,en</option>
</select>
<?php
echo "</td></tr>\n\r";
}
echo "</table><input type='submit' name='update' value='Mettre à jour' /></form>\n\r";
}
Try that.
Your issue was that you were updating everything AFTER you pulled the data for the fields.

Categories