Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit html table data with via form (post)

Tags:

html

jquery

php

I want to post this html table on submit button click.

01 - I want to retrieve the data of this table in php code (server side) 02 - I also want to display this table on another page.

I'm using

PHP, JQuery

I have html table with many rows in form tag.

<form id="frm_test" class="form-vertical" method="post">
  <table id="mytable">
    <tr>
      <td>
        <input type="text"  name="color_1" value="" />
      </td>
      <td>
        <input type="text"  name="color_2" value="" />
      </td>
      <td>
        <input type="text"  name="color_3" value="" />
      </td>
      <td>
        <input type="text"  name="color_4" value="" />
      </td>
    </tr>
    ......
    ......
    ......
    ......
    .....
  </table>

  <input type="submit"  name="submit" value="submit" />
</form>

===========

there are 4 input fields in each row (4 cells in each row). and hundred rows in table. So if I get value via name in php code then I have to write lot of code to get 100(rows) * 4 (input fields) = 400 inputs. So my question was "What is the best way to achieve this"

like image 429
Ali Avatar asked Dec 05 '14 10:12

Ali


People also ask

How do you submit a table in HTML?

Since you are trying to submit multiple rows of inputs/selects with the same 'name' attribute to a backend, you would need to add square brackets [] to the end of the name value in those inputs (in the table rows). This tells your browser to create an array for that name property.

How can we send data through POST method in HTML?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.

How do I send posts with form data?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.

How Add submit button after table in HTML?

A submit button is just an <input> element with type="submit". The value attribute specifies the text that shows on the submit button. So, if you wanted the button to say "OK", you would use value="OK" in the start tag for <input>.

How do I post HTML form data to the server?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.

How do I submit a form using GET and post?

Use the GET method for submitting non-sensitive data or retrieving data from a server (for example, during searches). Use the POST request when submitting files or sensitive data. Let’s take what we’ve learned about forms and use it to make a simple contact form.

How to post a form to a table in PHP?

<form id="frm_test" class="form-vertical" name="THE_PHP_FILE_TO_POST.php" method="post"> When you want to POST values you should specify input fields with a certain name. If you only want the table is visible you should use the type hidden so the form will POST data, but the inputs aren't visible.

How is form data sent to the server?

When a user fills in a form and submits it with the submit button, the data in the form controls are sent to the server through GET or POST HTTP request methods. So how is the server indicated?


1 Answers

Since you are trying to submit multiple rows of inputs/selects with the same 'name' attribute to a backend, you would need to add square brackets [] to the end of the name value in those inputs (in the table rows).

<form>
<input type="number" name="examplevar" />
<table>
  <tr>
    <td><input type="text" name="color_1[]" /></td>
    <td><input type="text" name="color_2[]" /></td>
  </tr>
  <tr>
    <td><input type="text" name="color_1[]" /></td>
    <td><input type="text" name="color_2[]" /></td>
  </tr>
<table>
<input type="submit" />
</form>

This tells your browser to create an array for that name property.

In php, read it as $_POST['color_1'][0] and $_POST['color_2'][0], and loop as you'd like.

The brackets are in Phil Bailey's answer, but they are not pointed out. (Added because a recent search led me to this)

like image 74
Kremnari Avatar answered Oct 01 '22 05:10

Kremnari