【问题标题】:javascript value in input post method to php not working [duplicate]php输入后方法中的javascript值不起作用[重复]
【发布时间】:2018-12-15 02:14:24
【问题描述】:

我的表单中的一个输入由变量 javascript 完成。我不明白为什么我的 php 代码没有看到这个变量? (如果我手动输入值,它将起作用)

 <?php $kwota2 = $_POST['kwota2']; // undefinded kwota2 ?? ?>

    <form action="post>
    <input id="kwota2" type="text" name="kwota2" disabled>

    <script>
           $("#kwota2").val(localStorage.getItem('sumalist'));
    </script>
<input type="submit" value="Zamawiam">
                    </form>

【问题讨论】:

    标签: php jquery


    【解决方案1】:

    这是第一种方法。您将使用 jQuery 序列化 disabled 的对象。

    $('form').on('submit', function(e) {
      e.preventDefault();
      
      var data = $('form').serializeArray();
      $(':disabled').each(function(i, item) {
        data.push({
          name: item.name,
          value: $(this).val()
        });
      });
      console.log(data);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form action="post">
      <input id="kwota2" type="text" name="kwota2" value="example" disabled>
      <input type="submit" value="Zamawiam">
    </form>

    第二种方法是添加只读属性。disabled和readonly的唯一区别是readonlyserializable。

    <form action="post">
      <input id="kwota2" type="text" name="kwota2" value="example" readonly>
      <input type="submit" value="Zamawiam">
    </form>
    

    还有: How to make $.serialize() take into account those disabled :input elements?

    【讨论】:

      【解决方案2】:

      它不起作用,因为它已将其标记为已禁用 将其转换为只读并尝试

      并在文档准备功能中添加您的 javascript 代码

      【讨论】:

      • 我必须保证这个输入不能被编辑。
      • @A.Jo 客户端没有什么是安全的。您需要在服务器上进行验证。
      猜你喜欢
      • 2019-07-08
      • 2015-09-12
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2020-08-17
      • 2014-05-13
      相关资源
      最近更新 更多