简体   繁体   English

如何通过单击超链接显示php变量的值

[英]How to display the value of a php variable by clicking on a hyperlink

I want to display the balance of the user when he clicks on the hyperlink. 我想在用户单击超链接时显示其余额。 the balance of the user is stored in a php variable. 用户的余额存储在php变量中。 I am hoping that this can be done using javascript but I dont have much experience in javascript. 我希望可以使用javascript完成此操作,但是我在javascript方面没有太多经验。

Below is my code: 下面是我的代码:

$userbalance = $xyz[2];
echo "Your balance stands at  ".$xyz[2];

This thing is working, but I want to display $userbalance by clicking on a link 这个东西正在工作,但是我想通过单击链接来显示$userbalance

I don't know what to put in the anchor tag. 我不知道在锚标记中放什么。

Is it possible that by clicking on the hyperlink the value of the php varibale gets displayed? 通过单击超链接,是否有可能显示php varibale的值?

Can I pass a function to href ? 我可以将函数传递给href吗? or can I use onclick in some way? 还是可以通过某种方式使用onclick

Try this: 尝试这个:

<a href="somepage.php?balance=<?php $balance = $xyz[2]; echo "Your balance stands at  ". $balance; ?>" >Click</a>

By above method you can display balance in url at any pages. 通过以上方法,您可以在任何页面的url中显示余额。 Then if you need to use that balance there just get that value with super global variable $_GET['balance']; 然后,如果需要使用该余额,只需使用超级全局变量$_GET['balance'];即可获得该值$_GET['balance'];

In the most simplest form , this can be done via storing the value in the "data-" attribute that HTML supports and then using Javascript to alert the value upon user's click. 以最简单的形式 ,可以通过将值存储在HTML支持的“ data-”属性中,然后使用Javascript在用户单击时提醒该值来完成此操作。

<p>
  Click to see your account
  <a href="javascript:void();" data-balance="123 €" onclick="window.alert(this.getAttribute('data-balance'))">balance</a>.
</p>

The PHP part of this would be just to echo the correct balance into the correct place in the HTML code above. PHP的一部分只是将正确的平衡返回到以上HTML代码中的正确位置。

<a href="javascript:void();" data-balance="<?= $balance ?>" onclick="window.alert(this.getAttribute('data-balance'))">balance</a>.

Example of the final HTML: https://jsfiddle.net/oh8jnmg2/1/ 最终HTML的示例: https//jsfiddle.net/oh8jnmg2/1/

This is a solution if you don't want to actually use two pages but still show new content and hide everything else: 如果您不想实际使用两个页面,但仍然显示新内容并隐藏其他所有内容,则可以使用此解决方案:

$userbalance = $xyz[2];
echo `Your balance stands at <a href='javascript:{document.documentElement.innerHTML = ""; document.write(` . $xyz[2] . `);}'>see variable</a>`;

If you want just to display a new value below, then use: 如果只想在下面显示一个新值,请使用:

$userbalance = $xyz[2];
echo `Your balance stands at <div id="myDiv"><a href='javascript:{document.getElementById("myDiv").innerHTML = ` . $xyz[2] . `;}'>see variable</a></div>`;

Niltish has given the right hint. 零食给出了正确的提示。 But I wouldn't recommend putting a output string into a link, which gets displayed in the url. 但我不建议将输出字符串放入链接,该链接会显示在url中。 Since HTML is rendered through PHP, you can simply attach your value to the link by typing: 由于HTML是通过PHP呈现的,因此您只需输入以下内容即可将值附加到链接:

<a href="yourprintsite.php?balance=$userbalance>Click here to see your balance</a>

And then on yourprintsite.php, you just type: 然后在yourprintsite.php上,您只需键入:

if (!empty($_GET['balance')) {
  echo "Your balance stands at " . $_GET['balance'];
}

And you're done. 这样就完成了。

Do you can write next code, function getAttribute must you help 您可以编写下一个代码吗,函数getAttribute必须帮助您

<script type="text/javascript">
    function clickHanler(d){
       alert(d.getAttribute("data-userbalance"));
    }
</script>

<a
   data-userbalance="21" 
   href="#" 
   onclick="clickHanler(this);">
        Click to do something
</a>

First, let's start with the HTML , you need to create a <a></a> that routes to no where : 首先,让我们从HTML开始,您需要创建一个<a></a>并路由到no where:

<a href="javascript:void(0)" id="linktobalance">Click to see Balance</a>

Then, in the Javascript part : 然后,在Javascript部分:

  1. We will assign a method to handle the click on our link. 我们将分配一种方法来处理对链接的单击。
  2. we need to use an Ajax call to get the balance from the balance.php page : 我们需要使用Ajax调用来从balance.php页面获取balance

     $("#linktobalance").on("click", function(){ $.post( "balance.php", function(balance) { //In this part, the balance is the value returned from th PHP, you can do whatever with it, I'll alert it alert( "The user balance is "+balance ); }); }); 

Note : we used jQuery , you can add it to your project by doing this : 注意 :我们使用jQuery ,您可以通过执行以下操作将其添加到项目中:

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

 $("#showbalance").click(function(){ $("#balance").show(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <a href="#" id="showbalance">Show Balance</a> <div id="balance" style="display: none;"> Balance = 1000 </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM