Is it possible that when
I select something from the list
And I click Go
Browser will remember my last choice from the list
<form action="<?php echo $_SERVER ['PHP_SELF']; ?>" method="get" name="search_frm" id="serch_frm">
<input name="serchStr" type="text" />
<select name="list">
<option value="">select</option>
<option value="client">table client</option>
<option value="user">table user</option>
</select>
<input name="submit" type="submit" value="go" />
</form>
For example,
if I chose from the list user table
And hit Go
He will remain there
you can use sessions to access some data at a later time on a different script:
session_start();
$_SESSION = $POST['list'];
now the selected item from the list will remain there, as long as the browser is still opened.
if you want to remove the item from the session use:
unset( $_SESSION['list'] );
and remember every time you use sessions you must use at the top of the file (always):
session_start();
In order to make a select "stick" you need to put the word selected inside the <option> tag before the value. In order to do that you’re going to need to dynamically generate the options using a foreach loop. Inside the loop you’re going to look at the $_REQUEST and if it matches the value of what is being iterated in the loop you echo selected.
I’ve done this a dozen or more times and it works perfectly.
<?php
$options = array("select", "client", "user");
?>
Then in the page:
<form action="<?php echo $_SERVER ['PHP_SELF']; ?>" method="get" name="search_frm" id="serch_frm">
<input name="serchStr" type="text" />
<select name="list">
<?php foreach ($options as $option): ?>
<option <?php if ($_GET['list'] == $option) { echo "selected"; } ?> value="<?php echo $option; ?>"><?php echo $option;?></option>
<?php endforeach ?>
</select>
<input name="submit" type="submit" value="go" />
</form>
Related
I am passing a variable from page1 to page2 using GET and it's already working. However, I also use another get in the <select> <option> on page2 which means when I click an item in the option, the value I get from page1 disappears and becomes undefined since the address bar changes. I want the variable I get from page1 to be compared on SQL where clause. I can't think of a way on how to solve this, I'm not really knowledgeable in PHP, can you help me?
Here is my code(I remove some unnecessary lines): page1
<form method="get" action="home.php">
<div class="form-group">
<input id="codehe" class="form-control" type="codehe" name="codehe" placeholder="Class Code">
</div>
<div class="form-group">
<input class="form-control button" type="submit">
</div>
</form>
page2 (this is how i get the value from page1)
<?php
$codehe = $_POST['codehe'];
echo $codehe;
?>
The other GET (form) on page2
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
</form>
<?php
if(isset($_GET['state'])) {
$str = $_GET["state"];
$sql = "SELECT * FROM classvideo WHERE subjects = '$str' AND linkcode = ''";
?>
The traditional way to do things like this (without client-side code) is to put a hidden input in your form on page 2:
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
<!-- Add this line -->
<input type="hidden" name="codehe" value="<?php echo $_GET['codehe'] %>">
</form>
How to pass multiple selected checkbox from one page to another? I am a beginner, sorry for that. Thank you in advance for those who'll help me out with this problem.
You can pass data from one page to another through :
SESSION
COOKIE
GET and POST
<form method="post" action="page2.php">
<input type="hidden" name="var1" value="value1">
<input type="submit">
</form>
page2.php
<!-- language: lang-php -->
<select>
<option value="value1" <?php if($_POST['var1'] == 'value1'){ ?><?php selected ?>>value1</option>
<option value="value1" <?php if($_POST['var1'] == 'value2'){ ?><?php selected ?>>value2</option>
<option value="value1" <?php if($_POST['var1'] == 'value3'){ ?><?php selected ?>>value3</option>
</select>
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>
I have a sample code:
<form action="index.php" method="get">
<select name="id">
<option value="1">One</option>
<option value="2">Two</option>
</select>
<input type="submit" value="submit" name="submit" />
</form>
When I submit form is url is "index.php?id=1" // or index.php?id=2
=> How to fix it is result "index.php?id=1&name=One" // or index.php?id=2&name=Two
I think the best approach is to populate the options from a db. Use the values as db ID numbers. Then you can store as much info as you want for each option. example:
//some sort of loop{
<option value="
<?php echo $row_option['id'];?>">
<?php echo $row_option['display_text'];?></option>
}
At the other end query db with value; use whatever data need with that value.
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"