How do I echo selected items in a do- while loop? - php

I am beginner in PHP. In edit section I have defined a Do-While loop to get the information from data base. But when the user choose from the drop-down and submit
the selected item disappears and fist option appears on drop-down. How Can I retrieve and echo selected items after submitting.
<?php
$cat_sql= "SELECT * FROM cars";
$cat_sql_query=mysql_query($cat_sql);
$cat_sql_row=mysql_fetch_assoc($cat_sql_query);
?>
<form action="MSPEditstock.php" method="POST">
<span style="font-size: 18px; margin:10px;">Please Choose your car from the dropdown:</span>
<select id="sortmethod" name="editstock" >
<?php do{?>
<option value="<?php echo $cat_sql_row['id']; ?>" > <?php echo $cat_sql_row['Brand'].$cat_sql_row['Model'];?> </option>
<?php ; }while($cat_sql_row=mysql_fetch_assoc($cat_sql_query))?>
</select>
<input type="submit" value="Start Editting" class="fvisual">
</form>

If I understood correctly you wish for the value selected by a user before submitting the form to be selected in the dropdown menu once the page has reloaded? Hopefully the following will give an idea as to how you could achieve that.
<?php
$cat_sql= "SELECT * FROM cars";
$cat_sql_query=mysql_query( $cat_sql );
$cat_sql_row=mysql_fetch_assoc( $cat_sql_query );
$editstock=!empty( $_POST['editstock'] ) ? $_POST['editstock'] : false;
?>
<form action="MSPEditstock.php" method="POST">
<span style="font-size: 18px; margin:10px;">Please Choose your car from the dropdown:</span>
<select id="sortmethod" name="editstock" >
<?php
while( $cat_sql_row=mysql_fetch_assoc( $cat_sql_query ) ){
$id=$cat_sql_row['id'];
$brand=$cat_sql_row['Brand'];
$model=$cat_sql_row['Model'];
$selected=$editstock && $editstock==$id ? ' selected ' : '';
echo "<option value='{$id}'{$selected}>{$brand}{$model}";
}
?>
</select>
<input type="submit" value="Start Editting" class="fvisual">
</form>

Related

keep selected values in select box field after search

I want to keep the selected values in select box after search .This is a vehicle search filter page and Page is coded using php and connected with database.
Database have 2 columns for both options that is driver age for 21-24 and driver_age for 25+ as some vehicles also available for both age of drivers
code below
<script>
$('#driver').change(function() {
if($('#driver option:selected').val() == '21-24') {
$('#driver').attr('name','driverage');
}
if($('#driver option:selected').val() == '25+') {
$('#driver').attr('name','driver_age');
}
});
</script>
<form id="myform" method="GET" action="">
<div class="fields">
<p>Vehicle Type</p>
<select class="car" name="car_type" id="car_type">
<option value="0">Select Vehicle</option>
<?php if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){ ?>
<option value="<?php echo $vt ?>" <?=isset($_GET['car_type'])&&$_GET['car_type']==$vt?'selected':'';?> ><?php echo $vt ?></option>
<?php }
} ?>
</select>
</div>
<div class="fields">
<p>Age of Youngest Driver</p>
<select class="half" id="driver" name="">
<option value="21-24">21-24</option>
<option value="25+">25+</option>
</select>
</div>
<input type="hidden" name="search" />
<div class="fields" style="text-align:center">
<input type="submit" value="Search" class="mitsub" />
</div>
</form>
Thanks and please help
You can add a conditional statement inside your foreach() that adds selected attribute to option. This will ensure that the selected value is selected in the list of options.
<?php
if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){
echo "<option value='$vt'";
if(isset($_GET['car_type']) && $_GET['car_type'] == $vt){
echo " selected";
}
echo ">$vt</option>\r\n";
}
}
?>
You can do this all in one, you just have to make sure the logic is correct.
echo "<option value='$vt'" . ((isset($_GET['car_type'] && $_GET['car_type'] == $vt) ? " selected" : "") . ">$vt</option>\r\n";
I just find it easier to work with a full if statement.

retrieve value from drop down bar and display result

on calculatePC.php, I have this code to display the finish_product
Select product:
<select class="itemTypes">
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
Let's say I have chosen Table as the finish_product.
On docalculate.php, I would like to display what I've chosen based on the dropdown list I've selected.
I tried this but there is error.
<?php echo $_POST['finish_product'] ?>
May I know how to display the result?
This doesn't exist:
$_POST['finish_product']
because you don't have a form element named "finish_product" in your markup. Add that name to the form element:
<select name="finish_product" class="itemTypes">
You need to do two things:-
Create a form before select and give it an action
give name attribute to your select box, then only you can get data in $_POST
So do like below:-
<form method="POST" action = "docalculate.php"> // give the php file paht in action
<select class="itemTypes" name="finish_product"> // give name attribute to your select box
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
<input type ="submit" name="submit" value ="Submit">
</form>
Now on docalculate.php:-
<?php
echo "<pre/>";print_r($_POST['finish_product']); // to see value comes or not
Note:- through Jquery its also possible. Thanks

Retain the option in the dropdown list in php

I have a dropdown with the following options:
i.)All
ii.)Public User
Where The option public user is selected from the database
<select name="desc" id="selectbox">
<?php
echo '<option value="all" >All</option>';
$a=$obj->getPublicUser();
echo '<option value="PublicUser" >'.$a.'</option>';
?>
</select>
$a has getPublicUser() :
public function getPublicUser()
{
$publicuser=self::PUBLIC_USER;
$result= mysql_query(" select category_desc from user_category where category_id=4");
if($result)
{
while ($row = mysql_fetch_assoc($result))
{
$user=$row['category_desc'];
}
}
return $user;
}
I have used
My Submit button is as follows:
<input type="submit" name="GETREPORT" value="Get Report" />
My problem is On selecting option ALL and clicking on submit button the all option is retained. But on selecting Public user and then clicking on submit button the option resets to "all". How can I make the public user retain even after the submit ? Plz help
you can try with this
if (isset($_GET['desc']) && $_GET['desc'] == "PublicUser") selected="seleceted"
Please Try this, Hope it help you:-
<html>
<form name = "client_question">
<select name="desc" id="selectbox">
<?php
echo '<option value="All" >All</option>';
echo '<option value="PublicUser">PublicUser</option>';
echo '<option value="NormalUser">NormalUser</option>';
?>
</select>
<input type="submit" name="GETREPORT" value="Get Report" />
</form>
<script>
<?php if(isset($_GET['desc']) && $_GET['desc']) {?>
document.getElementById("selectbox").value = "<?php echo $_GET['desc'] ?>";
<?php } ?>
</script>
</html>

