I am using multiple select elements on a page. This page's url is something like this: "index.php?s=foo".
Now I want that changing my select boxes will alter my URL and refresh page, this just works partly :(
Here is the example:
<form action="'.$_SERVER['REQUEST_URI'].'" method="GET">
<select name="this" onchange="this.form.submit()">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
</form>
So if I change my selection the url will swap to "index.php?this=1", but I would like to hold the previous _GET Parameters, so change would occur to "index.php?s=foo&this=1".
Any chance I would get that?
Thanks very much for your help.
Best Regards
Using REQUEST_URI does not include your current GET parameters. You will need the function in this gist to transform all parameters into input fields: https://gist.github.com/eric1234/5802030 .
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="get">
<?php array_to_input($_GET); ?>
<select name="this" onchange="this.form.submit()">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
</form>
CAUTION: array_to_input can introduce XSS vulnerabilities. Please escape contents of the $_GET variable so this cannot happen.
You could add each query string item as a hidden field which will be added to the query string when the form is submitted. Be sure to do some sanitizing on these things to make them safe.
if(!empty($_GET))
{
foreach($_GET AS $k => $v)
{
echo '<input type="hidden" name="'.$k.'" value="'.$v.'">';
}
}
The form action can then be hard coded to something like the file name or $_SERVER['SCRIPT_NAME']. Not $_SERVER['PHP_SELF'] - as pointed out by comment below.
edit : posted answer before I was finished. :(
The simple answer is instead of this:
<form action="'.$_SERVER['REQUEST_URI'].'" method="GET">
Do the action like this:
<form action="<?php echo $_SERVER['REQUEST_URI'].'?'.$_SERVER['QUERY_STRING']; ?>" method="GET">
Which will include the current $_GET values in your form's action/url and by extension be present when the form is submitted. Basically that is the way forms are designed in general to handle submission to a page with query parameters, ie. the default way it works.
Just a note but I think the way you have it now you might get this for your action/url
"http://'.$_SERVER['REQUEST_URI'].'"
It's just a guess, but it looks like the form is html, and the php will just print and not be executed as code ( without php tags ). Probably just an over-site in the example code. But, I would expect this with the format you have:
echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="GET">';
Cheers!
Related
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 need help on 2 things since I'm not savy in PHP:
Content that changes depending on the:
<select>
<option>Option 1</option>
<option>Option 2</option>
</select>
I think this can be done using PHP, by giving names to the options, having PHP get the names, creating variables depending on those names and putting an "include" that shows the changing content.
Here's the model for the script:
READ Select
IF User selects "variable1, varialbe2,etc"
THEN Display "page1,page2,page3"
*The page changes within the page so that if the user wants to change it again they dont have to go back...Maybe this can be done by using:
<?php include 'page1.php'; ?>
That content will include a button, and depending on that button, I want a text box to be filled with certain text.
<input type="button" value="">
Then Depending on the value of that button the PHP will put text into a textbox.
From what I've read this will be using $request and $post ...
(This of course is separate PHP)
There may be a couple of different ways to accomplish your goal. If we stick to using PHP as you've requested then something like this is probably best. I've used a case/switch here for security purposes and a POST instead of a GET to prevent URL manipulation although with the case/switch you have addressed a lot of URL manipulation anyway so it becomes a matter of preference for you.
<?php
$page = null;
if(isset($_POST['page'])){
$page = $_POST['page'];
}
switch($page){
case 'page3': include_once('/path/to/page3content.php'); break;
case 'page2': include_once('/path/to/page2content.php'); break;
case 'page1': include_once('/path/to/page1content.php'); break;
default: include_once('/path/to/defaultcontent.php'); break;
}
?>
<form name="myform" action="" method="post">
<select name="page" onchange="this.form.submit()">
<option value="page1"<?php if($page == "page1"){ echo " selected"; }?>>Page 1</option>
<option value="page2"<?php if($page == "page2"){ echo " selected"; }?>>Page 2</option>
<option value="page3"<?php if($page == "page3"){ echo " selected"; }?>>Page 3</option>
</select>
</form>
However, depending on your file structure, you can accomplish something similar even easer with just a little basic Javascript and HTML.
<select name="page" onchange="window.location=this.value">
<option value="/path/to/page1.php">Page 1</option>
<option value="/path/to/page2.php">Page 2</option>
<option value="/path/to/page3.php">Page 3</option>
</select>
You need to have a name field in your select tag, and value fields in your option tags:
<form name='myform' action='myform.php' method='POST'>
<select name='myselect'>
<option value='1'>Option 1</option>
<option value='2'>Option 2</option>
<option value='3'>Option 3</option>
</select>
<input type='submit'>
</form>
Then the PHP page will receive the selection via $_POST['myselect'].
You can submit the form onChange event of the select option.
Then you can run a $_GET and display the appropriate content for that particular page by comparing the value of $_GET['page']
<form action="page.php" method="get">
<select name="page" onchange="this.form.submit()">
<option value="page1">Page 1</option>
<option value="page2">page 2</option>
</select>
</form>
<?php
if(isset($_GET['page'])){
if($_GET['page']=="page1"{
//content for page 1
}elseif($_GET['page']=="page2"{
//content for page 2
}
}
?>
In this way you wont be needing the submit button as well. You can also redirect it to certain page by comparing the values of $_GET['page'] however you will need to include this form in every page with appropriate method to get your work done.
You could also bypass the submit button and use the following:
<form action="index.php" name="myForm" method="post">
<select onchange="window.location.href = this.value" name="select">
<option value="index.php?page=yourpage">yourpage</option>
<option value="index.php?page=yourpage2">yourpage2</option>
</select>
</form>
Then where your content would go you could:
<?php
if (isset($_GET['page'])) {
if ($_GET['page'] == "yourpage") {include("pages/page1.php");}
else ($_GET['page'] == "yourpage2") {include("pages/page2.php");}
} else {include("pages/default.php");}
?>
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;
}
}
?>
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.