3

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..

2
  • I recommend jQuery AJAX and a bit of googling or to consider a form since you are submitting data. Commented Sep 4, 2013 at 12:50
  • 1
    Read through the questions listed as "Related" on the panel on the right. There are plenty of similar questions already. Commented Sep 4, 2013 at 12:51

3 Answers 3

5

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 :)

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot. Its work perfectly to my task. thank you so much.
hi david, I have one more query to you if i use anchor tag and href class like this mean how can get the my requirement '<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>' will you help this one? thank in advance
0

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"];

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.