javascript toggle based on php header get value?
I have two links which show / hide div sections (inside eac开发者_如何学JAVAh div section i am running sql queries)
<li><a href="javascript:toggle('subscription')"><b>Subscription Details</b></a></li>
<li><a href="javascript:toggle('stusearch')"><b>Personal Information</b></a></li>
while getting query results back to original page, i would be passing parameters via url and would like to receive them on usrsecuredpage.php which contains the various javascript toggles which i would like to show/hide based on the header i get.
for example is i get
header('Location: usrsecuredpage.php?id=personalinfo');
i would like to toggle "stusearch"
if the header is
header('Location: usrsecuredpage.php?id=subscriptioninfo');
then toggle "subscription"
Please help
It doesn't work like that. The PHP code runs on the server. Javascript runs on the client.
If all you want to do is a simple redirect, do it with Javascript.
function toggle(name) {
window.location = "usrsecuredpage.php?id=" + name;
}
I believe you want
function get(name){
if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
}
then change the html to
<li><a href="toggle(get('id'))"><b>Subscription Details</b></a></li>
<li><a href="toggle(get('id'))"><b>Personal Information</b></a></li>
In php header sends headers... If you are visiting usrsecuredpage.php and want to know what id is you need to use the $_GET superglobal.
if ($_GET['id'] == 'personalinfo') {
echo '<div>...';
}
else if ($_GET['id'] == 'subscriptioninfo') {
echo '<div>...';
}
Update:
If I'm understanding you correctly, you want the divs on the page regardless and the appropriate div to show up based on the id passed to the php script. If so, you could do this at the bottom of your page.
<script type="text/javascript">
<?php if ($_GET['id'] == 'personalinfo'): ?>
toggle('stusearch');
<?php elseif ($_GET['id'] == 'subscriptioninfo'): ?>
toggle('subscription');
<?php endif; ?>
</script>
It's kinda an ugly solution but it works.
精彩评论