Pass variable via Javascript
How can I pass a variable ($member_id
) to the target php page(Member Profile details Query)?
///-----
echo '<div id="show_profile_box">Show Member Profile details
//MySQL Query to display member profile details</div>
<div id="display_member_id"><a href="show_profile.php"?member_id=$member_id">Display Member id</a></div>';
echo '<script>
$("#show_profile_box").hide(100);
$("#display_member_id").click(function () {
$("#show_profile_b开发者_运维百科ox").show(500);
});
</script>';
Thank you. Let me know if you need more info.
* The whole idea is to display/show SELECTED member profile details. *
You are using single quotes. Single quotes do not parse PHP variables. When outputting multiline HTML from PHP, use heredoc :
echo <<< END_OF_HTML
<div id="show_profile_box">Show Member Profile details
<!--MySQL Query to display member profile details -->
</div>
<div id="display_member_id">
<a href="show_profile.php?member_id={$member_id}">Display Member id</a>
</div>
<script>
$("#show_profile_box").hide(100);
$("#display_member_id").click(function () {
$("#show_profile_box").show(500);
});
</script>
END_OF_HTML;
Also, note that the variable do not have to be enclosed by {}
but still a good practice to do so.
This will create a link like "show_profile.php?member_id=3
" that, if you click on the link, will call the script with the $_GET['member_id'] = 3
. You can even get the page content via a XHR call inside your click
event :
$.get($(this).attr('href'), function(data) { /* process data here */ });
Do you mean something like
<script type="text/javascript">
var member_id = <?= $member_id ?>;
</script>
?
You've added a querystring key/value for member_id so you can access the $.GET variable on your PHP page to retrieve the member_id as such:
$member_id = $.GET["member_id"];
Example of the AJAX functionality you're looking for:
$.GET("show_profile.php?member_id=<your_member_id>", null, function(data){
$("#show_profile_box").html(data);
});
If I am getting you correctly, you need to do this in php:
echo '<script type="text/javascript">memberID =' . $memberID . ';></script>;
I assume that you want to do this on some specific action like click so you specify a js function in the onlick handler which takes the value of the global JS variable memberID and makes an AJAX call to your other page.
精彩评论