dougua3705 2018-12-24 05:46
浏览 73

submit()重复执行,php只接收默认值

I have a submit form using POST method, without a submit button.

<form action="" method="post" id="submit_cookie_form">
   <input type="hidden" name="lang" value="" id="cookie_form">
</form>

My goal:

  1. Pass value to input hidden value attribute[*1]
  2. Set the code to be executed/triggered when a value is passed to value attribute[*2]
  3. Submit the form when a value is passed to value attribute[*3]=>ISSUE
  4. Pass Javascript value('en' or 'jp') to php variable. [*4]=>ISSUE

[*3] => When I use submit() as follows to [*3], the page is being reloaded forever. It seems like the form is being submitted forever. Why does this happen, and how can I fix the code?

[*4] => Why does php only receive 'en' value? Although, value attribute of <input type="hidden" name="lang" value="" id="cookie_form"> perfectly recieves 'en' and 'jp' according to user's clicks on the webpage. How can I fix this?

 let latestCookie;

    $(document).on('ready', function(){

       if (localStorage.getItem("language") === null || localStorage.getItem("language") === flag.en){
         document.cookie = "language=en";

       } else if (localStorage.getItem("language") === flag.jp){
         document.cookie = "language=jp";
       }

        latestCookie = getCookieValue('language');
        document.getElementById('cookie_form').value = latestCookie; //[*1]
        setLang(latestCookie); //[*2]
     })


      $('#cookie_form').change(function(){
          $('#submit_cookie_form').submit(); }); //[*3]

      function setLang(chosenLang){
          $('#cookie_form').val(chosenLang).trigger('change');
        }

   <?php 
         if(isset($_POST['lang'])){     
           $lang=$_POST['lang'];   //[*4]
           echo $lang;
         }
         else{
           echo $lang;
         }
   ?>
  • 写回答

1条回答 默认 最新

  • doukuang6795 2018-12-24 10:46
    关注
    $('.dropdown-menu a img').on('click', function(){ 
        let flag = $(this).attr('src'); 
        $('#selected').attr('src', flag); 
        let key = $('#selected').attr('src'); 
        localStorage.setItem("language", key); 
        refreshPage(key); 
    });
    function refreshPage(key){
        window.location.href = '<php echo $_SERVER[\'PHP_SELF\']."?lang="; ?>'+key;
    }
    

    then in PHP you can get in GET method as a querystring.

    <?php 
         if(isset($_REQUEST['lang'])){     
           $lang=$_REQUEST['lang'];   //[*4]
           echo $lang;
         }
         else{
           echo $lang;
         }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真