losing other parameters in the url when using GET method - php

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" />

Related

How to get input field value in a variable for a link using PHP

I found this topic:
How to get input field value using PHP
But it didn't help to me. I didn't have any form just this:
echo '<input required type = "number" name = "db" value="1" /><br>';
And I want this input type value store in a variable if I hit a link it should send this checkbox data as well and open the href with these values.
Like ?buy=#product_id&quantity=#insert_value_here
Now it sends only the product_id and the quantity value is empty.
There is a form shown below that will submit to a url and it will add buy=#product_id&quantity=#insert_value_here so you can access those variables in php using. $_GET['buy'] and $_GET['quantity']
This is in the javascript console after submitting using the SO snippet:
You'll need to do some validation to ensure the values are set and that they are the proper type of info. Also, post would be better than get, and using a nonce would help prevent unwanted multiple submissions...
<form action="http://www.google.com" method="get" name="add-to-cart">
<select name="buy">
<option value="1">Product One</option>
<option value="2">Product Two</option>
<option value="3">Product Three</option>
</select>
<input type="number" name="quantity" max="10" min="0" step="1" required />
<input type="submit" value="Submit" />
</form>

Passing Drop down Select Value to URL

<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">
.
.
.

How to get link url when using select box form method get?

I have a sample code:
<form action="index.php" method="get">
<select name="id">
<option value="1">One</option>
<option value="2">Two</option>
</select>
<input type="submit" value="submit" name="submit" />
</form>
When I submit form is url is "index.php?id=1" // or index.php?id=2
=> How to fix it is result "index.php?id=1&name=One" // or index.php?id=2&name=Two
I think the best approach is to populate the options from a db. Use the values as db ID numbers. Then you can store as much info as you want for each option. example:
//some sort of loop{
<option value="
<?php echo $row_option['id'];?>">
<?php echo $row_option['display_text'];?></option>
}
At the other end query db with value; use whatever data need with that value.

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.

HTML form submit to PHP script

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.

Categories