[英]Pass variable via Javascript
How can I pass a variable ( $member_id
) to the target php page(Member Profile details Query)? 如何将变量( $member_id
)传递给目标php页面(会员档案详细信息查询)?
///-----
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. *整个想法是显示/显示SELECTED成员资料详细信息。 * *
You are using single quotes. 您正在使用单引号。 Single quotes do not parse PHP variables. 单引号不解析PHP变量。 When outputting multiline HTML from PHP, use heredoc : 从PHP输出多行HTML时,请使用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
. 这将创建一个类似“ show_profile.php?member_id=3
”的链接,如果单击该链接,将使用$_GET['member_id'] = 3
调用脚本。 You can even get the page content via a XHR call inside your click
event : 您甚至可以通过click
事件中的XHR调用获取页面内容:
$.get($(this).attr('href'), function(data) { /* process data here */ });
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添加了查询字符串键/值,因此您可以访问PHP页面上的$ .GET变量以检索member_id:
$member_id = $.GET["member_id"]; $ member_id = $ .GET [“member_id”];
Example of the AJAX functionality you're looking for: 您正在寻找的AJAX功能示例:
$.GET("show_profile.php?member_id=<your_member_id>", null, function(data){
$("#show_profile_box").html(data);
});
Do you mean something like 你的意思是什么?
<script type="text/javascript">
var member_id = <?= $member_id ?>;
</script>
? ?
If I am getting you correctly, you need to do this in php: 如果我正确地告诉你,你需要在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. 我假设您希望在某些特定操作(如单击)上执行此操作,因此您在onlick处理程序中指定了一个js函数,该函数获取全局JS变量memberID的值并对您的其他页面进行AJAX调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.