Handling isset and select box in php html and for loop - php

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 }?>

Related

HTML form POST data not transferring to next page

I have a form with a drop-down box. The user selects an option from the dropdown box and hits submit. On submit, a new page opens up with a new form. The option that was selected is not being transferred to the next page. My form code is:
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname" type="input">
<option></option>
<?php foreach ($data as $row): ?>
<option>
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit" name="heroname">Submit</button>
</div>
</form>
The next page loads and displays some test variables at the top, but the variable from the POST is empty. My code on the page is
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
include('header.php');
$username = $_SESSION['username'];
echo $_POST['heroname'];
echo $username;
$test1 = "test 1, before the if statement";
echo $test1;
I get no errors on the page. The username variable and the test1 variable echo normally. The heroname variable doesn't. I need help figuring out why the selection from the form is not transferring to the next page. Thanks in advance.
You have forgotten the value attribute from the <option> elements. Whatever is in the value attribute is what is returned to the server script.
You have a type="input" attribute on the <select> element, that does not belong there.
You have called the button <button type="submit" name="heroname"> which is also what you called the dropdown <select name="heroname"> you cannot use the same name more than once. So change the button name to something else like <button type="submit" name="submithero"> for example
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname">
<option></option>
<?php foreach ($data as $row): ?>
<option value="<?php echo $row['Hero_Name'] ?>">
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit" name="submithero">Submit</button>
</div>
</form>
the button does not need a name attribute
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname">
<option></option>
<?php foreach ($data as $row): ?>
<option value="<?php echo $row['Hero_Name'] ?>">
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
First, I removed the value attribute from select. Second, I added a value attribute to option. Third, I changed the name for the button to match the IF statement on the next page. The heroname is now being passed to the next page. I have a whole new set of errors now about undefied index, which I will research first, and if needed, post a new question. Thank you all for the help!!

Passing an input value to a select option to another page using POST

I have an input text from form1.php that I want to pass to form2.php that has a select option, and i'm trying to get whatever the input text from form1.php to be the selected option in form2.php.
It looks straight forward when trying to pass a select value to an input, but i'm stuck when i turn things around.
form1.php:
<form class="form1" action="form2.php" method="POST">
<input type="text" name="inputval">
<button type="submit" name="submit">Submit</button>
</form>
form2.php:
<?php
if (isset($_POST["submit"]))
{
<select name="productselect" id="productselect">
<option>Car</option>
<option>Bus</option>
<option>Plane</option>
<option>Sail Boat</option>
</select>
}
else{}
?>
Any help in pointing me in the right direction would really be appreciated. Thank you.
You need to check in each select option if the value of $_POST['inputVal'] is the same as the option, and if so add selected="selected" to mark it as selected option.
Also take into account that if no option is marked as selected, the first one will be selected automatically.
<?php
if (isset($_POST["submit"])) {
?>
<select name="productselect" id="productselect" >
<option <?php if($_POST['inputVal'] == 'Car') echo 'selected="selected"' ?>> Car</option >
<option <?php if($_POST['inputVal'] == 'Bus') echo 'selected="selected"' ?>> Bus</option >
<option <?php if($_POST['inputVal'] == 'Plane') echo 'selected="selected"' ?>> Plane</option >
<option <?php if($_POST['inputVal'] == 'Sail Boat') echo 'selected="selected"' ?>> Sail Boat </option >
</select >
<?php
} else {
}

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

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>

php form exec, sed and file_get_contents

I am working on a script to edit a file on my vps, so far thanks to the help from a user here I have the following:
<?php
if(!empty($_REQUEST['color_choice'])){
exec('sed -i '.escapeshellarg('s/color=.*/color='.$_REQUEST['color_choice'].'/g')." /home/user/colors/color.choices");
echo 'File color choice has been updated';
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="color_choice">;
<option value="red">red</option>;
<option value="blue">blue</option>;
<option value="black">black</option>;
<option value="orange">orange</option>;
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
This changes the values as needed but I have one issue I am still trying to solve. The script does not get the current value that is in the file itself, so when I first visit the page it always says "red". After I make a change it still says "red" on the php form.
Edit: I would like the form to always display the current value in the color.choices file, instead of just going back to "red" every time. If I change the value on the form to "blue" I want the form to display that the current value in the file is set to "blue".
I have been told I need to use "file_get_contents" to first retrieve the value from the file itself. I have made a few attempts but I am getting no where. If I need to add more information please let me know!
What if you did this?
<?php
$current = file_get_contents("/var/www/html/colors/color.choices");
$color = explode("=", trim($current));
if(!empty($_REQUEST['color_choice'])){
exec('sed -i '.escapeshellarg('s/color=.*/color='.$_REQUEST['color_choice'].'/g')." /var/www/html/colors/color.choices");
echo 'File color choice has been updated';
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="color_choice">;
<option value="red" <?php if($color[1] == 'red'){?>selected="selected"<?php }?>>red</option>;
<option value="blue" <?php if($color[1] == 'blue'){?>selected="selected"<?php }?>>blue</option>;
<option value="black" <?php if($color[1] == 'black'){?>selected="selected"<?php }?>>black</option>;
<option value="orange" <?php if($color[1] == 'orange'){?>selected="selected"<?php }?>>orange</option>;
</select>
<input type="submit" name="Submit" value="Submit" />
</form>

Using select dropdowns in a form

I am trying to use HTML select dropdowns to help sort the search results on my website and I am having some trouble. In the code below, I specifically select Option1 and when I try to echo the result after I submit the form, it always echo's 'FAIL'. I was wondering if someone can help me understand what I am doing wrong and what is the proper way to retrieve the data from the option the user selected, after the form was submitted?
<?php
echo '<form method="post" action="search.php">
<select>
<option name="one">Option1 </option>
<option name="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
if(isset($_POST['one'])){
echo 'Option1 is set';
}else{
echo 'FAIL';
}
?>
thank you
This is because the name attribute goes with the select tag.
<select name="dropdown">
<option>Option 1</option>
<option>Option 1</option>
</select>
if(isset($_POST['dropdown'])) {
echo $_POST['dropdown']; //echoes 'Option 1'
}
If you like, you can add a value attribute to the option element, but you don't have to.
If you do
<option value="foobar">Option1</option>
The form will post foobar and not Option1.
<?php
echo '<form method="post" action="search.php">
<select name="my_option">
<option value="one">Option1 </option>
<option value="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
echo "My option value is : " . $_POST['my_option'];
?>
You need to name your select tag and access that instead.
<select name="options"></select>
echo $_POST['options']; will give you your selected option
Instead of name="one" an option needs a
value="myvalue"
and your select tag needs an
name="nameofthisthinghere"

Categories