猿问

PHP 提交 POST

我创建了 2 个表单,我正在尝试获取所有数据以及其中的所有数据并将它们发送到一个简单的表格中,但是我希望每个表单只有一个标题,无论输入或打开多少数据它有多少列它将继续添加行而不是像这样的标题:它只有 2 列,但将来可以有 3:

因此,我创建了此代码以使其尽可能通用:


<!DOCTYPE HTML>

<html>

    <head>

        <script src="js/Jquery.js"></script>

        <link rel="stylesheet" type="text/css" href="css/style.css">

        <title>Test</title>

        <style>

        html

        {

            -webkit-background-size:cover;

            -moz-background-size:cover;

            -o-background-size:cover;

            background-size:cover;

        }

        </style>

    </head>

<body id="body" class="dark-mode">

    <select name="Template_Picker" id="Template_Picker">

        <option value="" Disabled selected hidden>Select Template</option>

        <option value="payment">payment</option>

        <option value="identity">identity</option>

    </select>

    <br>

    <div id="Templates_Pages">

        <button class="Template" value="Send" id="Template_Button">Show selected</button>

    </div>

    <div class="vl"></div>

    <form action="test/submit.php" method="post" id="submit_template">

        <center id="output">

        </center>                                                                                                                                    

    </form>

</body>

<script src="test/template.js"></script>

</html>


扬帆大鱼
浏览 110回答 1
1回答

繁花如伊