Handling isset and select box in php html and for loop

Here is my code. This needs some optimization if you think the below code is bad and someone can really help me
I'm using two if condition and two else condition. Is there anyway this can be fine-tuned.
In the search box i need to retain the previous selected book name. i.e when the submit button is clicked the select box should display the result with the
selected item in the select box. for ex: if in dropdown if i select "book1" and click the submit button, the result should be displayed along with the "book1" as default inside the select box. in my case, the search box is getting reset on each submit click.
My Code:
<?php
if(isset($_POST['submit'])){
$get_project_id = $_POST['project_name'];
if($get_project_id == 1){
..display all the books(mysql query)..
}else{
..display the particular book(mysql query)..
}
}else{
..if the search button is not clicked then display all the books(mysql query)..
}
?>
<form method="post" name="book_list">
<select name="book_list" id="book_list">
<option value="1">All Books</option>
<?php while($row=mysql_fetch_array($result_books)){?>
<option value="<?php echo $row['Project_Auto_Id'];?>">
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
</select>
<input type="submit" name="submit" class="btn btn-default pull-right" value="Search"/>
</form>
Any help will be appreciable.
Than,ks
Kimz
You can do it by checking the condition inside while loop. Checking the value of option and the post value are same or not as shown in the below code.
<form method="post" name="book_list">
<select name="book_list" id="book_list">
<option value="1">All Books</option>
<?php while($row=mysql_fetch_array($result_books)){?>
<option <?php if(isset($_POST['book_list']) && ($_POST['book_list'] == $row['Project_Auto_Id'])) { echo 'selected'; } ?> value="<?php echo $row['Project_Auto_Id'];?>">
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
</select>
<input type="submit" name="submit" class="btn btn-default pull-right" value="Search"/>
</form>
Not sure if there is any better way to do the two if... else statements but even if there is there's nothing wrong with the way you've done it, just a different way.
With regards to having the option selected after submitting you need to change your output of the options to this:
<?php while($row=mysql_fetch_array($result_books)){?>
<option value="<?php echo $row['Project_Auto_Id'];?>"<?php if(isset($_POST['book_list']) && $_POST['book_list'] == $row['Project_Auto_Id']) echo ' selected'; ?>>
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>

Session not passing dropdown selected element to other php script

First php script -
<?php
session_start();
?>
<html>
<head>
</head>
<body><form method = "post">
<select name="feature" id="feature">
<?php
?>
<option value > Select Feature</option>
<?php
foreach($newFeature as $feat)
{
?>
<option value="<?php echo $feat;?>"><?php echo $feat;?></option>
<?php
}
?>
</select>
</form>
</body>
</html>
<?php
$_SESSION['feature'] = $feature;
?>
second php script -
<?php
session_start();
echo $_SESSION['feature'];
?>
When I run second php script, I get Array as echoed element instead of the element I selected.
What is wrong with the logic here ?
Please guide.
You have to submit the select. It is impossible to set $feature at that moment because the user hasn't yet selected anything.
<form method = "post">
<select name="feature" id="feature">
<option value > Select Feature</option>
<?php foreach($newFeature as $feat) : ?>
<option value="<?php echo $feat;?>" <?= $feat == $_SESSION['feature'] : " selected = 'selected'" : "" ?>><?php echo $feat;?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="send" name="mySubmit" />
</form>
When you hit 'send' you can get the value by using $_POST['feature']; on the same page. If you want to go to another page you have to set the form action property.
session_start();
$_SESSION['feature'] = $_POST['feature'];
After the submit the page will 'reload'. Check if mySubmit is set and set the $_SESSION['feature'](don't forget to start your session at top of the page):
if (isset($_POST['mySubmit'])){
$_SESSION['feature'] = $_POST['feature'];
}

Categories