javascript 如何将javascript变量值传递给php变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18614270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:36:19  来源:igfitidea点击:

How to pass the javascript variable value to php variable?

javascriptphpjquery

提问by Kumar Shanmugam

this is my javascript code

这是我的 javascript 代码

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

my html code

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

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

现在我如何将 javascriptvar ll值发送到mypage.php

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

实际上我希望图像 alt 值传递它,怎么可能请给出一些想法..

回答by davidkonrad

create a hidden field inside the <form>

在里面创建一个隐藏字段 <form>

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

in javascript

在 JavaScript 中

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

and then you have llin PHP, when you submit the form.

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



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 llcontaining the desired language code?

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

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

将重新加载您的页面,例如 ex mypage.php?ll=it_IT。与通过 mypage.php 访问的表单一样$_GET['ll']

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

在这里使用 jQuery,因为你在你的标签列表上有它(而且它是最容易/最快的:)

回答by Paolo Gibellini

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

为您的表单命名,并使用隐藏输入发布到您的 php 文件

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

在您的 PHP 文件中,您可以检索 $_POST 数组中的数据:

$my_data=$_POST["my_var"];

回答by gabrielgmm

You can try use Json:

您可以尝试使用 Json:

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

1 - 首先。用 PHP 保存您的页面。2 - 第二。在 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'];

在这种情况下,您要创建一个名为“busca_dados3.php”的文件,该文件接收 $_REQUEST['value'] 中的变量;

You need Jquery to.

你需要Jquery。