由于今天的工作已被取消,我有 1/2 小时的空闲时间并使用 vanilla javascript 编写了一个小演示,我认为,语法上有效的 HTML 标记提供了比原始标记更大的灵活性,因为您可以简单地添加新template标签(带有内容)并且 javascript 应该相应地容纳它们并生成所需的输出。整个评论应该有助于澄清正在发生的事情......document.addEventListener('DOMContentLoaded',()=>{&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // references to HTML elements&nbsp; &nbsp; const oSelect=document.querySelector('select[name="picker"]');&nbsp; &nbsp; const oBttn=document.querySelector('input[type="button"][name="template_bttn"]');&nbsp; &nbsp; const oForm=document.querySelector('form[name="template"]');&nbsp; &nbsp; const oTable=document.querySelector('table[id="output"]');&nbsp; &nbsp; const colTemplates=Array.from( document.querySelectorAll('template.source') );&nbsp; &nbsp; const oSubBttn=document.querySelector('input[type="button"][name="subdata"]');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // event handler for when the `show selected` button is clicked&nbsp; &nbsp; const clickhandler=function(e){&nbsp; &nbsp; &nbsp; &nbsp; // only proceed if the SELECT has been changed from the default&nbsp; &nbsp; &nbsp; &nbsp; if( oSelect.value != oSelect.childNodes[1].value ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // find the template from HTML&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let template=document.querySelector( 'template[ id="'+oSelect.value+'" ]' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add the template HTML to the empty form&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oForm.innerHTML=template.innerHTML;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //assign event handler to button within form&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let bttn=oForm.querySelector('input[type="button"]');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bttn.addEventListener('click', addcontenthandler );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // clear the form when select menu changes&nbsp; &nbsp; const changehandler=function(e){&nbsp; &nbsp; &nbsp; &nbsp; oForm.innerHTML='';&nbsp; &nbsp; };&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // event handler that does the final processing of data when all rows have been added...&nbsp;&nbsp; &nbsp; const submithandler=function(e){&nbsp; &nbsp; &nbsp; &nbsp; console.info( oTable.outerHTML );&nbsp; &nbsp; };&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; const addcontenthandler=function(e){&nbsp; &nbsp; &nbsp; &nbsp; // form elements that are NOT hidden or buttons...&nbsp; &nbsp; &nbsp; &nbsp; let col=Array.from( oForm.querySelectorAll('input:not( [type="button"] ):not( [type="hidden"] )') );&nbsp; &nbsp; &nbsp; &nbsp; // the type determines which tbody to write content to&nbsp; &nbsp; &nbsp; &nbsp; let type=oForm.querySelector('input[ type="hidden" ]').value;&nbsp; &nbsp; &nbsp; &nbsp; // the tbody that will be populated according to selection made in the SELECT menu&nbsp; &nbsp; &nbsp; &nbsp; let tbody=oTable.querySelector('[ data-name="'+type+'" ]')&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // If this particular tbody does NOT have column headers, add them based upon form element names in this form&nbsp; &nbsp; &nbsp; &nbsp; if( tbody && !tbody.querySelector('tr[ scope="col" ]') ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let tr=document.createElement('tr');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tr.setAttribute('scope','col');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tbody.appendChild( tr );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; col.forEach( input=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let td=document.createElement('td');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; td.textContent=input.name.replace(/\_/gi,' ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tr.appendChild( td );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // for each input element add it's content to the table...&nbsp; &nbsp; &nbsp; &nbsp; tr=document.createElement('tr');&nbsp; &nbsp; &nbsp; &nbsp; tbody.appendChild( tr );&nbsp; &nbsp; &nbsp; &nbsp; col.forEach( input=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; td=document.createElement('td');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; td.textContent=input.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.value='';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tr.appendChild( td );&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; };&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // add new tbody for each Template found in HTML source & add entry to the select menu&nbsp; &nbsp; colTemplates.forEach( template=>{&nbsp; &nbsp; &nbsp; &nbsp; let tbody=document.createElement('tbody');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tbody.dataset.name=template.id;&nbsp; &nbsp; &nbsp; &nbsp; oTable.appendChild( tbody );&nbsp; &nbsp; &nbsp; &nbsp; oSelect.appendChild( new Option( template.id, template.id ) );&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // assign event listeners to various DOM elements&nbsp; &nbsp; oBttn.addEventListener('click', clickhandler );&nbsp; &nbsp; oSelect.addEventListener('change', changehandler );&nbsp; &nbsp; oSubBttn.addEventListener('click', submithandler );});body, body *{font-family:monospace;box-sizing:border-box}table{border:1px solid gray;border-collapse:none;width:50%;margin:5rem 0 0 0;padding:1rem;}td{border:1px dotted gray;padding:1rem;margin:0.1rem;}tr[scope='col']{background:gray;color:white}[name='subdata']{padding:1rem;width:50%;}.pl-276{}.collapse.toggle{}<form method='post'>&nbsp; &nbsp; <select name='picker'>&nbsp; &nbsp; &nbsp; &nbsp; <option selected hidden disabled>Please Select Template&nbsp; &nbsp; &nbsp; &nbsp; <!-- other options are populated dynamically -->&nbsp; &nbsp; </select>&nbsp; &nbsp; <input type='button' name='template_bttn' value='Show Selected' /></form><form method='post' name='template'><!-- content will be added here dynamically depending upon chosen template --></form><table id='output'></table><input type='button' name='subdata' value='Submit generated data' /><!-- you can add as many `TEMPLATES` as you like with as many `INPUT ELEMENTS` as you like --><template id='identity' class='source'>&nbsp; &nbsp; <input type='hidden' name='DB_Table' value='identity' />&nbsp; &nbsp; <div class='collapse.toggle pl-276'>&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' placeholder='Full Name' name='Full_Name' />&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' placeholder='Last Name' name='Last_Name' />&nbsp; &nbsp; </div>&nbsp; &nbsp; <input type='button' value='Add Content' /></template><template id='payment' class='source'>&nbsp; &nbsp; <input type='hidden' name='DB_Table' value='payment' />&nbsp; &nbsp; <div class='collapse.toggle pl-276'>&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' placeholder='Full Name' name='Full_Name' />&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' placeholder='Credit Card' name='Credit_Card' />&nbsp; &nbsp; </div>&nbsp; &nbsp; <input type='button' value='Add Content' /></template><template id='banana' class='source'>&nbsp; &nbsp; <input type='hidden' name='DB_Table' value='banana' />&nbsp; &nbsp; <div class='collapse.toggle pl-276'>&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' placeholder='Banana Colour' name='Banana_Colour' />&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' placeholder='Banana Shape' name='Banana_Shape' />&nbsp; &nbsp; &nbsp; &nbsp; <input type='text' placeholder='Banana Weight' name='Banana_Weight' />&nbsp; &nbsp; </div>&nbsp; &nbsp; <input type='button' value='Add Content' /></template>
随时随地看视频慕课网APP
我要回答