【问题标题】:Send value of div on form submit在表单提交时发送 div 的值
【发布时间】:2014-01-21 19:44:03
【问题描述】:

我正在尝试提交一个包含几个不同输入的表单,它们都可以正常工作。但是,其中一个输入是文本区域(有点)。我不得不将其更改为内容可编辑的 div,主要是因为我创建了自己的粗体、斜体和下划线按钮,这些按钮不适用于普通文本区域。问题是提交时的表单没有将文本发送到 php,我想知道我可以做些什么来获取 php 中 div 的值。

这里是“文本区域”:

<div id="dash_new_shout_textarea" name="dash_new_shout_textarea" 
     class="dash_new_shout_textarea" contenteditable="true" 
     placeholder="Write your shout..." name="dash_new_shout_textarea" 
     value="<?php echo isset($_POST['dash_new_shout_textarea']) ?
     $_POST['dash_new_shout_textarea'] : '' ?>"></div>

表单只是一个带有method=post的普通表单。

这里是php:

$newShoutTextarea = $_POST['dash_new_shout_textarea'];

感谢您的帮助

【问题讨论】:

标签: javascript php html


【解决方案1】:

我建议你遵循其他答案并使用 jQuery,但由于你的问题中没有 jQuery 标签,我想我应该为你提供一个好的非 jQuery 解决方案。

HTML

<form onsubmit="prepareDiv()">
    <div id="dash_new_shout_textarea" class="dash_new_shout_textarea" contenteditable="true" placeholder="Write your shout..." name="dash_new_shout_textarea" value="<?php echo isset($_POST['dash_new_shout_textarea']) ? $_POST['dash_new_shout_textarea'] : '' ?>"></div>
    <input type="hidden" id="dash_new_shout_textarea_hidden" name="dash_new_shout_textarea" />
</form>

Javascript

function prepareDiv() {
    document.getElementById("dash_new_shout_textarea_hidden").value = document.getElementById("dash_new_shout_textarea").innerHTML;
}

您的 PHP 保持不变。

【讨论】:

    【解决方案2】:

    你可以这样做:

    <div id="dash_new_shout_textarea"></div>
    <input type="hidden" name="dash_new_shout_textarea" id="dash_new_shout_textarea_hidden" />
    <script type="text/javascript">
    setInterval(function () {
      document.getElementById("dash_new_shout_textarea_hidden").value = document.getElementById("dash_new_shout_textarea").innerHTML;
    }, 5);
    </script>
    

    【讨论】:

      【解决方案3】:

      问题在于 DIV 不是表单元素,无法发布。如果你使用一些 javascript,你可以用数据填充一个隐藏的输入字段,然后在服务器端接收它作为 POST 变量

      【讨论】:

        【解决方案4】:

        您可以在表单中添加隐藏输入并使用 jquery 更新它

        <div id="dash_new_shout_textarea" name="dash_new_shout_textarea" 
         class="dash_new_shout_textarea" contenteditable="true" 
         placeholder="Write your shout..." name="dash_new_shout_textarea" 
         value="></div>
        
        <form id="target" method="post" action="destination.php">
          <input id="textarea_hidden" name="textarea_hidden" type="hidden" value="">
          <input type="submit" value="Submit">
        </form>
        

        脚本

        $("#target").click(function() {
          $("#textarea_hidden").val($("#dash_new_shout_textarea").val());
        });
        

        【讨论】:

        • 我忘了指出这需要 jQuery。
        【解决方案5】:

        你可以做的是使用 jQuery。

        DEMO HERE 适当的情况是:

        *从 div 中获取值并将其保存到隐藏字段中。 *

        这必须在您提交表单时完成:

        $(function () {
            $("#send").on("click", function () {
                $("#hiddenfield").val($("#dash_new_shout_textarea").text());
        
                alert($("#hiddenfield").val());
        
                $("form#formID").submit();
            });
        });
        

        【讨论】:

          猜你喜欢
          • 2014-04-30
          • 1970-01-01
          • 2013-11-03
          • 2022-08-18
          • 1970-01-01
          • 1970-01-01
          • 2013-02-24
          • 2011-07-22
          相关资源
          最近更新 更多