开发者

Retrieve data from HTML table to PHP

开发者 https://www.devze.com 2023-04-01 10:01 出处:网络
I\'ve got a dynamic html table and I want to retrieve the data from that table using the POST method. Is it possible to do this? What other ways would you recommend to achieve this.

I've got a dynamic html table and I want to retrieve the data from that table using the POST method. Is it possible to do this? What other ways would you recommend to achieve this. Here's a simple example from what I got:

<html>
      <form 开发者_如何学Cid="form1" method="post" action="process.php">
         <table id="tblSample" name="tblSample">
         <tbody>
            <tr>
                <td>
                John Appleseed
                </td>    
                <td>
                Australia
                </td>
           </tr>
           <tr>
              <td>
              Mary Poppins
              </td> 
              <td>
              USA
              </td>   
            <tr>  
          </tbody>
          </table> 
       </form>
</html>

Maybe it's possible to store the info in a javascript array or variable in order to post it to the action file, I'm not sure.

I don't know if this has anything to do, but the table is generated dynamically. The rows and cells are added by the user using javascript.

I'd appreciate your answers very much.


A table cannot be accessed as such, unless you use form elements (input, textarea, etc.) and add name attribute to them; then, you can access your elements with $_POST.

If you have your data in a table, the best way to retrieve our data is by JavaScript, using innerHTML.


You can assign the innerHTML of the form to a variable and use AJAX to post it back to the server as a string, or store it in a textarea and do the same. Regardless, you need to parse the string to extract the data.


A table is not a form control, and will not be submitted as form data even when included within a form tag (as in your example).

If you wish to submit the tabular data, you will need to use Javascript to extract it. You can then insert the data into hidden form element, or submit the data via an AJAX call.


You can try to use:

HTML:

<form id="form1" method="post" action="process.php" onSubmit="submitAction();">
    <input type="hidden" name="table_content" id="table_content"/>
    <input type="submit" name="submit" value="Send"/>

JavaScript:

function submitAction()
{
    var form = document.getElementById("form1");
    form.table_content.value = document.getElementById("tblSample").innerHTML;
    return form.submit();    
}


This is a many way for resolve this problem. For example: 1) Use ajax: Put your data in some html container - like span with class name 'formData_name' for example or with id= 'formData_'+name = where name is name of this data. Then you in JS search all this span and send by ajax. This is very simply if you use jquery. In pure js this is not hard too but code will be biger.

2) Put your data in some html container - like span with class name 'formData_name' for example or with id= 'formData_'+name = where name is name of this data. Bind submit button that when user click submit you create an added to dom input hidden fields with name adn return true. 3)You can send innerHTML by post through binding submit event and added hidden field for store innerHTML and parse it on server 4)you can replace all td to input on submit action

But all this solutions not good and if you must do some of this then your project architectural is bad

0

精彩评论

暂无评论...
验证码 换一张
取 消