Use form button to submit GET value - php

I have the following code to submit GET values:
<form method="GET">
<input type="hidden" name="price" value="ASC" />
<input type="submit" value="Price Low to High" />
</form>
<form method="GET">
<input type="hidden" name="price" value="DESC" />
<input type="submit" value="Price High to Low" />
</form>
<form method="GET">
<input type="hidden" name="price" value="" />
<input type="submit" value="Default" />
</form>
Which is working fine. So I am not repeating myself with three forms, is there a way I could have a single form, and define what GET values are submitted by the buttons? They would still need to have the same information visible to the user e.g. the button that submits DESC needs to display as Price High to Low

use this code:
<form method="GET">
<input type="submit" name="price" value="ASC">Price Low to High</input>
<input type="submit" name="price" value="DESC" >Price High to Low</input>
<input type="submit" name="price" value="" >Default</input>
</form>

If having three buttons is not obligatory by design, then I suggest, why not just use a select to let the user decide what he wants (as most of the sites already do)
<form method="GET">
<select id="price" name="price">
<option value="">Default</option>
<option value="ASC">Price Low to High</option>
<option value="DESC">Price High to Low</option>
</select>
<input type="submit" value="Price Low to High" />
</form>
Demo
And if having three buttons is necessary then, you could do either one of (though not limited to) the following..
Use plain links (<a> anchor tags), yup you read that correct, since the form is just submitting using the $_GET method, eventually the data is gonna be inside the URLs, so why not just have it there in the first place using the href attributes of the "buttons" ?
Use the solution that #Aref Anafgeh suggested, and have three submit buttons each with different values and just let the html handle which value is sent to the server.
Use JavaScript and make ajax calls, which pretty much allows you to handle what is & isn't sent in the request.

Try this code.
<!DOCTYPE html>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET")
{
echo "Form Submited by ".$_GET['submitbutton']." Button";
}
?>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="ASC" />
<input type="submit" value="Price Low to High" name="submitbutton"/>
</form>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="DESC" />
<input type="submit" value="Price High to Low" name="submitbutton"/>
</form>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="" />
<input type="submit" value="Default" name="submitbutton"/>
</form>
<script>
</script>
</body>
</html>

<form method="get">
<select name="sort">
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
<option value="default">Default</option>
</select>
<button name="price">Sort price ascending</button>
</form>
snip
<?php if ($_GET['sort'] == "asc") {
// Do something
} elseif ($_GET['sort'] == "desc") {
// Do something else
}
// etc
?>
Hyperlinks to http://url/?price=XXX would also work fine since you're using GET

Related

My html form automatically passing all field data in url

I have created a simple html form but I have encountered a weird problem which I have never seen before. When I click on submit then all of the data is directly passing to the URL bar. I don't know what is wrong with this can anyone help?
Form:
<form id="form-data">
Name<br><input type="text" name="name" id="name" value=""><br><br>
Age<br><input type="number" name="number" id="age" value=""><br><br>
Gender<br>
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Fe-male<br><br>
<select name="country">
<option value="Kashmir">Kashmir</option>
<option value="Egypt">Egypt</option>
<option value="Norway">Norway</option>
<option value="Iceland">Iceland</option>
</select><br>
<br><input type="submit" id="submit" value="Save">
</form>
the form uses get method therefore it displays the form values in the url. change the method to post to avoid this
method="post"
<!DOCTYPE html>
<html>
<head>
<title></title>
<body>
<form id="form-data" method="post">
Name<br><input type="text" name="name" id="name" value=""><br><br>
Age<br><input type="number" name="number" id="age" value=""><br><br>
Gender<br>
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Fe-male<br><br>
<select name="country">
<option value="Kashmir">Kashmir</option>
<option value="Egypt">Egypt</option>
<option value="Norway">Norway</option>
<option value="Iceland">Iceland</option>
</select><br>
<br><input type="submit" id="submit" value="Save">
</form>
</body>
</html>
Add method attribute to your form tag like this:
<form action="welcome.php" method="post">
https://www.w3schools.com/tags/att_form_method.asp
If you don't send the data somewhere, it gets passed as URL parameters to the current page:
From MDN Web Docs:
The action attribute defines where the data gets sent. Its value must
be a valid relative or absolute URL. If this attribute isn't provided,
the data will be sent to the URL of the page containing the form — the
current page.

