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.
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>
Im trying to build a web app where a user selects the value from a dropdown list and clicks the button next to the item. But only the value of select gets sent over to the next page.
echo "<td>".$row["idTaco"]."</td><td>".$row["nameTaco"]."</td><td>".$row["priceTaco"]."</td><td>".$row["descTaco"]."</td>";
?>
<td>
<form action="Includes/dodajVKosarico.inc.php?idTaco="<?php $row["idTaco"] ?> method="get">
<select name="kolicina">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<button type="submit" name="submit" class = "dodaj">Dodaj</button>
</form>
</td>
The row['idTaco'] works fine in the echo but when i try to send it using this form i cant pass the value to another page. Im sorry im still new to this.
Rather than using a query string value, one (arguably neater and easier to see) option is to just add the value again as a hidden field in your form, then it will get submitted along with the rest of the form data.
<form action="Includes/dodajVKosarico.inc.php" method="get">
<input type="hidden" name="idTaco" value="<?php echo $row["idTaco"] ?>"/>
...etc
Or you could just fix the broken quotes (and missing echo) in your existing code:
action="Includes/dodajVKosarico.inc.php?idTaco=<?php echo $row["idTaco"] ?>"
Everything is fine just missing an echo before your $row["idTaco"], You must do as follows:
<form action="Includes/dodajVKosarico.inc.php?idTaco=<?php echo $row['idTaco']; ?>" method="get">
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);
}
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 am making an HTML form. I want the results to appear in the PHP script.
<form action="chk_kw.php" method="post"> <br />
<select> name="website_string"
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij/option>
</select>
<input type="submit" name="website_string" >
</form>
The problem is I cannot get the value passed to the PHP. if I use value:
<INPUT TYPE="submit" name="website_string" value="selected" >
It always passes the text in the quotes. In this case "selected". How do I pass one of the strings from the option?
Try this:
<form method="post" action="check.php">
<select name="website_string">
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij</option>
</select>
<input TYPE="submit" name="submit" />
</form>
Both your select control and your submit button had the same name attribute, so the last one used was the submit button when you clicked it. All other syntax errors aside.
check.php
<?php
echo $_POST['website_string'];
?>
Obligatory disclaimer about using raw $_POST data. Sanitize anything you'll actually be using in application logic.
<form method="POST" action="chk_kw.php">
<select name="website_string">
<option selected="selected"></option>
<option value="abc">abc</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit">
</form>
As your form gets more complex, you
can a quick check at top of your php
script using print_r($_POST);,
it'll show what's being submitted an the respective element name.
To get the submitted value of the element in question do:
$website_string = $_POST['website_string'];
It appears that in PHP you are obtaining the value of the submit button, not the select input. If you are using GET you will want to use $_GET['website_string'] or POST would be $_POST['website_string'].
You will probably want the following HTML:
<select name="website_string">
<option value="" selected="selected"></option>
<option value="abc">ABC</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit" />
With some PHP that looks like this:
<?php
$website_string = $_POST['website_string']; // or $_GET['website_string'];
?>
Assuming you've fixed the syntax errors (you've closed the select box before the name attribute), you're using the same name for the select box as the submit button. Give the select box a different name.
For your actual form, if you were to just post the results to your same page, it should probably work out all right. Try something like:
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="POST>
Here is what I find works
Set a form name
Use a default select option, for example...
<option value="-1" selected>Please Select</option>
So that if the form is submitted, use of JavaScript to halt the submission process can be implemented and verified at the server too.
Try to use HTML5 attributes now they are supported.
This input
<input type="submit">
should be
<input name="Submit" type="submit" value="Submit">
whenever I use a form that fails, it is a failure due to the difference in calling the button name submit and name as Submit.
You should also set your enctype attribute for your form as forms fail on my web host if it's not set.