I have a site that appends a textbox to a div when the user clicks a button. The textbox works great. Now, I would like to generate a unique number for the id of each box. The following code is giving me 0 as the id of each box. I’m trying to get them to go 1,2,3,4…
Code:
<?php $x=0; ?>
$("#add").click(function(){
$('#boxes').append(<input type="text" id="<?php echo $x; $x++; ?>">);
});
PHP code is excuted earlier before the javascript code is executed. But in your code example you try to execute both at the same time. That is just not the case.
Instead of a PHP variable you need to use a javascript variable: