How to call function from jquery to php? -


php:

$product_code = $_get['product_code']; $countrycode = getcountrycode(); <script src="js/register_script.js" type="text/javascript"></script> 

jquery:

function getcountrycode() {     countrycode = qrcode_getinfo(qrcode1,"mfg_c_a_code",0)     return countrycode; }  

i want country code jquery perform validation task, however, wasn't able retrieve country code. idea should work? please note library been called. (i had read on examples dont understand how works..)

it not possible directly call php function javascript , vice versa. part of reason @pekka indicated js runs clientside (in browser) while php runs server-side (on server).

it possible use ajax purposes:

html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  <script type="text/javascript"> function getcountrycode() {     countrycode = qrcode_getinfo(qrcode1,"mfg_c_a_code",0)     return countrycode; }     function doajax(){     var countrycode = getcountrycode();     $.ajax({         'url'     : '<the_url_to_your_php>?countrycode=' + countrycode,         'method'  : 'get',         'success'  : function(data){              console.log(data);              // handling here         },         'error'   : function(data){             console.log(error);             // function executed on http error         }     }); } </script> 

php

this php file validation. in principle different file file outputs html shown above.

$countrycode = $_get['countrycode']; # handle countrycode echo "<what want return js>"; 

edit

on additional question of how session variables js.

would navigate page in browser, following happen: if requested file .php extension, php processer gets run on it. processer identifies code between tags <?php , ?> php, , runs code. if encounters echo statement, echo's out specific value at location of php-block. after php done processing, page served (given) browser, interpreted combination of html/javascript/... javascript interprets between tags <script> , </script> javascript.

so, if make request index.php?par=val on server, , index.php looks like

<script type="text/javascript">     function dostuff(){         var param = <?php echo json_encode($_get['par']);?>;     } </script>   

(the json_encode function ensures variable in proper json format. of course can change `$_get['par']; whatever like.)

remember, php cares whats between <?php , ?> tags, output:

<script type="text/javascript">     function dostuff(){         var param = "val";     } </script> 

you need edit match requirements of course, basics of passing parameters php client-side (js)


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -