admin管理员组文章数量:1401598
If I had some coding as follow.
<form method="POST" action="localhost/carts/delete">
<a href="#" onclick="$(this).closest('form').submit();">Item 1</a>
<a href="#" onclick="$(this).closest('form').submit();">Item 2</a>
<a href="#" onclick="$(this).closest('form').submit();">Item 3</a>
</form>
And I like to post to a form with some hidden value to indicate which Item is clicked, how to do that???
Thanks.
Edited Text.
Thanks for so many useful suggestions.
I actually use Laravel 4 rather than PHP, my Laravel code produced this HTML
<form ...>
<ul>
<li>
<h1>Key: 1 </h1>
<input name="cart_id" type="hidden" value="1"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
<li>
<h1>Key: 2 </h1>
<input name="cart_id" type="hidden" value="2"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
<li>
<h1>Key: 6 </h1>
<input name="cart_id" type="hidden" value="6"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
<li>
<h1>Key: 7 </h1>
<input name="cart_id" type="hidden" value="7"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
</ul>
</form>
So, when I clicked on any items, I got 7 which is the last value of cart_id, how to place at the right place, in JavaScript???
If I had some coding as follow.
<form method="POST" action="localhost/carts/delete">
<a href="#" onclick="$(this).closest('form').submit();">Item 1</a>
<a href="#" onclick="$(this).closest('form').submit();">Item 2</a>
<a href="#" onclick="$(this).closest('form').submit();">Item 3</a>
</form>
And I like to post to a form with some hidden value to indicate which Item is clicked, how to do that???
Thanks.
Edited Text.
Thanks for so many useful suggestions.
I actually use Laravel 4 rather than PHP, my Laravel code produced this HTML
<form ...>
<ul>
<li>
<h1>Key: 1 </h1>
<input name="cart_id" type="hidden" value="1"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
<li>
<h1>Key: 2 </h1>
<input name="cart_id" type="hidden" value="2"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
<li>
<h1>Key: 6 </h1>
<input name="cart_id" type="hidden" value="6"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
<li>
<h1>Key: 7 </h1>
<input name="cart_id" type="hidden" value="7"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
</ul>
</form>
So, when I clicked on any items, I got 7 which is the last value of cart_id, how to place at the right place, in JavaScript???
Share Improve this question edited Aug 30, 2013 at 10:15 Artisan asked Aug 30, 2013 at 10:01 ArtisanArtisan 4,11213 gold badges39 silver badges64 bronze badges 4-
Can you use
input type="button"
instead? – Halcyon Commented Aug 30, 2013 at 10:02 - check console for errors – Tushar Gupta - curioustushar Commented Aug 30, 2013 at 10:05
- @FritsvanCampen I'm using jQuery Mobile so I really need to use <a> tag, otherwise I cannot get the expected UI style. – Artisan Commented Aug 30, 2013 at 10:06
- @TusharGupta There is no errors yet, but I don't get the concept of how to pass hidden field value according to clicked <a>, so I don't what to code next. :( – Artisan Commented Aug 30, 2013 at 10:08
3 Answers
Reset to default 1Create a hidden element inside the form:
<form method="POST" action="localhost/carts/delete">
<input type="hidden" name="myValue" value="" id="myValue"/>
<a href="#" data-value="1">Item 1</a>
<a href="#" data-value="2">Item 2</a>
<a href="#" data-value="3">Item 3</a>
</form>
Then on click over any link (a
) change its value and submit the form.
$('a').click(function(e){
//preventing the default link redirection
e.preventDefault();
$('#myValue').val($(this).data('value'));
$(this).closest('form').submit();
});
Use a hidden input field like:
<input type="hidden" value="hidden value" name="id"/>
This box is not visible in your page.
Use input fields instead.
<form method="POST" action="localhost/carts/delete">
<input type="submit" value="Item 1" name="whichitem[]" />
<input type="submit" value="Item 2" name="whichitem[]" />
<input type="submit" value="Item 2" name="whichitem[]" />
</form>
Then in PHP you can retrieve the clicked value like this:
$_POST["whichitem"]
If you are worried about the styling, simply add it in your css:
input[type="submit"]{
//style
}
本文标签: phpHow to post to a form using ltagt tag and passing hidden valueStack Overflow
版权声明:本文标题:php - How to post to a form using <a> tag and passing hidden value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744221625a2595891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论