getting object not found error after clicking submit button, I want to display the value of hidden tag

<html>
<BODY>
<form action= "myscript.php" method="post">
first name: <input type="text" name="firstname" value="Mary"><br>
Last name: <input type="text" name="lastname" value= "clan"><br>
<input type="checkbox" name="mychoices[]" value="choice1" checked>
<input type="checkbox" name="mychoices[]" value="choice2">
<input type="checkbox" name="mychoices[]" value="choice3">
<select name="myselection">
<option value="selection1" selected>Option1</option>
<option value="selection2">Option2</option>
<option value="selection3">Option3</option>
</select>
<select name="myselections[]" size="3" multiple>
<option value="choice1" selected >Choice1</option>
<option value="choice2" selected>choice2</option>
<option value="choice3">choice3</option>
</select>
<textarea name="mytextarea" rows="10" cols="40">
Welcome to the web developement world.
</textarea>
<input type="password" name="mypassword">
<input type="hidden" name="myname" value = "myvalue">
<input type="reset" value="reset form">
<input type="image" name="myimage" src="desert.jpg" height="42" width="42" onclick= "document.write('<? php Aftersubmit() ?>');"/>
<input type="submit" name="submitbutton" value="Submit Form">
</form>
<?php
function Aftersubmit()
{
$myname = $_POST['myname'];
if(isset($myname)){
echo ($myname);
}
}
?>
</BODY>
</HTML>
I want to display the value of hidden tag after clicking submit button. But getting "Object not found" error 404. Beginner in php, pls help. I also want to know how to call php functions from html.
You can't call a PHP function from an onClick event. That only works with Javascript functions. So one problem is in this line:
onclick= "document.write('<? php Aftersubmit() ?>');
You can remove that, and then pull your PHP out of the function. This will display your name if you click the submit button.
<?php
if(isset($_POST['submitbutton'])){
$myname = $_POST['myname'];
if(isset($myname)){
echo ($myname);
}
}
?>
You may also have to change the action of your form:
<form action= "myscript.php" method="post">
If the file myscript.php doesn't exist, then you'll get a 404 Not Found error every time. You can make a form point to itself by removing the action attribute:
<form method="post">

More than one html form in a php file

I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)

removing current _POST data to submit form to new page

