I have looked in quite a few places and cannot find the a really good example of what I need. I have a button that when submitted needs to pop up a form. I have done this in the past using the input=hidden name. However, I am using trying to use if statements instead of just a variable. I will give you the code that I have in hopes that you can help find out what exactly I am doing wrong. You can see that I am indeed using the hidden name for some but I really don't know how to incorporate that into the if statements.
if($row[status] == 0) { print "<input type='image' src='images/rework-ticket.png' alt='Rework' value='Rework' name='button' style='height:50px; width:50px; '>"; }
elseif($row[status] == "Rework (In Progress)") { print "<img src='images/rework-ticket.png' alt='Rework' style='height:50px; width:50px; '>"; }
print "<td align='center'>";
print "<form method='post' action='test2.php'>";
print "<input type='image' src='images/rework-ticket.png' alt='Add Rework Ticket' value='Enter Employee ID' name='button' style='height:50px; width:50px; '>";
print "<input type='hidden' name='proceed_to_rework' value='true'>";
print "<input type='hidden' name='invoice_number' value='$row[invoice_number]'>";
print "<input type='hidden' name='last_name' value='$row[last_name]'>";
print "<input type='hidden' name='status' value='$status'>";
print "Status:";
print "<td><select name='status' size='1'>";
if($status != NULL) { print "<option value='$status'>$status</option>"; }
if($status != "Parts Prep (In Progress)") { echo "<option value='Parts Prep (In Progress)'>Parts Prep (In Progress)</option>"; }
if($status != "Parts Prep (Complete)") { echo "<option value='Parts Prep (Complete)'>Parts Prep (Complete)</option>"; }
if($status != "Assembly (In Progress)") { echo "<option value='Assembly (In Progress)'>Assembly (In Progress)</option>"; }
if($status != "Assembly (Complete)") { echo "<option value='Assembly (Complete)'>Assembly (Complete)</option>"; }
if($status != "Finish (In Progress)") { echo "<option value='Finish (In Progress)'>Finish (In Progress)</option>"; }
if($status != "Finish (Complete)") { echo "<option value='Finish (Complete)'>Finish (Complete)</option>"; }
if($status != "Plumbing (In Progress)") { echo "<option value='Plumbing (In Progress)'>Plumbing (In Progress)</option>"; }
if($status != "Plumbing (Complete)") { echo "<option value='Plumbing (Complete)'>Plumbing (Complete)</option>"; }
print "</form>";
print "</td>";
I really appreciate any comments on how I can not only fix this code but improve this question.
You can use the if statement as follows.
<?php if (condition): ?>
<!-- HTML here -->
<?php if (anotherCondition): ?>
<!-- HTML here -->
<?php endif; ?>
<!-- HTML again -->
<?php if (anotherCondition): ?>
<!-- HTML here -->
<?php endif; ?>
<!-- HTML again -->
<?php endif; ?>
With this way, you don't have to use that much print statements.
Read the examples in php docs.
Instead of the if statements, use an array. This will also help if you want to add more options in the future. Also, instead of outputing HTML code line by line, try using one single echo statement.
If you want to retrieve the status value from POST, then use $status = $_POST['status']; before everything else
Be careful when writing $row[status] because you are using a constant. I have not modified it, but if you wanted $row[$status] instead, then add the $ before status.
if($row[status] == 0) {
echo '
<button type="button" id="Rework" >
<img src="images/rework-ticket.png" alt="Rework" style="height:50px; width:50px; "></img>
</button>';
} elseif($row[status] == "Rework (In Progress)") {
echo '
<button type="button" id="Rework" >
<img src="images/rework-ticket.png" alt="Rework" style="height:50px; width:50px; "></img>
</button>';
}
echo "
<td align='center'>
<form method='post' action='test2.php'>
<input type='image' src='images/rework-ticket.png' alt='Add Rework Ticket' value='Enter Employee ID' name='button' style='height:50px; width:50px; '>
<input type='hidden' name='proceed_to_rework' value='true'>
<input type='hidden' name='invoice_number' value='{$row[invoice_number]}'>
<input type='hidden' name='last_name' value='{$row[last_name]}'>
<input type='hidden' name='status' value='{$status}'>
Status:
<td><select name='status' size='1'>
";
$statusOptions = array(
NULL,
"Parts Prep (In Progress)",
"Parts Prep (Complete)",
"Assembly (In Progress)",
"Assembly (Complete)",
"Finish (In Progress)",
"Finish (Complete)",
"Plumbing (In Progress)",
"Plumbing (Complete)"
);
foreach($statusOptions as $option){
if($status != $option){
echo "<option value='{$option}'>{$option}</option>";
}
}
echo '</select>';
echo "</form>";
echo "</td>";
echo '
<script type="text/javascript">
function showDocument(){
document.getElementById("ReworkForm").style.visibility="visible";
}
window.onload = function(){
document.getElementById("ReworkForm").style.visibility="hidden";
document.getElementById("Rework").addEventListener("click", showDocument);
};
</script>
';
Related
So, I'm making a PHP page. I'm trying to get a url like this
https://nikkiedev.com/folder/indexfile/?schedule-btn=true&time-zone=CET
but instead, when I click on my GET form for the time zone it goes to
https://nikkiedev.com/folder/indexfile/?time-zone=CET
Here's my code:
<div class='mobile-center'>
<form method="GET">
<button name="schedule-btn" type="submit" value="true" class="btn">schedule</button>
<button name="roles-btn" type="submit" value="true" class="btn">roles</button>
</form>
<?php
$sched_btn = $_GET["schedule-btn"];
$roles_btn = $_GET["roles-btn"];
if (isset($sched_btn)) {
echo "<div class='mobile-center'>";
echo "<h1>Change schedule</h1>";
echo "<hr>";
echo "<p>Choose time zone</p>";
echo "<form method='GET'>";
echo "<p>
<input type='radio' name='time-zone' value='CET'> CET
<input type='radio' name='time-zone' value='EST'> EST
<input type='submit'>
</p>";
echo "</form>";
$timezone = $_GET["time-zone"];
echo "</div>";
if ($timezone == "CET") {
echo "<h1>CET</h1>";
}
else if ($timezone == "EST") {
echo "<h1>EST</h1>";
}
}
?>
</div>
Add the value of $sched_btn as a hidden input in the new form.
if (isset($sched_btn)) {
echo "<div class='mobile-center'>";
echo "<h1>Change schedule</h1>";
echo "<hr>";
echo "<p>Choose time zone</p>";
echo "<form method='GET'>";
echo "<input type='hidden' name='schedule-btn' value='$sched_btn'>";
echo "<p>
<input type='radio' name='time-zone' value='CET'> CET
<input type='radio' name='time-zone' value='EST'> EST
<input type='submit'>
</p>";
echo "</form>";
$timezone = $_GET["time-zone"];
echo "</div>";
if ($timezone == "CET") {
echo "<h1>CET</h1>";
}
else if ($timezone == "EST") {
echo "<h1>EST</h1>";
}
}
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a>  ";
echo "<input type='submit' name='acc' value='Accept'>   <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
}
Blockquote
//on submit here i need to disply corresponding $row['user_from']
value
Use <input type="hidden" name="whateveryouwant" />, if you don't want to display text field to user.
Try this:
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><input type='hidden' name='user_from' value='".$row['user_from']."' /><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a>  ";
echo "<input type='submit' name='acc' value='Accept'>   <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
echo $_POST['user_from'];//echo the value of user_form
}
<?php
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
?>
<form method="post" action="">
<?php $row['user_from'] ?>
<input type="submit" name="acc" value="Accept">
<input type="submit" name="cancel" value="Reject Rrquest">
</form>
<php
}
?>
<?php
if(isset($_POST['acc']) && !empty($_POST['yo']))
{
$link = $_POST['yo'];
// do what you want to do with this `url`
}
?>
NOTE: Don't Complex Your Code With Using Html COde In Php echo. You Can Just Open the While Loop Brackets { and then close the php ?> then simple html you have to written, So Just Avoid to used html code inside the php echo
I am attempting to create a single PHP page that opens a salesman or customer window based off a hidden input field in a form being posted. The tricky part for me is I also need two select option dropdowns that appear based on the same hidden input field being posted but ALSO retain their value after submit.
I know how to retain the values of a form like so..
<form action="" method="post">
<select name="newyear">
<option <?php if ($sqlyear == '2015cust') { ?>selected="true" <?php }; ?>value="2015cust">2015</option>
<option <?php if ($sqlyear == '2016cust') { ?>selected="true" <?php }; ?>value="2016cust">2016</option>
</select>
</form>
But when I try and implement this method inside my PHP if statements it does not retain the selected option.
if ($open == 'salesman_window'){
echo "This is salesman dropdown menu";
echo "$sqlyear";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='newyear_trigger' value=''>";
echo "<input type='hidden' name='window' value='salesman_window'>";
echo "<select name='newyear'>";
echo "<option if ($sqlyear == '2015users') { selected='true' }; value='2015users'> 2015</option>";
echo "<option if ($sqlyear == '2016users') { selected='true' }; value='2016users'>2016</option>";
echo "</select>";
echo "<button class='documentation_button'>Submit</button>";
echo "</form>";
}
if ($open == 'customer_window'){
echo "this is customer dropdown menu";
echo "$sqlyear";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='newyear_trigger' value=''>";
echo "<input type='hidden' name='window' value='customer_window'>";
echo "<select name='newyear'>";
echo "<option if ($sqlyear == '2015cust') { selected='true' }; value='2015cust'>2015</option>";
echo "<option if ($sqlyear == '2016cust') { selected='true' }; value='2016cust'>2016</option>";
echo "</select>";
echo "<button class='documentation_button'>Submit</button>";
echo "</form>";
}
?>
I have tried breaking up the option with multiple echo ""; and tried a few ".." of these in there as well but either get syntax errors or it just doesn't retain the selection. Everything works in regards to showing the correct dropdown menu based on what is posted but I just cannot get the select option to retain. The $sqlyear is for sure getting the correct value each time the dropdown option is selected so I know it isn't that either. Can anyone help?
A couple of options:
Use a ternary operator in your echo:
echo "<option" . (($sqlyear == '2015users') ? " selected='true'" : '')
. " value='2015users'>2015</option>";
echo "<option" . (($sqlyear == '2016users') ? " selected='true'" : '')
. " value='2016users'>2016</option>";
Keep using the if, but without opening/closing PHP tags as much:
<option <?php if ($sqlyear == '2015users') echo " selected='true'"; ?> value='2015users'>
2015</option>
<option <?php if ($sqlyear == '2016users') echo " selected='true'"; ?> value='2016users'>
2016</option>
I m trying to make a quiz.It's working but not giving the right result. On a correct answer for example answer 1 variable rans should be incremented by one but it is incrementing after submitting the 2nd question, that's why the value of the 10th current answer is not including in the total correct answer.
<?php
require_once("global.inc.php");?>
<form name="test" method="post" action="test.php">
<?php
$qid=(!isset($_POST['q_id'])) ? 0 : $_POST['q_id'];
$rans=(!isset($_POST["rans"])) ? 0 : $_POST["rans"];
$totalquestion=(!isset($_POST["totalquestion"])) ?
0 : $_POST["totalquestion"];
echo $rans;
if(isset($_POST["submit"]))
{
echo "<table align='center' style='border:1px solid silver' width='80%'
bgcolor='green'>";
echo "<tr><td>Total Question Attempt</td><td>",$totalquestion,"</td><tr>";
echo "<tr><td>Correct Answer</td><td>",$rans,"</td></tr>";
echo "<tr><td>Wrong Answer</td><td>",$totalquestion-$rans,"</td></tr>";
echo "<tr><td>Correct Answer Percentage</td> <td>",$rans/$totalquestion*100,"%</td></tr>";
echo "<tr><td>Wrong Answer Percenntage</td><td>",($totalquestion-$rans)/$totalquestion*100,"%</td></tr>";
echo "</table><br><br>";
$query="select * from questions,answers
where questions.q_id=answers.q_id";
echo "<table cellpadding='5px' align='center' style='border:1px
solid silver'>";
echo "<tr><th colspan='4' id='heading'>Online Quiz Test
Question</td></tr>";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>",$row['q_id'],"</td><td colspan='2'>",$row['question'],"</td></tr><tr><td></td>";
echo "<td colspan='2'>A. ",$row['opt1'],"</td>";
echo "<td colspan='2'>B. ",$row['opt2'],"</td></tr>";
echo "<tr><td></td><td colspan='2'>C. ",$row['opt3'],"</td>";
echo "<td colspan='1'>D. ",$row['opt4'],"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'>Correct option is ",strtoupper($row['correct_ans']),"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'><hr></td></tr>";
}
echo "</table>";
echo "<p align='right'><a href='#' onclick='window.print()'>Print</a></p>";
echo "<div style='visibility:hidden;display:none'>";
}
?>
<form name="test" method="post" action="test.php">
<?php
if(!isset($a))
{
$a=0;
//unset($_SESSION['score']);
}
if(isset($_POST['next'])) {
$a=$_POST['a'];
$totalquestion=$_POST['totalquestion'];
if(isset($_POST['rans']))
$rans=$_POST['rans'];
}
$sql1="SELECT * FROM questions,answers
where questions.q_id=answers.q_id limit 1 offset $a";
$result=mysql_query($sql1);
$num = mysql_num_rows($result);
echo "<form method='post' action=''>";
if($result) {
while ($row = mysql_fetch_array($result))
{
$qid = $row["q_id"];
$questions = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
$correct = $row["correct_ans"];
echo $rans;
?>
<p >Q.<?php echo $qid ?> <?php echo $questions;?></p>
<input type="radio" value="<?php echo $opt1;?>" name="choice"/><?php echo $opt1;?> <br/>
<input type="radio" value="<?php echo $opt2;?>" name="choice"/><?php echo $opt2;?><br/>
<input type="radio" value="<?php echo $opt3;?>" name="choice"/><?php echo $opt3;?><br/>
<input type="radio" value="<?php echo $opt4;?>" name="choice"/><?php echo $opt4;?><br/>
<input type="hidden" value="$answer" name="rightanswer[$qid]"/>
<?php
$b=$a+1;
$sql2="SELECT * FROM questions where q_id=$qid-1 ";
$result2=mysql_query($sql2);
while ($row2 = mysql_fetch_array($result2)) {
$ans=$row2['correct_ans'];
}
if(isset($_POST['choice'])) {
if($ans==$_POST['choice']){
//echo "<input type='hidden' name='rans' value='".($rans+1). "'>";
$rans=$rans+1;
}
else {
//echo "<input type='hidden' name='rans' value='" . $rans . "'>";
$rans=$rans;
}
}
//$query="select correct_ans from questions where q_id='$qid'";
//$result=mysql_query($query);
//while ($row = mysql_fetch_array($result)) {
//echo $row['correct_ans'];
echo "<input type='hidden' value='$b' name='a'>";
echo "<input type='hidden' value='count' name='count'>";
echo "<input type='hidden' name=qid value='$qid'>";
echo "<input type='hidden' name='totalquestion' value='".$totalquestion+1)."'>";
echo "<input type='hidden' name='rans' value='" . $rans . "'>";
echo "<input type='submit' name='next' value='next'> ";
echo "<input type='submit' name='submit' value='submit'><br><br>";
echo "</form>";
}
}
?>
Okay, your code is a bit of a mess.
You have random tautologies (like the $rans=$rans; which does absolutely nothing. If the answerer clicks "next" you're assigning $totalquestion twice. Definitely take a good, hard look and refactor this page.
But the answer to your question is probably because you're checking to see if they entered in the right answer at the bottom of the code -- after you've presented the results or the next question.
You've utilized the scripting capabilities of PHP without touching on any functions so it will evaluate top to bottom.
I'd move everything around: Move the handler for "next" to the top, underneath your default variable assignments, then put the check for right answer underneath that, then do the presentation of the next question, then the "submit"handler.
I'd break up the different units into functions for readability and reusability, also. For example, make a function to print out the specified question, make another one to validate the user entered in the right answer.
I've got a form with 50 questions. After the user has filled in the form, I want to take them to another page to first cancel / confirm that they are finished with the form. My question is if they select cancel, how do I put back all the fields that has been answered in the form?
EDITED
I've edited my question to show my code because I'm strugling:
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY RAND()";
$result1=mysql_query($sql1);
echo "<form method='post' action='....'>";
while($row1 = mysql_fetch_array($result1))
{
$test_name=$row1['test_name'];
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
$option3=$row1['option3'];
echo "$question";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='radio' name='question[$q_nr]' value='C'>$option3<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='C'>$option3<BR>";
} else {
echo ''; }
} //end else if q_type <> mr
echo "<BR>";
echo "<BR>";
echo "</p>";
} //end while row1
echo "<input type='submit' value='Finish'>";
echo "</form>";
Make the Cancel button a Submit and let it post back to the original questionnaire. In there, you can do like this:
<input type="text" name="q1" value="<?=isset($_POST['q1']) ? $_POST['q1'] : '';?>" />
Or:
<input type="radio" name="q2" value="A" <?=isset($_POST['q2']) && $_POST['q2'] == 'A' ? 'checked="checked"' : '';?>" />
<input type="radio" name="q2" value="B" <?=isset($_POST['q2']) && $_POST['q2'] == 'B' ? 'checked="checked"' : '';?>" />
Same goes for option elements inside a select (selected="selected").
Pass the $_POST or $_GET array to the confirmation page, and if they hit cancel, pass the array back to the page, and fill in the elements.
The easiest solution would be adding:
<INPUT type=button value="Cancel" onClick="history.back();">
When you go back, the form would still be there.
You can just use $_REQUEST['Answer'] in value tag of form.