0

I have this code and I need to get the count of inputs that I put in inserting the rows and input text form. Please can anyone help me?

function myFunction() {
  var table = document.getElementById("mytable");
  var rowCount = table.rows.length;
  var row = table.insertRow(rowCount - 1);

  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);

  cell1.innerHTML = table.rows[0].cells[0].innerHTML;
  cell2.innerHTML = table.rows[0].cells[1].innerHTML;
}
<table id="mytable">
  <form method="POST">
    <tr>
      <td>
        <input name="s1[]" type="text" />
      </td>
      <td>
        <input name="s2[]" type="text" />
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="submit" name="submit" value="insert" />
      </td>
    </tr>
  </form>
</table>

<button onclick="myFunction()">more</button>

<?php if(isset($_POST[ 'submit']))
  {
    $count = count($_POST[ 's1']); 
    echo "<script>alert('".$count. "');</script>"; 
  } 
?>

3
  • what is your problem? explain? Commented Nov 9, 2015 at 16:59
  • When i click the button "more" the function Javascript insert a row to the table with input type= text but when i submit and click submit the row count of input text return all times 1 Commented Nov 10, 2015 at 20:54
  • Hi @nuddzz : I posted my answer. Please check it out. Thanks. :) Commented Nov 10, 2015 at 21:38

2 Answers 2

1

A form is not allowed to be a child element of a table, tbody or tr.

You can have an entire table inside a form. You can have a form inside a table cell. You cannot have part of a table inside a form.

Use one form around the entire table. Then either use the clicked submit button to determine which row to process (to be quick) or process every row (allowing bulk updates).

Everything fine. Put your <form> & </form> outside table. <table></table> should be inside <form></form>

<form method="POST">
    <table id="mytable">
        <tr>
          <td>
            <input name="s1[]" type="text" />
          </td>
          <td>
            <input name="s2[]" type="text" />
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <input type="submit" name="submit" value="insert" />
          </td>
        </tr>
    </table>
</form>

For more info, check this

Sign up to request clarification or add additional context in comments.

4 Comments

thank you dear Partykar it worked very well after i put <from> outside the table i really appreciate your help.
Hi @nuddzz: Glad to help :). If you think, my answer helped you, then please mark it as correct answer as other user's will find this answer easily. Thanks.
What happened @nuddzz , why u unanswered my answer.
oh... how are you now??
0

Perhaps you should put the table inside the form:

<form method="POST">
    <table id="mytable">
        <tr>
            <td>
                <input name="s1[]" type="text" />
            </td>
            <td>
                <input name="s2[]" type="text" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="submit" value="insert" />
            </td>
        </tr>
    </table>
</form>

3 Comments

I tried this and noth change, look when I add <tr> <td> <input type ="text" name="s1[ ]"..... Manually i works perfectly so iii think the prob is from Javascript because when i insert the row it doesn't appear with form
So when you put the table inside the form, you click on "more" a few times and then you click "insert", the alert still returns 1?
Does the page reload then you press "insert"? I've added this to the button to prevent that. <button onclick="myFunction(); return false;">more</button> Does that work for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.