Kumar Shanmugam
Kumar Shanmugam

Reputation: 61

How to pass the javascript variable value to php variable?

this is my javascript code

<script type="text/javascript">
    function MyFunc(lang){
    var ll = lang;              
    //alert(ll);
    }
</script>

my html code

<form  method="post" action="mypage.php?>">
   <div>    
    <input type='image' src='/images/flags/us.png' name='US' value='en_EN' onclick='MyFunc(this.value)'/>   
<input type='image' src='/images/flags/it.png' name='IT' value='it_IT' onclick='MyFunc(this.value)'/> 
<input type='image' src='/images/flags/fr.png' name='FR' value='fr_FR' onclick='MyFunc(this.value)'/> 
    </div>

now how can i send the javascript var ll value to mypage.php

Actually I want the image alt value to pass it, How it is possible please give some idea..

Upvotes: 3

Views: 16332

Answers (3)

davidkonrad
davidkonrad

Reputation: 85588

create a hidden field inside the <form>

<input type="hidden" name="ll" id="ll">

in javascript

<script type="text/javascript">
    function MyFunc(lang){
        var ll=document.getElementById('ll');
        ll.value=lang;
    }
</script>

and then you have ll in PHP, when you submit the form.


update

I guess you wonder if you have links instead of a form, like this? :

<a href="#" class='lang'><img src="images/flags/it.png" alt="it_IT" /></a>
<a href="#" class='lang'><img src="images/flags/fr.png" alt="fr" /></a>
<a href="#" class='lang'><img src="images/flags/us.png" alt="en_EN" /></a>

And you just want to reload the page with ll containing the desired language code?

<script type="text/javascript">
$('.lang').click(function() {
    var lang = $(this).find('img').attr('alt');
    document.location.href='mypage.php?ll='+lang;
});
</script>

Will reload your page, eg ex mypage.php?ll=it_IT. As with the form accessible in mypage.php through $_GET['ll']

Using jQuery here, since you have it on your tag-list (and it was the far easiest / fastest to produce :)

Upvotes: 5

gabrielgmm
gabrielgmm

Reputation: 9

You can try use Json:

1 - First. Save your page in PHP. 2 - Second. Modify your function in Javascript

Example

    var ll = lang;


            //caminho do ar



       $.getScript("http://www.site.com.br/busca_dados3.php?value="+ll, function(){

}

In this case, you want to create a file called 'busca_dados3.php' who receive the variables in $_REQUEST['value'];

You need Jquery to.

Upvotes: -1

Paolo Gibellini
Paolo Gibellini

Reputation: 320

Give a name to your form, and use a hidden input to be posted to your php file

<script type="text/javascript">
    function MyFunc(lang){
        //var ll = lang;              
        document.my_form.my_var.value=lang;
    }
</script>


<form name="my_form" method="post" action="mypage.php">
   <div>    
      <input type='image' src='/images/flags/us.png' name='US' value='en_EN' onclick='MyFunc(this.value)'/>   
      <input type='image' src='/images/flags/it.png' name='IT' value='it_IT' onclick='MyFunc(this.value)'/> 
      <input type='image' src='/images/flags/fr.png' name='FR' value='fr_FR' onclick='MyFunc(this.value)'/> 
   </div>
   <input type="hidden" name="my_var">
</form>

In your PHP file you can retrieve your data in $_POST array:

$my_data=$_POST["my_var"];

Upvotes: 0

Related Questions