PHP Unique names for multiple submit buttons fails
I am busy making a small search engine that gets info out of my DB. The next thing that has to happen, is that every searched record has the option to edit it. So I have to give every record a submit button with a unique name. This works fine, until I press the button开发者_如何学Python. Then every button has the same name.
create form:"
<form sutff>
$t=0;
while (){<br/>
$t+=1;<br/>
input type="Submit" name="$t"/><br/>
}</form>
one pressed a button:
while($_POST[$t]>0){
if (isset($_POST[$t])) {
do stuff
}
else{
$_POST[$t]-=1;
}
}
Don't worry about accolades, '' and "". I'll fix it myself.
You could use a seperate form for each row, that way you can have hidden fields with data.
Another solution is to use an onclick event on the submitbutton to set a hidden field before continuing with the submit.
Naming the submitbutton should work though.
A third option is to not use submit at all but go for a plain button and use onclick to open an url instead.
A forth option is to fake the button with an image with a link around.
But with more information we might get some more ideas.
A JavaScript approach goes like this:
<form name="form1" method="post">
<input type="hidden" name="item_id" value="">
<input type="submit" value="submit this" onclick="document.form1.item_id.value = '<?php echo 16; ?>';">
<input type="submit" value="submit this" onclick="document.form1.item_id.value = '<?php echo 32; ?>';">
<input type="submit" value="submit this" onclick="document.form1.item_id.value = '<?php echo 48; ?>';">
</form>
On the server side:
<?php
$item_id = $_POST["item_id"];
?>
Another option, perhaps better since its 100% HTML+CSS solution, is to do something along these lines:
<style type="text/css">
.submit-button {
.submit-button {
/* WARNING: works only in standards compliance mode */
text-indent: -1000px; /* this hides the button label */
background: url("edit.gif") no-repeat; /* this places an icon inside the button */
margin: 0; /* rest makes the button look CoOl */
border: 1px solid;
border-color: buttonhighlight buttonshadow buttonshadow buttonhighlight;
padding: 0;
width: 18px;
height: 18px;
}
</style>
<form name="form1" method="post">
<input type="submit" name="item_id" value="<?php echo 16; ?>" class="submit-button">
<input type="submit" name="item_id" value="<?php echo 32; ?>" class="submit-button">
<input type="submit" name="item_id" value="<?php echo 64; ?>" class="submit-button">
</form>
On the server side:
<?php
$item_id = $_POST["item_id"];
?>
Im not sure what you are asking
You can set a value for the submit button and read that
<input type='submit' name='sub' value='edit' />
<input type='submit' name='sub' value='search' />
And then in php you can do
if ($_POST['sub'] == "edit") {
//Do edits
}
else if ($_POST['sub'] == "search"){
//Do searching
}
else{
//Run Away....
}
Hope this helps!
Why does it need to be a submit button, or even a button?
<a href="edit.php?id=1">Edit item 1</a>
<a href="edit.php?id=2">Edit item 2</a>
<a href="edit.php?id=3">Edit item 3</a>
<a href="edit.php?id=4">Edit item 4</a>
精彩评论