[英]Pass client-side javascript variable to server-side php
I try again to ask my question. 我再试一次问我的问题。 I hope that this time it's clear enough.
我希望这次很清楚。
Here's the live demo of my script: http://www.virusgaming.net/test/ 这是我的脚本的实时演示: http : //www.virusgaming.net/test/
And this is the code: 这是代码:
<? // I set the starting percentage to 0%.
$something = 0; ?>
<!-- jsProgressBarHandler prerequisites : prototype.js -->
<script type="text/javascript" src="js/prototype/prototype.js"></script>
<!-- jsProgressBarHandler core -->
<script type="text/javascript" src="js/bramus/jsProgressBarHandler.js"></script>
<span style="color:#006600;font-weight:bold;">Multi Color Bar</span> <br/>
<span id="element6">Loading...</span>
<span class="extra"><a href="#" onclick="manualPB2.setPercentage('0', true);return false;"><img src="images/icons/empty.gif" alt="" title="" onmouseout="$('Text6').innerHTML ='« Select Options'" onmouseover="$('Text6').innerHTML ='Empty Bar'"/></a></span>
<span class="options"><a href="#" onclick="manualPB2.setPercentage('+10');return false;"><img src="images/icons/add.gif" alt="" title="" onmouseout="$('Text6').innerHTML ='« Select Options'" onmouseover="$('Text6').innerHTML ='Add 10%'"/></a></span>
<span class="options"><a href="#" onclick="manualPB2.setPercentage('-5');return false;"><img src="images/icons/minus.gif" alt="" title="" onmouseout="$('Text6').innerHTML ='« Select Options'" onmouseover="$('Text6').innerHTML ='Minus 5%'" /></a></span>
<span class="options"><a href="#" onclick="manualPB2.setPercentage('30');return false;"><img src="images/icons/set.gif" alt="" title="" onmouseout="$('Text6').innerHTML ='« Select Options'" onmouseover="$('Text6').innerHTML ='Set 30%'"/></a></span>
<span class="options"><a href="#" onclick="manualPB2.setPercentage('98'); manualPB2.setPercentage('30'); return false;"><img src="images/icons/fill.gif" alt="" title="" onmouseout="$('Text6').innerHTML ='« Select Options'" onmouseover="$('Text6').innerHTML ='Fill 98%, and then 30%'" /></a></span>
<span class="getOption"><a href="#" onclick="alert(manualPB2.getPercentage());return false;"><img src="images/icons/get.gif" alt="" title="" onmouseout="$('Text6').innerHTML ='« Select Options'" onmouseover="$('Text6').innerHTML ='Get Current %'"/></a></span>
<span id="Text6" style="font-weight:bold">« Select Options</span>
<script type="text/javascript">
document.observe('dom:loaded', function() {
manualPB2 = new JS_BRAMUS.jsProgressBar(
$('element6'),
<? echo $something; ?>,
{
barImage : Array(
'images/bramus/percentImage_back4.png',
'images/bramus/percentImage_back3.png',
'images/bramus/percentImage_back2.png',
'images/bramus/percentImage_back1.png'
),
onTick : function(pbObj) {
switch(pbObj.getPercentage()) {
case 70:
alert('we are at 70%');
break;
case 100:
alert('we are at 100%');
break;
}
return true;
}
}
);
}, false);
</script>
I want to pass the client-side javascript percentage to server-side php variable. 我想将客户端JavaScript百分比传递给服务器端php变量。 I did tens of tries with GET and POST javascript (hidden fields, forms, anchor, onclick event etc) with no success.
我使用GET和POST javascript(隐藏字段,表单,锚点,onclick事件等)进行了数十次尝试,均未成功。 Basically with the onclick="alert(manualPB2.getPercentage()); function i stamp the current percentage in an alert. Is possible edit this alert so that it post/get the variable to php?
基本上使用onclick =“ alert(manualPB2.getPercentage());函数,我可以在警报中标记当前百分比。可以编辑此警报,以便将变量发布/获取到php吗?
Instead of the alert, try this ajax call, 尝试使用此ajax调用来代替警报,
var url = 'YourServerSideScript.php';
var pars = 'percentage=' + pbObj.getPercentage(); // you have to use $_POST['percentage'] on your script
var target = 'targeDivId';
var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars});
As a variation on jose's answer (there's no need to use Ajax.Updater
unless you're expecting an HTML response) Prototype really does provide everything you need. 作为jose回答的一种变体(除非期望HTML响应,否则无需使用
Ajax.Updater
)Prototype确实可以提供所需的一切。
document.observe('dom:loaded', function() {
// after existing initialising code...
$$('.getOption a').first().observe('click', function(event) {
new Ajax.Request('YourServerSideScript.php', {
parameters: {
percentage: parseInt(manualPB2.getPercentage())
}
});
event.stop();
});
});
...and the value will be $_POST['percentage']
in your script. ...并且您的脚本中的值将为
$_POST['percentage']
。 I didn't set method
since it will be POST anyway, and parameters
is an object which means Prototype will urlencode it for you, so saving the effort. 我没有设置
method
因为无论如何它都将是POST,而parameters
是一个对象,这意味着Prototype将为您urlencode,因此节省了工作量。 Remember to read the manual . 请记住阅读手册 。
parseInt
will strip out any %
characters if they exist in the percentage. parseInt
会删除百分比中存在的所有%
字符。 event.stop()
neatly prevents the anchor's normal click behaviour. event.stop()
巧妙地阻止了锚点的正常点击行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.