When I change the value of the select element, the
Page should load
The value based output should be echoed.
the url Should indicate the http://mysite.com/?sort=value
HTML
<form name="form1" action="" method="post">
<select id="filter1">
<option value="?sort=recent" onselected="this.form.submit();">Most recent</option>
<option value="?sort=views" onselected="this.form.submit();">Most viewed</option>
</select>
</form>
PHP
<?php
if(isset($_GET['filter1']))
{
$term = strtolower($_POST['filter1']);
switch($term)
{
case 'recent':
echo "recent";
break;
case 'views':
echo "by views";
break;
}
}
?>
Try
<form name="form1" action="newurl.php" method="post">
<select name="sort" onChange="this.form.submit();">
<option value="recent">Most recent</option>
<option value="views">Most viewed</option>
</select>
</form>
And remember your form method is post so the sort value will be stored on $_POST['sort']
Also, you can just change the form's method to "get" and leave the form's action value blank, since the default action behavior on post is to call the same file (let's call it form.php) with the get values attached (something like "form.php?sort=selectedvalue"), which already implies an url change, despite being the same file.
You seem to be a little confused about the process of sending data in forms. Here is the code which will work for you, based on your PHP file, and the variables it's looking to get from the querystring.
<form name="form1" action="" method="get">
<select id="filter1" name="filter1" onchange="this.form.submit();">
<option value="recent">Most recent</option>
<option value="views">Most viewed</option>
</select>
<input type="submit" value="Submit" />
</form>
You can see I've changed the method attribute to get. This means the data will be sent via the querystring, so once the form is submit the URL will change to: http://example.com/?filter1=value
Also for the sake of redundancy, you should include a submit button for people who have javascript turned off.
You have to use onChange event on your select object:
<form name="form1" action="" method="post">
<select name="filter1" onchange="parentNode.submit()">
<option value="recent">Most recent</option>
<option value="views">Most viewed</option>
</select>
</form>
The value of var filter1 on your PHP script will be recent or views, no need to use ?sort=whatever to send the data.
Then you must to retrieve the vars from you POST super-global, and not GET as you used. If you want to use GET you must change the method type on your form to get. I think using GET is the right way to do what you want.. so:
<?php
if(isset($_GET['filter1']))
{
$term = strtolower($_GET['filter1']);
switch($term)
{
case 'recent':
echo "recent";
break;
case 'views':
echo "by views";
break;
}
}
?>
Related
I'm trying to make a simple search for Wikipedia where I can type what I want to search and a select where I can select the language. What I'm trying to do is to get the select value to be able to search in different languages so I just replace the language string in the url of wikipedia.org
(e.g. If I select French in the select dropdown the form should redirects me to fr.wikipedia.org and if I select English, it should redirects me to en.wikipedia.org)
Here's what I tried so far:
<form action="https://<?php $_POST["language"] ?>.wikipedia.org/w/index.php">
<input name="search" type="text" />
<select name="language">
<option value="en">English</option>
<option value="fr">French</option>
</select>
</form>
Now, on submit I get this url: https://.wikipedia.org/w/index.php?search=cat
How to pass select value to form action so I can add it at the start of the URL?
The reason your current script isn't working is because PHP gets processed when the page is loaded, not after you submit the form. The first time you go to this page, $_POST['language'] isn't set, so the form action gets set to https://.wikipedia.org/w/index.php. Then if you submit the form (get) you'll end up at https://.wikipedia.org/w/index.php?search=cat&language=en.
Instead what you need to do is process the form submission on your page, and then redirect to Wikipedia. By default, when you submit a form (with a default method of get) you'll end up at a page like /index.php?search=cat&language=en. So now you can read in those $_GET parameters to construct the URL to redirect to.
<?php
if (isset($_GET) && count($_GET)) {
$language = $_GET["language"];
$search = $_GET["search"];
$action = "https://".$language.".wikipedia.org/w/index.php?search=".$search;
header('Location: '.$action);
}
?>
<form>
<input name="search" type="text"/>
<select name="language">
<option value="en">English</option>
<option value="fr">French</option>
</select>
</form>
Another way would be to set <form method="post"> and then in PHP use $_POST like:
if ($_POST) {
$language = $_POST["language"];
$search = $_POST["search"];
$action = "https://".$language.".wikipedia.org/w/index.php?search=".$search;
header('Location: '.$action);
}
JS will make it a lot easier than PHP.
EDIT: This is only for your specific usage. If you want a more general way to do this using PHP instead of javascript, look at the answer by #WOUNDEDStevenJones
<input id="srch" type="text" />
<select id="lang" name="language">
<option value="en">English</option>
<option value="fr">French</option>
</select>
<button onclick="search()">Search!</button>
<script>
function search() {
var term =document.getElementById("srch").value;
var lang = document.getElementById("lang").value;
var link = "https://"+lang+".wikipedia.org/wiki/"+term;
location.replace(link);
}
</script>
I am new to php and want to make a dropdown menu where you can select a site that you want, then when you click a button you will be redirected to that site. The button will take you to for example "google.com" if that is what you select, if you select "stackoverflow.com" the same button will take you there. I currently have no working php code as I am not sure where to start. I will include the html code below.
<form method="post" action="testttt.php">
<select id="mySelect" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="Itslearning"> Itslearning
<option value="NDLA"> NDLA
</select>
<input type="submit" value="GO"/>
The form ends a bit further down, so please don't complain about there being no :P ANY HELP IS APPRECIATED :)
Try the below code , i have done the example which you want and its work perfectly . You can do it this in single file as i have done .
<?php
if(isset($_POST['btnsubmit']))
{
$options = $_POST["testing"];
header('Location: '.$options.'');
echo "Your option value".$options;
}
?>
<form method="post" action="#">
<select id="mySelect" name="testing" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="http://www.google.com"> Itslearning
<option value="http://www.stackoverflow.com"> NDLA
</select>
<input type="submit" name="btnsubmit" value="GO"/>
</form>
testttt.php will receive values in the $_POST array, so things like 'echo $_POST["somefield"];' will get you data...
...BUT you have to name your form elements. Not '<select id="somefield"...' but '<select name="somefield"...'. you can still have id attributes if you want, but pass to $_POST happens by what you put in the name= attribute.
Once you know the URL you're redirecting to, header("Location: $url");
Hope that gives you the push you need :-)
can you not just use the myfunction function like this:
function myfunction(event){
return window.open(event.target.options[event.target.options.selectedIndex].value,'');
}
If you want to do it in php, your select need a name attribute :
<form method="post" action="testttt.php">
<select id="mySelect" name="mySelect" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="Itslearning"> Itslearning
<option value="NDLA"> NDLA
</select>
<input type="submit" value="GO"/>
</form>
You can do that either in PHP :
if(isset($_POST['mySelect']) && !empty($_POST['mySelect'])){
header('Location: ' . $_POST['mySelect']);
}
Or in Javascript using the onchange attribute you already set up :
myFunction(){
var target = event.target;
document.location.href(target.options[target.options.selectedIndex].value);
}
Well i'm submitting form through onChange of select
<form name="selForm" id="selForm" type="POST>
<select name="sel_status" id="sel_status">
<option value="one">one</option>
<option value="two">two</option>
</select>
</form>
My jQuery Code
$('#sel_status').change(function(){
$('#selForm').submit();
});
And then i'm echoinging the value submitted in php
echo $_POST['sel_status'];
My network tab in Chrome showing 302 status
So how to submit the form through onChange of select and receive those values in php?
<form name="selForm" id="selForm" type="POST">
The action is missing and it's method, not type
You've got an error in your HTML code.
First, there is a " missing (after POST) and this attribute is called method, not type. So the correct code should be:
<form name="selForm" id="selForm" method="post">
<select name="sel_status" id="sel_status">
<option value="one">one</option>
<option value="two">two</option>
</select>
</form>
If your PHP code is in an other file, you also have to set the action="yourfile.php" attribute
Use $_POST['sel_status'];
You cannot use echo $_POST['selForm']; "selForm" is name of the form.
Add form action="yourphpfile.php" and change method="POST".
Try:
echo $_POST['sel_status'];
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.
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.