I am adding some javascript code into my php file like as following:
This is the action part:
<script type="text/javascript">
function show_product(){
<?php foreach($products as $p){ ?>
if("<?php echo $p->category ?>" == "Smartphone")
document.write('<?php echo ($p->name); ?>');
<?php } ?>
}
</script>
This is the button part:
<?php
$js = 'onClick="show_product()"';
echo form_button('change', 'Search', $js);
?>
But, the function is calling after a button calling which is placed on the same place and all the data are available at first, still my page show loading after clicking the button, though it shows the perfect result what I desired.
Can anyone help me to find out what the problem is?
Can u pass the $products array from php by encoding it into JSON and use as array in javascript. Use that array in for or foreach loop to get the data.. so that u no need to use more php tags inside it.. it wll work…
use json_encode to convert it into json in php before sending to view template where u have javascript