0

country value from jQuery variable need to insert into URL with PHP encrypt function, how can I use jQuery value into PHP

$("#country_select").change(function(){
    var country = $("#country_select").val();

$(".price_content a").attr("href","signup.php?pack=<?php echo encrypt(2,$key);?>&country="<?php echo encrypt(country,$key);?>);

});

need to add var country into encrypt function...

How can i use?

paary
  • 421
  • 6
  • 16
Dhamu
  • 1,694
  • 5
  • 22
  • 47
  • 3
    Javascript is executed on the client side, php - on the server. Php in your code sample will be executed before JS is. If you need the data encrypted then you could do an ajax call to a php file which returns encrypted values, then use them in the url. – Pankucins Jul 01 '13 at 07:32
  • create a javascript version of the encrypt function, or make an ajax call but i think you dont want to do that, since this will make you still pass the variable unencrypted over the network – Joel Harkes Jul 01 '13 at 07:34
  • [___What is PHP ?___](http://php.net/manual/en/intro-whatis.php) – NullPoiиteя Jul 01 '13 at 07:42
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – NullPoiиteя Aug 18 '13 at 06:45

4 Answers4

2

Your PHP code will only be executed once on the server side when the page is requested. It will not be called again in the change event.

Possible solutions:

  • Do an ajax call to encrypt the value, in the change event
  • Encrypt all values of the select and store them in a data-encrypted attribute in each option
  • Encrypt the values and store them as a Javascript array for lookup later
MrCode
  • 63,975
  • 10
  • 90
  • 112
2

there is no way you can do this directly. It's because PHP is processed before JQuery. PHP on server side, JQuery on client side.

You can use encodeURIComponent as proposed here.

However, if you still insist on processing parameter via PHP, you can still make ajax call to PHP script with input parameter - your desired string to be encoded and PHP will output you encoded string.

Community
  • 1
  • 1
Peter Pivarc
  • 469
  • 2
  • 6
1

Here is your solution:

<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

<script type="text/javascript">
// extend jQuery Ajax and change it
$.extend({
    getValues: function(url, qs) {
        var result = null;
        $.ajax({
            url: url,
            type: 'post', 
            data: qs,           
            async: false,
            success: function(data) {
                result = data;
            }
        });
        return result;
    }
});
$(function(){
    $("#country_select").change(function(){
        var country = $("#country_select").val();
        $(".price_content a").attr("href","signup.php?pack="+$.getValues("encrypt.php",{q:'2'})+"&country="+$.getValues("encrypt.php",{q:country}));
    });
})

</script>
<select name="country_select" id="country_select">
    <option value="">Select country</option>
    <option value="IND">India</option>
    <option value="SL">Sri Lanka</option>
    <option value="PAK">Pakistan</option>
</select>

<div class="price_content"><a href="">Click</a></div>
Nono
  • 6,986
  • 4
  • 39
  • 39
0

Well, PHP is a server-side language, and Javascript is client side, which means, you can't pass variables to php without a post. You can use an AJAX post, if you don't want to refresh the page. On the other hand, it is fairly easy to use PHP variables in jQuery, so if that is reasonable for your work, you should consider that.

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
TheScarecrow
  • 153
  • 9