How to detect how many time the users have click the button...
Posted
by Jerry
on Stack Overflow
See other posts from Stack Overflow
or by Jerry
Published on 2010-05-28T01:32:59Z
Indexed on
2010/05/28
1:41 UTC
Read the original article
Hit count: 265
Hello guys. Just want to know if there is a way to detect how many times a user has clicked a button by using Jquery.
My main application has a button that can add input fields depend on the users. He/She can adds as many input fields as they need. When they submit the form, The add page will add the data to my database. My current idea is to create a hidden input field and set the value to zero. Every time a user clicks the button, jquery would update the attribute of the hidden input field value. Then the "add page" can detect the loop time. See the example below.
I just want to know if there are better practices to do this. Thanks for the helps.
main page
<form method='post' action='add.php'>
//omit
<input type="hidden" id="add" name="add" value="0"/>
<input type="button" id="addMatch" value="Add a match"/>
//omit
</form>
jquery
$(document).ready(function(){
var a =0;
$("#addMatch").live('click', function(){
$('#table').append("<input name='match"+a+"Name' />") //the input field will append //as many as the user wants.
a++;
$('#add').attr('name', 'a'); //pass the a value to hidden input field
return false;
});
Add Page
$a=$_POST['a']; //
for($k=0;$k<$a;$k++){
//get all matchName input field
$matchName=$_POST['match'.$k.'Name'];
//insert the match
$updateQuery=mysql_query("INSERT INTO game (team)
values('$matchName')",$connection);
if(!$updateQuery){
DIE('mysql Error:'+mysql_error());
}
© Stack Overflow or respective owner