简体   繁体   English

将变量从javascript传递到php

[英]passing a variable from javascript to php

I'm trying to pass a variable from a js function to php form (in the same file). 我试图将变量从js函数传递到php形式(在同一文件中)。 Here is my form : 这是我的表格:

 <div id="hoursDetails" class="toogle_form" style="display:none">
      <table border=0>
            <?php
                 for ($i=0 ; $i<$N; $i++){ //$N is the variable I want to retrieve
                 echo '<tr>
            <td><div id="date'.$i.'" ></div></td>   
            <td>
             <input type=text name="action'.$i.'"  placeholder="Votre action"/>
             <input type="text" name="temps'.$i.'"  placeholder="nombre heures"/>
            </td>
                </tr> ';}
              ?>
      </table>
</div>

I tried to create a hidden input in the form : 我试图创建以下形式的隐藏输入:

<input type="hidden" id="variable" name="variable" value="" >

And to set the value from the js function using $('#variable').val(10) for example, but still don't know how to retrieve this variable when looping. 并使用$('#variable').val(10)设置js函数的值,但是在循环时仍然不知道如何获取此变量。 I can retrieve this variable from the controller, but is it possible to pass it from controller to the view without using index() function? 我可以从控制器中检索此变量,但是可以在不使用index()函数的情况下将其从控制器传递到视图吗? (I'm using CodeIgniter Could anyone help me please ? Thanks (我正在使用CodeIgniter,有人可以帮我吗?谢谢

You can't pass a variable from Js to PHP without making a request. 您不能在没有请求的情况下将变量从Js传递给PHP。 Condiser that PHP is evaluated on server side, then when rendering on client side, Javascript triggers. 认为PHP是在服务器端评估的,然后在客户端渲染时,会触发Javascript。 If you need to pass a value from Js to PHP, you should try to use Ajax. 如果需要将值从Js传递给PHP,则应尝试使用Ajax。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM