The script below works fine, it combine JavaScript and PHP.
By pressing the button I get its value (buttonValue = 20) and the existing item list (A,B,C) is being replaced by the new list that stopped when $i <= 10 (1,2,3… 10):
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var buttonValue = $(':button').val();
alert(buttonValue);
$("#City > option").remove(); // remove all items from list
<?php $i = 1; while ($i <= 10): ?>
$('<option><?php echo $i; ?></option>').appendTo('#Country select');
<?php $i++; endwhile; ?>
});
});
</script>
<body>
<button value="20">click to start</button>
<div id="Country">
<select id="City">
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
</div>
</body>
I would like to change the PHP loop so that instead $i <= 10, it will show $i <= $the_buttonValue (assign the button value to show numbers 1,2,3… 20).
I've tried jQuery AJAX functions: load(), get() or the ajax() method, to send the variable to the server but that doesn't seems to solve the above request.
I know that the two sides of PHP and JavaScript are communicate differently and that maybe an AJAX request can get the new requirement to work, but I’m not sure how.
Any tip to make it works as requested, would be greatly appreciated.
value="20")?20coming from? A client-side input? Is it set byvalue="<?php echo $value; ?>"? Is it just static text in your HTML?