javascript - The sessions only store data for a second before being changed the last sessions value -
when click sort date created
button, string date
passed function sort()
, alert 1 (see code) prints string date
. string date
stored in session in first if
statement , alert 2 prints date
. issue date
being stored temporarily , alert 3 alerts service
, no matter type. if change order of if statements, last if statement's string somehow stored in session.
<?php session_start(); ?> <html> <head> <script> function sort(type){ alert('<?php echo $_session['sort']; ?>'); ///alert 1 if (type == 'date'){ <?php $_session['sort'] = 'date'; ?> alert('<?php echo $_session['sort']; ?>'); ///alert 2 } else if (type == 'cost'){ <?php $_session['sort'] = 'cost'; ?> alert('<?php echo $_session['sort']; ?>'); } else if (type == 'service'){ <?php $_session['sort'] = 'service'; ?> alert('<?php echo $_session['sort']; ?>'); } alert('<?php echo $_session['sort']; ?>'); ///alert 3 } </script> </head> <body> <input type="button" onclick="sort('date');" value="sort date created"> <input type="button" onclick="sort('cost');" value="sort cost"> <input type="button" onclick="sort('service');" value="sort service"> </body> </html>
you confused difference between client side , server side code. have written following code:
alert('service'); <?php $_session['sort'] = 'date'; $_session['sort'] = 'cost'; $_session['sort'] = 'service'; ?> if (type == 'date'){ alert('service'); ///alert 2 } else if (type == 'cost'){ alert('service'); } else if (type == 'service'){ alert('service'); }
php not respect javascript if
statements. php compile static web page latter part of code have written.
i feel answer trying explain difference between client-side , server-side code little broad , long-winded stackoverflow, there plenty resources out there on topic.
Comments
Post a Comment