I have a form where I would like to use _POST data to submit check box values. My problem is there are already post values in my current page. When I submit the form it is returning the current post values instead of the ones I want, which gives me the Notice: Undefined index message.
Basically this form is to compare selected products, but in the current post values it has the product information along with order by values. Is there a way to just submit my new form's information on its own?
Thank you
EDIT
Well, here's the thing about my code, I am using a shopping cart called Virtuemart. I've added the code below minus all the other stuff which don't effect the form.
Product File
// Code here for Template file for individual items in category
//creating checkbox for comparison form
echo "<input type =\"checkbox\" name=\"fruit[]\" value=\"".$product_sku."\" />
<label for=\"".$product_name."\"> ".$product_name."</label>";
Category File
//all other forms on this page
<form action="items/index.php" method="get" name="results" target="_blank">
<input class="inputbox" name="search" id="item-search" type="text" value="Search"></td><td>
<input type="image" class="sub-button" src="/search.png" alt="submit" name="submit" value="search">
</form>
<form action="index.php" method="get" name="order">
Sort by:
<select class="inputbox" name="orderby" onchange="order.submit()">
<option value="product_list" >Select</option>
<option value="product_name" selected="selected">Product Name</option>
<option value="product_price" >Price</option>
<option value="product_cdate" >Latest Products</option>
</select>
<script type="text/javascript">//<![CDATA[
document.write(' <input type="hidden" name="DescOrderBy" value="ASC" /><img src="images/sort_asc.png" border="0" alt="Ascending order" title="Ascending order" width="12" height="12" />');
//]]></script>
<noscript>
<select class="inputbox" name="DescOrderBy">
<option value="DESC">Descending order</option>
<option selected="selected" value="ASC">Ascending order</option>
</select>
<input class="button" type="submit" value="Submit" />
</noscript>
<input type="hidden" name="Itemid" value="89" />
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="page" value="shop.browse" />
<input type="hidden" name="category_id" value="0" />
<input type="hidden" name="manufacturer_id" value="0" />
<input type="hidden" name="keyword" value="" />
<input type="hidden" name="keyword1" value="" />
<input type="hidden" name="keyword2" value="" />
<div id='limitbox'> Display #
<select class="inputbox-reg" name="limit" size="1" onchange="this.form.submit();">
<option value="6" >6</option>
<option value="18" >18</option>
<option value="30" >30</option>
</select>
<input type="hidden" name="limitstart" value="0" /></div> <noscript><input type="submit" value="Submit" /></noscript>
<!-- PAGE NAVIGATION AT THE TOP <br/>
<div style="text-align:center;"> </div>
-->
</form>
// all other forms end
Form I want to pass values from
echo"<form method=\"POST\" name=\"compare\" id=\"compare\" action=\"http://localhost/comparepage/\">";
//TEMPLATE FILE HERE THAT GENERATE PRODUCTS FROM PRODUCT FILE CODE ABOVE.
//Basically it has foreach statements to generate all products
echo "<input type=\"submit\" name=\"submit\" value=\"Compare\" onClick=\"document.compare.submit()\" >";
echo "</form>";
I've changed POST to GET just to get the values that are being passed and in the next page I see a bunch of values in the url.
Try putting the fields of the second form within separate <form> tags
Form 1:
<form method='post' action='/process.php'>
<input name='town' type='text'/>
<input name='country' type='text'/>
<input type='submit'/>
</form>
Form 2:
<form method='post' action='/process.php'>
<input name='gender' type='text'/>
<input name='state' type='text'/>
<input type='submit'/>
</form>
If you submit form 1, $_POST will contain town, country
If you submit form 2, $_POST will contain gender & state
As suggested, some code and more details would be helpful. But if you have multiple form fields on the same page, and depending on the action, you want to submit different sets of those fields, then just wrap them in different <form></form> elements. A POST submit only submits the form fields contained with that particular block.
So you could have:
<form name='form1' method='POST' action='#'>
<input type='hidden' name='field1' value='true' />
<input type='submit' name='submit' value='Submit' />
</form>
<form name='form2' method='POST' action='#'>
<input type='hidden' name='field1' value='false' />
<input type='submit' name='submit' value='Submit' />
</form>
If the first submit button is clicked, $_POST['field1'] = 'true', else = 'false'

Best method for submitting multiple options with associated values

I have a project in which I'm going to have a long list of items with an associated value or two that I want a user to be able to click on and add to a database.
For example I'd like a page with these values:
Cat, animal_id=1
Dog, animal_id=2
Pig, animal_id=3
etc.
Now if this was only a few items I'd use multiple forms:
<form action="add.php" method="post" name="animal_form1" id="animal_form1">
<input id="animal_name" name="animal_name" type="hidden" value="cat" />
<input id="animal_id" name="animal_id" type="hidden" value="1" />
<input type="submit" name="animal_add" id="animal_add" value="add" />
</form>
<form action="add.php" method="post" name="animal_form2" id="animal_form2">
<input id="animal_name" name="animal_name" type="hidden" value="cat" />
<input id="animal_id" name="animal_id" type="hidden" value="1" />
<input type="submit" name="animal_add" id="animal_add" value="add" />
</form>
etc.
but if there are a whole lot of items with many associated values I want to submit, what would be the most effective way of doing this?
you could use a select box that includes all needed values at once:
<form action="add.php" method="post" name="animal_form" id="animal_form">
<select name="animal">
<option value="cat|1" />Cat 1</option>
<option value="cat|2" />Cat 2</option>
<option value="pig|3" />Pig 3</option>
</select>
<input type="submit" name="animal_add" id="animal_add" value="add" />
</form>
and in php, split it:
$animal = explode('|', $_POST['animal']);
echo $animal[0] . ', animal_id=' . $animal[1];

Categories