如何将javascript变量值传递给php变量? [英] How to pass the javascript variable value to php variable?

查看:90
本文介绍了如何将javascript变量值传递给php变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的javascript代码

this is my javascript code

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

我的HTML代码

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

现在如何发送javascript var ll mypage.php

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

实际上我想要图像alt值传递它,如何可能请给出一些想法..

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

推荐答案

< form>

create a hidden field inside the <form>

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

javascript中的

in javascript

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

然后你在PHP中有 ll ,当你提交表格时。

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

我猜你想知道你是否有链接而不是表格,像这样? :

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>

你只想用 ll重新加载页面包含所需的语言代码?

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>

会重新加载你的页面,例如ex mypage.php?ll = it_IT 。与mypage.php中可访问的表单一样,通过 $ _ GET ['ll']

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

在此处使用jQuery,你有它在你的标签列表上(这是最简单/最快的产品:)

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

这篇关于如何将javascript变量值传递给php变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