Search code examples
phpjavascriptprototypejs

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/

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 ='&laquo; 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 ='&laquo; 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 ='&laquo; 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 ='&laquo; 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 ='&laquo; 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 ='&laquo; Select Options'" onmouseover="$('Text6').innerHTML ='Get Current %'"/></a></span>
<span id="Text6" style="font-weight:bold">&laquo; 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. I did tens of tries with GET and POST javascript (hidden fields, forms, anchor, onclick event etc) with no success. 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?


Solution

  • Instead of the alert, try this ajax call,

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