Passing Drop down Select Value to URL - php

<select id="langu" name="langu">
<option value="English">English</option>
<option value="Chinese">中国的</option>
<option value="Korean">한국의</option>
<option value="Vietnamese">Việt</option>
</select>
The above is part of a .PHP based, based on the selection a user makes, I'd like to pass the value of the select as part of the URL for the form action:
<form name="frmLang"
action="index2.php?r=<?php echo $rguid;?>&l=[THE DROPDOWN OPTION HERE];?>"
method="post">
First off is this possible? If so, how does one pass that variable into the URL without having to go to another page? Javascript? (Cannot use AJAX).
Thanks,
H.

Change method="post" to method="get" and include $rguid as a hidden input:
<form name="frmLang" action="index2.php" method="get">
<input type="hidden" name="r" value="<?php echo $rguid; ?>">
<select id="langu" name="l">
.
.
.

Related

losing other parameters in the url when using GET method

Here is my form
<form method="GET" action="/admin.php?rubrique=users&action=detail">
<select name="id" onchange="this.form.submit()">
<option value="user1">user1</option>
<option value="user2">user2</option>
</select>
</form>
When I change the select, I go to /admin.php?id=user1 and the rubrique & action parameters are removed !
How can I preserve these values (I need to user GET method).
Is the only solution is using some hidden inputs ?
The browser does indeed not append values to the existing query parameters in action, it overwrites it completely. Simply use hidden form elements to transport those static query values:
<input type="hidden" name="rubrique" value="users">
<input type="hidden" name="action" value="detail">
They will be send together with the selected value as query parameters.
You can get value of ID and assign it to window.location to reload page via following method.
Try
<form method="GET" action="/admin.php?rubrique=users&action=detail">
<select name="id" onchange="window.location='/admin.php?rubrique=users&action=detail&id='+this.value">
<option value="user1">user1</option>
<option value="user2">user2</option>
</select>
</form>
You need to use hidden input fields for rubrique and action
<input type="hidden" name="rubrique" value="users" />
<input type="hidden" name="action" value="detail" />

Passing two variables from dropdownmenu in PHP

I want to reload my page (PHP) on changing value from the dropdownmenu and passing the value of selected item as a variable alongwith one another variable to the page. I have tried following -
<select name="application" onChange=Refresh(this.value,$query)" \>
<option>Any Application</option>
<option>ChIP</option>
<option>Enzyme Immunoassay</option>
<option>ELISA</option>
<option>Flow Cytometry</option>
<option>FACS</option>
</select>
The Refresh function is:
function Refresh(id,gene){
location.href="getnet.php?app=" + id + "&query=" + gene;
}
And i am receiving variables as:
$query = $_POST['gene'];
$app = $_POST['application'];
you must use form submit() method in onchange event of the dropdown and hidden field for second parameter, for example:
<form name="myForm" method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>">
<select name="first" onChange="document.myForm.submit();">
<option value="1">Value 1
<option value="2">Value 2
<option value="3">Value 3
</select>
<input type="hidden" name="second" value="second parameter value"/>
</form>
On the server side you can get your variables in $_GET supperglobal array like:
$_GET['first'] - it's value from dropdown element and $_GET['second'] - second hidden param.
Try to write js function for dropdown onchange event. Below is sample code:
<script type="text/javascript">
function reload_url(val){
window.location.href = 'some_php_file.php?new_var='.val;
}
</script>
<select name="my_Sel" onchange="reload_url(this.value);">
<option value="1">1</option>
<option value="2">2</option>
</select>
What about trying a form submit onChange?
<form name="form" action="url" method="get">
<select name="application" onChange="this.form.submit();" >
<option>Any Application</option>
<option>ChIP</option>
<option>Enzyme Immunoassay</option>
<option>ELISA</option>
<option>Flow Cytometry</option>
<option>FACS</option>
</select>
<input type="hidden" name="gene" value="variable" />
</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"

Append form value to URL that already has variables in it

I've got a page showing the results of a MYSQL query written in PHP. The URL has the variables that the user submitted on the previous page as:
www.mydomain.com/search/?var1=xx&var2=xx&var3=xx
When the user is on the results page they need to be able to sort the results. To do this I've got a SELECT form
<form action="/search<?php echo $urlQuery; ?>" name="order "class="formsrch" method="post" >
<label>sort by:</label>
<select class="order" id="order" name="order" onChange="this.form.submit()">
<option value="pricedesc">Price High to Low</option>
<option value="priceasc">Price Low to High</option>
<option value="dist">Distance</option>
</select>
</form>
The variable $urlQuery contains the string to be appended onto the url:
i.e. $urlQuery = "?var1=xx&var2=xx&var3=xx"
The problem is that when the form is submitted the page is reloaded and at the end of the url is ?order=dist.
Is there a way of replacing the question mark with an ampersand so the page will load and the value of order can be retreived?
Or, if anyone has a better way of doing the whole thing I'm definitely open to suggestions.
Thanks
why don't you put them in the form as hidden?
<?php
$extraVars = "";
foreach($_GET AS $key=>$value) {
$extraVars .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
}
?>
<form action="/search" name="order "class="formsrch" method="post" >
<?php echo $extraVars;?>
<label>sort by:</label>
<select class="order" id="order" name="order" onChange="this.form.submit()">
<option value="pricedesc">Price High to Low</option>
<option value="priceasc">Price Low to High</option>
<option value="dist">Distance</option>
</select>
</form>
You could make a hidden field for each of the variables you want to transfer so that they get appended too.
Better still, make the method of the form get and then access all the variables with the $_REQUEST global in the backend script.
Output the other variables as <input type="hidden" name="var1" value="xx" /> instead of as the form action, so they'll get taken into your query string.
With this action="/search" and $urlQuery = "?var1=xx&var2=xx&var3=xx"
then to have the value appended on the querysting you should change the form method to "GET".
If you want to keep the form method to "POST" then some javascript would be required to change the action of the form when the form is submitted.

php filter by dropdown without submit button

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.

Categories