简体   繁体   English

如何将数据从一种形式发布到另一种iFrame形式

[英]How to post data from one form to another iFrame form

All I am looking to do here is take the form inputs of one form and submit it to another form linked to an iFrame. 我在这里要做的只是获取一种表单的表单输入,并将其提交到链接到iFrame的另一种表单。 Purpose of this is that the visitor can preview what car he has selected in the iframe, if happy press save one form one and done. 这样做的目的是,访问者可以预览自己在iframe中选择的汽车,如果高兴的话可以保存一个表格并完成。

I feel like I'm almost close, but I can't figure how to send the data from form one to the iFrame post and can't find any answers on how to do this. 我觉得自己快要接近了,但是我无法确定如何将数据从表单一发送到iFrame帖子,也找不到如何执行此操作的答案。 I got a single form version working as below, but can't pull in the data from form one to submit to iFrame 我只有一个表单版本,其工作方式如下,但无法从表单中提取数据以提交给iFrame

<form action="/save.php" method="post" id="save">

  <input type="text" name="firstname">
  <input type="text" name="carname">
  <input type="text" name="cartype">

  <button type="submit" name="save">Save</button>      
</form>



<form action="/preview.php"  target="iframe" method="post" id="iframebox">

  <button id="preview" type="submit" name="reload" value="post">Reload</button>   

</form>

<iframe name="iframe" src="/preview.php" ></iframe>


            $('#preview').click(function(e){
                e.preventDefault();
                var d = $("#save").serialize();
                $('#iframebox').append(d);
                $('#iframebox').submit();
            });

This is kind of dirty, but if you need to do it in an iFrame, it works. 这有点脏,但是如果您需要在iFrame中进行操作,则可以使用。 Your preview box currently was just a form element and you were appending query string text inside of it essentially. 当前,您的预览框只是一个表单元素,实际上是在其中添加查询字符串文本。 If you want to actually send it to the iframe itself, you need to parse the inputs and have it submit to the preview page that is in the targeted iFrame. 如果您想将其实际发送到iframe本身,则需要解析输入并将其提交到目标iFrame中的预览页面。 You can either use static named inputs if you know that they will always exist, or if there are going to be additional inputs that you cannot predict, you can have the JS generate hidden input elements on the fly. 您可以使用静态命名输入(如果您知道它们将始终存在),或者可以使用无法预测的其他输入,可以让JS动态生成隐藏的输入元素。

In form.php 在form.php中

<form action="save.php" method="post" id="save">

    <input type="text" name="firstname" placeholder="firstname">
    <input type="text" name="carname" placeholder="carname">
    <input type="text" name="cartype" placeholder="cartype">

    <button type="submit" name="save">
        Save
    </button>
    <button id="preview">
        Preview
    </button>
</form>

<form action="preview.php"  target="iframe" method="post" id="iframebox">
    <input type="hidden" name="firstname">
    <input type="hidden" name="carname">
    <input type="hidden" name="cartype">
</form>

<iframe name="iframe" src="preview.php" ></iframe>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
    $('#preview').click(function(e) {
        e.preventDefault();
        $("#save input").each(function(index, value){
            $('#iframebox input[name="' + value.name + '"]').val(value.value);
        });
        $('#iframebox').submit();
        return false;
    });
</script>

In preview.php 在preview.php中

    <ul>
        <li>
            First Name : <?php echo $_POST['firstname'] ? : "N/A"; ?>
        </li>
        <li>
            Car Name : <?php echo $_POST['carname'] ? : "N/A"; ?>
        </li>
        <li>
            Car Type : <?php echo $_POST['cartype'] ? : "N/A"; ?>
        </li>
    </ul>

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

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