最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to pass the javascript variable value to php variable? - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Dec 18, 2013 at 0:41 davidkonrad 85.6k17 gold badges209 silver badges271 bronze badges asked Sep 4, 2013 at 12:48 Kumar ShanmugamKumar Shanmugam 611 gold badge2 silver badges5 bronze badges 2
  • I remend jQuery AJAX and a bit of googling or to consider a form since you are submitting data. – pedro_sland 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. – Spudley Commented Sep 4, 2013 at 12:51
Add a ment  | 

3 Answers 3

Reset to default 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 :)

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

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

发布评论

评论列表(0)

  1. 暂无评论