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>
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>
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>
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"
I have a simple problem in php select option.
The page has 5 select option & submit button. When I select a option then it should goes on specific web page. For Example: http://onlinetools.org/tricks/using_multiple_select.php
Then I select a option & press Send then It show Which option I select. But I need go specific webpage.
I have tried with this code but I have failed...
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
<option value="http://google.com">one</option>
<option value="http://yahoo.com">two</option>
<option value="http://facebook.com">three</option>
<option value="http://who.is">four</option>
<option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
$test=$_POST['test'];
echo "
<script type=\"text/javascript\">
window.location = \"$test\"
</script>
";
?>
Anybody Can help me?
This should be a simple select not a multiple one since you want to redirect to only one site. here is the code :
<?php
$test=$_POST['test'];
if(isset($test)){
header("Location: $test");
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test">
<option value="http://google.com">one</option>
<option value="http://yahoo.com">two</option>
<option value="http://facebook.com">three</option>
<option value="http://who.is">four</option>
<option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
As you've declared:
<select name="test[]" multiple="multiple">
test is an array, thus you have to take the first object of the array or remove the unused []. By the way, you can do an HTTP redirection, which is faster and works more often than javascript ones.
ive got the code below set up. but how do i filter the result without submit button and filtering the result based on dropdown value (id for user)?
form method="post" name="report_filter" action="<?= $_SERVER['PHP_SELF'];?>" >
select name="name" onchange="report_filter.submit();">
<option value="--">Filter by:</option>
<option value="1">Andi</option>
<option value="2">Katy</option>
<option value="3">Max</option>
<select>
PHP //
<? if(isset($_POST['submit'])):
$mem->StaffTodayFilterSummary($_GET('value'));
else :
$mem->StaffToday();
endif;
?>
</form>
You need to change name="report_filter" to id="report_filter".
Then change your onchange event to say onchange="document.getElementById('report_filter').submit()"
Here's the full code:
<form method="post" id="report_filter" action="<?= $_SERVER['PHP_SELF'];?>" >
<select name="name" onchange="document.getElementById('report_filter').submit();">
<option value="--">Filter by:</option>
<option value="1">Andi</option>
<option value="2">Katy</option>
<option value="3">Max</option>
<select>
<?
if(isset($_POST['name'])):
$mem->StaffTodayFilterSummary($_POST['name']);
else :
$mem->StaffToday();
endif;
?>
</form>
I changed isset() to check POST['name'] and set the filter summary to pass in $_POST['name'] as well. I don't know where $_GET('value') was trying to get anything from but unless it's in the URL I don't see how it would work.