简体   繁体   English

单击提交按钮时,将在javascript中创建的动态生成的html表保存并传递给php

[英]preserve and pass dynamically generated html table created in javascript to php when submit button is clicked

I have to submit a dynamically generated html table that is created using javascript and pass this table to a php mail function when submit button is clicked ,I would like to know whether its possible to preserve the dynamically generated table in javascript when the form has any errors also when submit button is clicked and the dynamically generated table is lost . 我必须提交使用javascript创建的动态生成的html表,并在单击“提交”按钮时将此表传递给php邮件功能,我想知道当表单具有任何形式时是否有可能在javascript中保存动态生成的表单击“提交”按钮并且动态生成的表丢失时,也会发生错误。 I am a beginner and i have fared till making a dynamically generated javascript table with delete , now i need to pass this table to a php submit button and then can only proceed further 我是一个初学者,在制作带有delete的动态生成的javascript表之前,我一直很高兴,现在我需要将此表传递给php提交按钮,然后只能继续进行

Any help will be grateful since time is short for me right now pls help. 任何帮助将不胜感激,因为时间对我来说很短,请提供帮助。

the code is as follows 代码如下

To add rows to a table 向表添加行

function addRow(){

var a =document.getElementById("sociallink").value;
    var b =document.getElementById("socialemail").value;
    var c =document.getElementById("socialpass").value;


var media=document.getElementById('media');

if ( media.selectedIndex  ==  "1" )
{
    media_type = "facebook";
}

var table = document.getElementById("dataTable");


var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
row.insertCell(0).innerHTML= '<input type="button" value = "Delete" onClick="Javacsript:deleteRow(this)">';
row.insertCell(1).innerHTML= media_type;
row.insertCell(2).innerHTML= a;
row.insertCell(3).innerHTML=b;
row.insertCell(3).innerHTML=c;

table.style.display ="block";

}

php echoing html is as follows PHP的回显HTML如下

    $form ="<form action='./form3.php'  method='post'  name='seoform'> 
        <tbody id='dataTable' border='1' cellpadding='2'>  
                      <tr>
                  <td> Remove Media </td> 
                      <td> Media </td>
                                <td>Media Link</td>
                                <td> Media Email</td>
                                <td> Media Password </td>

                      </tr>
<tr> 
<td> </td> 
<td  > <input type='submit' name='seoformbtn' value='Register'  />   </td> 
</tr> 


        </tbody> </form> ";

    echo $form;

You can store the markup for the table created in an hidden input which is then sent along when the form is submitted. 您可以将创建的表的标记存储在隐藏的输入中,然后在提交表单时将其发送。

Add a hidden textarea to your form 在表单中添加隐藏的文本区域

<textarea id="table-markup"></textarea>

CSS: CSS:

#table-markup{
   display: none;
}

give your table an id like so: 给你的表一个ID,像这样:

<table id='table'><tbody id='dataTable'>.....</tbody></table>

var tableMarkup = document.getElementById("table");
document.getElementById("table-markup").value = tableMarkup.innerHTML;

Now when the form is submitted this will be sent along with it.. I recommend using the POST method to send your form data (depending upon your table markup) 现在,提交表单后,它将与它一起发送。.我建议使用POST方法发送表单数据(取决于表标记)

To store the table is simple lets say your table is 存储表很简单,假设您的表是

<table id='tableid' >......</table>

var tbl = $('#tableid').html();
// to complement previous answer, then you put in hidden field
$("#table-markup").val(tbl);

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

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