1

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 to get my country code from the jquery to perform some validation task, however, i wasn't able to retrieve the country code. Any idea what should i do for it to work? please note that the library is already been called. (I had read up on examples but dont really understand how it works..)

rrk
  • 15,677
  • 4
  • 29
  • 45

1 Answers1

4

It is not possible to directly call PHP function from JavaScript and vice versa. Part of the reason for this is that as @Pekka indicated JS runs clientside (in your browser) while PHP runs server-side (on your server).

It is however possible to use AJAX for your 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);
             // do your handling here
        },
        'error'   : function(data){
            console.log(error);
            // this function is executed on an HTTP error
        }
    });
}
</script>

PHP

This is your php file that would do your validation. In principle it is a different file than the file that outputs the html as shown above.

$countrycode = $_GET['countryCode'];
# handle countrycode
echo "<What you want to return to JS>";

Edit

On the additional question of how to get session variables to js.

Would I navigate to your page in my browser, the following would happen: If I requested a file with an .php extension, the PHP processer gets run on it. This processer identifies all code between the tags <?php and ?> as PHP, and thus runs all this code. If it encounters an echo statement, it echo's out the specific value at the location of the php-block. After PHP is done processing, the page is served (given) to your browser, where it will be interpreted as a combination of HTML/javascript/... Javascript interprets everything between the tags <script> and </script> as javascript.

So, what if we where to make a request to index.php?par=val on your server, and index.php looks like

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

(the json_encode function ensures that the variable is in proper json format. Of course you can change the `$_GET['par']; to be whatever you would like.)

Remember, PHP only cares about whats between the <?php and ?> tags, so this would output:

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

You will need to edit this to match your requirements of course, but this is the basics of passing parameters from PHP to client-side (JS)

MartyB
  • 135
  • 6
  • You might be right however my url contain a get method (eg. test.php?test="A12") before the validation. What about is there any way to get the session from php to jquery? If can, i can do the validation there – Yan Shuang Low Sep 21 '15 at 08:34
  • Take a look at [this](http://stackoverflow.com/questions/4365738/how-to-access-php-session-variables-from-jquery-function-in-a-js-file) page :) – MartyB Sep 21 '15 at 08:38
  • Thank you @MartyB, it seem useful – Yan Shuang Low Sep 21 '15 at 09:00
  • Tried to understand the codes.. but don't really understand since it is quite different from mine. Can help me simplified to my question? – Yan Shuang Low Sep 23 '15 at 01:16
  • You might have misunderstood my question. I want to get data from JS to PHP and not the other way round. – Yan Shuang Low Sep 23 '15 at 08:53
  • Getting data from JS to your clientside is covered by the first part of my answer, before the Edit header. After that I detailed the process the other way arround, as in your first comment to my answer you ask about getting the session from php to javascript? If this is not what you want the I indeed do not understand the question. – MartyB Sep 23 '15 at 10:14
  • Oh no..I confuse myself. But is there any other way of get data from jquery without using of get method/ajax – Yan Shuang Low Sep 25 '15 at 01:56
  • I am afraid you are stuck with AJAX, but you do not have to use a GET request. An alternative is the POST request. To use this, just change the `method = "get";` line to `method="post";`. Any data you want to pass you can include under the key `data` (just add it in the AJAX call object). Data passed here can be of type `string`, `array` or `plainobject` You can read the data in php under `$_POST` – MartyB Sep 25 '15 at 07:29