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>
Related
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>
';
How can I display that when select value is null I want to display input field to enter customer own choice. I am new to php so anyone can please suggest me how to do it? Here is my reference code.
<select class="form-control" name="outlet">
<option>Select Outlet</option>
<?php
$values = explode(",",$camp_outlet);
foreach(explode(",",$camp_outlet) as $value){
?>
<option value="<?php echo "$value";?>">
<?php echo "$value";?>
</option>
<?php
}
?>
<?php
$theData = explode(",",$camp_outlet);
if (count($theData) > 0 ){
echo "<select class=\"form-control\" name=\"outlet\">";
echo "<option>Select Outlet</option>";
foreach($theData as $value){ // loop through the data and add the options to the select input
echo "<option value=\"$value\">$value</option>";
}
echo "</select>";
} else {
echo "<input type=\"text\" class=\"form-control\" name=\"outlet\">";
}
?>
I am using the following code to select a variable for a query. I need a value/variable to be automatically selected when the page loads.
$ID_SOCIEDAD = $_POST['Country'];
echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >";
echo "<select name='Country' >";
while($row = mysql_fetch_array($result))
{
echo " <option selected value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>";
}
echo " </select>
<input type='submit' value='Filter' />";
echo "</form>";
use this code
<?=($_POST['country']== $row['Fund_Manager_Company_Code']) ?"selected='selected'" : "" ?>
or
echo ($_POST['country']== $row['Fund_Manager_Company_Code']) ?"selected='selected'" : "";
I need your advice. I fetch data from database to table: ID, Name.
In table are Actions: Delete, Enable, Block. When action Delete
is selected, I would like, that respectively record will be deleted.
However, my script does not work and always delete last record, even I select
another record. I think problem is, that select name and hidden input
name is similar for all records. But I can not find way, how to create
them with different names.
Any advice is welcome.
HTML:
<form method='post'>
<table border='1'>
<tr>
<th> ID </th>
<th> Name </th>
<th> Action </th>
</tr>
Code:
$db = new PDO('mysql:host=localhost;dbname=****;charset=utf8', '**', '**');
$query = $db->query("SELECT ID,statusas,login,vardas,email FROM users");
while($row = $query->fetch(PDO::FETCH_BOTH)) {
echo "<tr><input type='hidden' name='id' value='".$row[0]."'>";
echo "<td>".$row[0]."</td>";
//echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
//echo "<td>".$row[3]."</td>";
// echo "<td>".$row[4]."</td>";
echo "<td><select name='action'>
<option value='choose'>Choose..</option>
<option value='delete'> Delete </option>
<option value='enable'> Enable </option>
<option value='block'> Block</option>
</select></td>";
echo "</tr>";
}
echo "<br><input type='submit' name='submit'></table></form>";
if($_POST['submit']) {
if ($_POST['action']== 'delete') {
echo $_POST['id']; // delete query, but now I am just checking if I get a proper ID.
}
}
else {
echo "bad";
}
You are using the same name attribute on every row in the form, so they are being overridden and it's using the last one.
What you could do is either wrap every row in its own form, or you could do something like this, and have only 1 submit button to execute the action on every row:
// remove hidden id element
...
echo "<td><select name='action[" . $row[0] . "]'>"
...
// remove submit button in the loop, but add it after the while loop
...
if (isset($_POST['action']))
{
foreach ($_POST['action'] as $id => $action)
{
if ($action !== 'choose')
{
// do action on the id;
echo $id . " -> " . $action . "<br>";
}
}
}
You put every ID in the form, which results in the CGI to send something like this: id=1, id=2, id=3, etc. PHP then only reads the last ID and deletes that.
To fix it, give each row its own form.
while($row = $query->fetch(PDO::FETCH_BOTH)) {
echo "<form method='post'>";
echo "<tr><input type='hidden' name='id' value='".$row[0]."'>";
echo "<td>".$row[0]."</td>";
//echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
//echo "<td>".$row[3]."</td>";
// echo "<td>".$row[4]."</td>";
echo "<td><select name='action'>
<option value='choose'>Choose..</option>
<option value='delete'> Delete </option>
<option value='enable'> Enable </option>
<option value='block'> Block</option>
</select></td>";
echo "</tr>";
echo "</form>";
}
echo "<br><input type='submit' name='submit'></table>";
I have two comboboxes which are generated by queries from mysql.
When user choose value in first combobox i need to display second combobox, but if nothing is choosen second combox should not be displayed.
The code:
<?php
echo "<form method= \"post\" name=\"formcombo\" action=''>";
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Choose main category</option>";
while($cat2 = mysql_fetch_array($query2)) {
if($cat2['category_id']==#$category)
{
echo "<option value='$cat2[category_id]'>$cat2[category_name]</option></br>";}
else
{
echo "<option value='$cat2[category_id]'>$cat2[category_name]</option>";
}
}
echo "</select></br>";
echo "<select name='subcat'><option value=''>Choose subcategory</option>";
while($cat = mysql_fetch_array($query1)) {
echo "<option value='$cat[subcat_name]'">$cat[subcat_name]</option>";
}
echo "</select>";
echo "<input type=\"submit\" value =\"Submit\">";
echo "</form>";
?>
I use javascript to generate combobox values:
<script type="text/javascript">
function reload(form){
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='main.php?cat=' + val ;
}
</script>
If you are okay with jQuery js library, it is easy.
see this jQuery plugin http://plugins.jquery.com/project/selectchain
try the demo provided in this page and use the plugin to accomplish.