doulie0178 2018-01-30 16:04
浏览 77

动态地将字段添加到HTML表单,发送数据并记住添加的字段和值

I have the following problem:

I want to add textfields to my html form, send them to my script and than redirect back AND have the number of fields and their values shown.

Adding the fields via jQuery is working fine, but I can't store the values later on, because it's done via adding DIVs and they're having the same name.

Can someone point me to the right direction?

Here's the code in my index.php and in my action.php

index.php

    <?php
    session_start([
    'cookie_lifetime' => 3600,
    ]);
    ?>
    <html>
    <head>
     <!-- HTML5 -->
    <meta charset='utf-8'>
     <!-- HTML 4.x -->
     <meta http-equiv='content-type' content='text/html; charset=utf-8'>
     <link rel='stylesheet' href='https://unpkg.com/purecss@1.0.0/build/pure-min.css' integrity='sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w' crossorigin='anonymous'>
     <link rel='stylesheet' href='style/style.css'>
     <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>
    myDrop2
    </title>
    <script src="scripts/jquery.js"></script>
    </head>
    <body>
    <script>
    function showLoadingMessage() {
      document.getElementById('content').style.display = 'none';
      var message = document.createElement('div');
      message.innerHTML = '<center><h1>Wird gearbeitet, bitte warten!</h1></center>';
      document.body.appendChild(message);
    }
    function checkRemove() {
        if ($('div.d1_drops').length == 1) {
            $('#remove').hide();
        } else {
            $('#remove').show();
        }
    };
    $(document).ready(function() {
        checkRemove()
        $('#add').click(function() {
            $('div.d1_drops:last').after($('div.d1_drops:first').clone());
            $('div.d2_drops:last').after($('div.d2_drops:first').clone());
            checkRemove();
        });
        $('#remove').click(function() {
            $('div.d1_drops:last').remove();
            $('div.d2_drops:last').remove();
            checkRemove();
        });
    });
    </script>
    <center>
    <div id='header'>
       <b>myDrop2</b>
    </div>
    <br><br>
    <div id='content'>
    <br>
    <button id='add'>[ + ]</button>
    <button id='remove'>[ - ]</button>
    <form action=scripts/action.php class='pure-form pure-form-aligned' onsubmit='showLoadingMessage();' method='post'>
    <!-- <form action=scripts/action.php class='pure-form pure-form-aligned' onsubmit='showLoadingMessage();' method='post'> -->
    <div id='settings1'>
    <br>
      Belichtung starten
      <br><br>
      <label>Warten (ms) <input name='firstwait' type='number'></label>
    </div>
    <div id='ventile'>
    <div id='ventil1'>
       <input type='radio' id='v1' name='ventile' value='1' checked='checked'>
       <label for='v1'><h2>1 Ventil</h2></label>
       <br><br>
       <div class='d1_drops'>
      <label>Tropfen 1 Dauer (ms) <input name='d1_drop[]' type='number'></label>
      <br><br>
      <label>Warten (ms) <input name='d1_wait[]' type='number'></label>
       <br><br>
      </div>
    </div>
    <div id='ventil2'>
       <input type='radio' id='v2' name='ventile' value='2'>
       <label for='v2'><h2>2 Ventile</h2></label>
       <br><br>
       <div class='d2_drops'>
      <label>Tropfen 1 Dauer (ms) <input name='d2_drop[]' type='number'></label>
      <br><br>
      <label>Warten (ms) <input name='d2_wait[]' type='number'></label>
      <br><br>
      </div>
    </div>
    </div>
    <div id='settings2'>
      Blitzen
      <br><br>
      <label>Warten (ms) <input name='lastwait' type='number'></label>
      <br><br>
      Belichtung stoppen
      <br><br>
    </div>
    <div id='run'>
      <button type='submit'>ausführen</button>
    <br>
    <br>
    </div>
    </form>
    </div>
    </center>
    </body>
    </html>

action.php

    <?php
    session_start();
    include ('../conf.php');
    $d1_array_drop = $_POST['d1_drop'];
    $d1_array_wait = $_POST['d1_wait'];
    $d2_array_drop = $_POST['d2_drop'];
    $d2_array_wait = $_POST['d2_wait'];
        (...) do stuff with the data (..)
    header('Location: ' . $_SERVER['HTTP_REFERER']);
    ?>
  • 写回答

1条回答 默认 最新

  • duangu6588 2018-01-30 16:09
    关注

    You can use sessionstorage or localstorage to remember values,if you want to output values from php ,first store them in session|files|db after redirecting , you can just output the values to js

    <?php
    $data=$_SESSION["data"];
    ?><script>
    var data=<?php echo json_encode($data);?>;
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加