Reputation: 13683
In a form i am making,i need to inform the processing php script what button was clicked.I have three buttons,new,save and delete.I was expecting the form and all the buttons values to be posted but the form + the button clicked only gets posted.I am looking at the html spec here http://www.w3.org/TR/html401/interact/forms.html#submit-format but i am yet to find some explanation if this should be expected.I have this php script
<?php
/**
tpost.php
*/
//Buttons
$new = $_POST['new'];
$save = $_POST['save'];
$delete = $_POST['delete'];
//Error: Notice: Undefined index: save in C:\wamp\www\form-dev\troll.php on line 7
//Forms
$city = $_POST['city'];
$zip = $_POST['zip'];
$cs = $_POST['cs'];
//Error: Notice: Undefined index: save in C:\wamp\www\form-dev\troll.php on line 8
//Buttons echo
echo '<b>'.$new.'</b>' .'<br/>';
echo '<b>'.$save.'</b>' .'<br/>';
echo '<b>'.$delete.'</b>' .'<br/>';
//Forms echo
echo '<b>'.$city.'</b>' .'<br/>';
echo '<b>'.$zip.'</b>' .'<br/>';
echo '<b>'.$cs.'</b>' .'<br/>';
?>
this is the html i am using
<!Doctype html>
<head>
<meta charset="uft-8">
<title>Buttons post</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<style type="text/css">
label{
width:15%;
float:left;
font-style:italic;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$('.inew').on("click",function()
{
alert('new');
});
$('.isave').on("click",function()
{
alert('save');
});
$('.idelete').on("click",function()
{
alert('delete');
});
});
</script>
</head>
<body>
<form action="tpost.php" name="troll" method="post">
<label>Enter Your City</label><input type="text" name="city" value="some-city"/><br/><br/><br/>
<label>Enter Your Neighbourhood</label><input type="text" name="zip" value="my-zip" /><br/><br/><br/>
<label>Enter Your Nearest Shopping Mall</label><input type="text" name="cs" value="shop-here" /><br/>
<br/><hr/><br/>
<br/>
<hr/>
<input class="inew" type="submit" name="new" value="new" />
<input class="isave" type="submit" name="save" value="save" />
<input class="idelete" type="submit" name="delete" value="delete" />
</form>
</body>
</html>
What is the explanation for this?.
Upvotes: 0
Views: 6745
Reputation: 13683
I found the answer here http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2
It says
If a form contains more than one submit button, only the activated submit button is successful.
Upvotes: 1