简体   繁体   中英

Post copy of dynamically generated div content to new page

So I was researching this a bunch and thought the things I was seeing seemed a lot more complicated than what I was imagining.

I just want to take all of the HTML content within specific div in a form, including tags and ids and whatever, and copy it line for line into a new div on another page upon post submission. Some of the content is generated dynamically using javascript such as table rows and their data but that is completed before submission.

For example: take the table in the div below and post it into a div on the page the form submits to. (I am using multipart/form-data because I have a file input in this same form)

<form action = "example.php" method = "post" enctype="multipart/form-data">
   <div class="table_container">
    <table class="example_table">
      <tr> <!--This HTML was all generated with Javascript-->
         <th>Test</th> 
         <th>Test</th>
         <th>Test</th>
      </tr>
      <tr>
         <td>Test</td>
         <td>Test</td>
         <td>Test</td>
      </tr>
      <tr>
         <td>Test</td>
         <td>Test</td>
         <td>Test</td>
      </tr>
    </table>
   </div>
</form>

I haven't tried anything yet because I haven't found anything that might do this. A lot people mentioned the PHP include feature but I don't need an entire page and with this being dynamically generated I don't see how that would work.

Any suggestions on how to do this would be greatly appreciated, whether it is PHP, AJAX, or JQUERY or anything.

Thanks in advance
Kevin

JSON was the way to go. I had the function that generates the table also send the data to an array

This code selects the array (references) and stringifys the data then stores it as the value of a hidden input(ref_data):

function input_ref_JSON() {
  var refsJSON = JSON.stringify(references);
  $('#ref_data').val(refsJSON); //write JSON data to hidden input value
}

then on the php page I decode it and use a recursive loop to generate a new table

<?php 
            $ref_JSON = $_POST['ref_data'];
            $ref_decoded = json_decode($ref_JSON);
        echo '<table>';
            foreach ($ref_decoded as $ref) {
                echo '<tr><td>'.$ref->type.'</td><td><a href="'.$ref->link.'">'.$ref->title.'</a></td><td>'.$ref->author.'</td><td>'.$ref->notes.'</td></tr>';
            }
        echo '</table>';
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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