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];
Related
I have this form:
<form action="<?php echo $php_self;?>" method="get">
<input type="submit" value="go" id="text33" />
<select name="match" id="text33" style="width:100px;">
<option value="1">1</option>
<option value="2">1</option>
</select>:select
</form>
This form is in the title page cup?c=2&match
I want to program the form so that if you press go .. go to the title page
cup?c= 2&match=1
As a function
$ cup=$_GET['c'];
$ match=$_GET['match'];
Just remove action param from tag form:
<form method="get">
<input type="submit" value="go" id="text33" />
<select name="match" id="text33" style="width:100px;">
<option value="1">1</option>
<option value="2">1</option>
</select>
</form>
Or replace your variable
$php_self
To
$_SERVER["PHP_SELF"]
i solve the problem
<form action="cup.php" method="get"><input type="hidden" value="<?php echo "2"; ?>" name="c" /><input type="submit" value="go" id="text33" /> <select name="match" id="text33" style="width:100px;"><option value="1">1</option><option value="2">2</option></select>:select</form>
thank you very much
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
Form Code :
<form action="my_php.php" method="post" enctype="multipart/form-data" id="postfile">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit" class="style1">
</form>
Select Tag :
<select id="lang" form="postfile">
<option value="gcc">C</option>
<option value="g++">C++</option>
<option value="javac">Java</option>
<option value="python">Python</option>
</select>
I am not able to access the $_POST["lang"] from the PHP Code.When i do:
echo $_POST["lang"];
It doesn't show anything.
No, you got to have the name="lang" attribute on your <select> tag:
<select id="lang" form="postfile" name="lang">
^ this
Reminder: Always access $_POST values, after the submission so you don't have to deal with undefined indices.
if(isset($_POST['submit'])) {
// then access your POST values
$lang = $_POST['lang'];
}
// this assumes you have an <input type="submit" name="submit" /> button
Basically, I want made a simple form in html and i want to output the all of the user input once clicked submit using php. So far its just displaying the details I entered but it doesn't get user's data to echo them out. Here are my two files
output.php:
<?php
echo 'Name: '.$POST["name"].'<br /> Gender: '.$POST["gender"].'<br />Country: '.$POST["country"];
?>
testingformphp.html (form part):
<form id="form" name="form" method="post" action="./PHP/output.php"/>
Name<input name="name" type="text" id="name"/><br/>
Gender<label><input type="radio" name="gender" id="gender_0" value="male"/>Male</label><br/>
<label><input type="radio" name="gender" id="gender_1" value="female"/>Female</label><br/>
<select name="country" id="select">
<optgroup title="europe">
<option value="Russia">Russia</option>
<option value="Greece">Greece</option>
</optgroup>
</select>
<input type="submit" id="button" value="Submit"/>
<input type="reset" id="reset" value="Reset"/>
</form>
Can anyone help?
$POST does not exists, try $_POST.
<?php
echo 'Name: '.$_POST["name"].'<br /> Gender: '.$_POST["gender"].'<br />Country: '.$_POST["country"];
?>
http://www.tizag.com/phpT/postget.php To review the post and get method.
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'