开发者

Get a <input> button to carry a variable?

I'm working on a little program for tradeshows we are doing. It is spitting out a block for each tradeshow with information about it.

I want to add an "Edit"-button in each block, to take me to a page where I can edit that specific tradeshow.

How can I get the Edit-button to bring along the ID for that block?

If I make the Value $id then I can brin开发者_运维知识库g it, but because value is also the label on the button it says a number (the id) instead of Edit, which isn't pretty.

Can someone give me a hand? :)


You can wrap each button in a form, like this:

<form action="/edit/42"><button type="submit">Edit</button></form>

Don't forget to use appropriate CSS styling for the new form element.

You can also attach any behavior you want with JavaScript. Store the ID in a data attribute, like this:

<button data-edit-id="42">Edit</button>
// In JavaScript (with jQuery)
$('button').click(function(ev) {
    location.href = '/edit/' + $(ev.target).attr('data-edit-id');
});


I would add a form for each block (if it isn´t already there...) and add a hidden field with the ID.


Use the button's click event to set the id into a hidden field and then submit the form.

Another way is to have every button in a different form, with each one having a different action (corresponding to the item to be edited). It may not be applicable depending on your markup.


I usually abuse the name for it:

 <input type="submit" name="edit[123]" value="edit">

Receiving code:

if(isset($_POST['edit'])){
    $id = key($_POST['edit']);
}


I don't see why you would want to use a button in this case. I think you should include a clickable icon that points to the edit form. From what I can tell is that your information blocks are in <form> format for reasons unknown when they should be in a table or set of neatly aligned divs.

I would suggest you create a link and on the other page use $_GET['id'] to edit that entry:

<a href="/edit/?id=<?php echo $tradeshowID; ?>"><img src="/icons/edit.png"></a>

Or do I not understand the purpose of the button in your question?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