0

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_box").show(500); 
    });
    </script>';

Thank you. Let me know if you need more info.

* The whole idea is to display/show SELECTED member profile details. *

8
  • How is this question related to jQuery or MySQL? Is $member_id PHP variable? Commented Aug 4, 2011 at 11:29
  • From my very little experience with PHP, it should be something like ...<a href="show_profile.php"?member_id=' .$member_id . '">..... Commented Aug 4, 2011 at 11:32
  • Remember the page doesn't get refreshed so it has to be added via Ajax but I can't figure it out. Commented Aug 4, 2011 at 11:34
  • So where exactly is $member_id defined? What is its value? I thought it's existing variable with predefined value, sorry. Commented Aug 4, 2011 at 11:36
  • possible duplicate of What is the safest way of passing arguments from server-side PHP to client-size JavaScript Commented Aug 4, 2011 at 11:39

4 Answers 4

2

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 */ });
Sign up to request clarification or add additional context in comments.

2 Comments

Tried it. On clicking the link goes to show_profile.php page. This is not what I want. I want it to show show_profile_box with which contains <!--MySQL Query to display member profile details -->. Thank you.
@Frank, your click event must return false if you don't want to follow the link specified within it's href attribute.
0

Do you mean something like

<script type="text/javascript">
var member_id = <?= $member_id ?>;
</script>

?

Comments

0

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);
});

2 Comments

Yes but how do I pass this variable to the php page via javascript?
If you don't want to send the user on to another page (display it inside your #show_profile_box) then you'll have to use AJAX to get the information from the server and display it. See: api.jquery.com/jQuery.get
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.