1

I want to send variable rows to post2.php with other HTML form variable using POST or GET methods.

The below code gives an error:

Notice: Undefined index: row1 in C:\xampp\htdocs\PhpProject1\OtherUsableItems\post2.php on line 8 

post1.php

 <html> <head> <SCRIPT language="javascript"> function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var colCount = table.rows[0].cells.length; for(var i=0; i<colCount; i++) { var newcell = row.insertCell(i); newcell.innerHTML = table.rows[0].cells[i].innerHTML; //alert(newcell.childNodes); switch(newcell.childNodes[0].type) { case "text": newcell.childNodes[0].value = ""; break; case "checkbox": newcell.childNodes[0].checked = false; break; case "select-one": newcell.childNodes[0].selectedIndex = 0; break; } } } </script> <script> function count(tableId){ var rows = document.getElementById(tableId).getElementsByTagName("TR").length; // window.location.href = "http://localhost/PhpProject1/OtherUsableItem /post2.php?rows=" + rows ; // alert('Your table has ' + rows + ' rows.'); $.post("post2.php", { 'row' : rows}, function(rows){alert('rows'+rows);}); } </script> </head> <body> <form action="post2.php" method="post"> <TABLE id="dataTable" border="1"> <TR> <TD> 1 </TD> <TD> <INPUT name="n1[]"type="text" /> </TD> <TD> <INPUT name="n2[]"type="text" /> </TD> <TD><SELECT name="country[]" type="select-one"> <OPTION value="in">India</OPTION> <OPTION value="de">Germany</OPTION> <OPTION value="fr">France</OPTION> <OPTION value="us">United States</OPTION> <OPTION value="ch">Switzerland</OPTION> </SELECT></TD> </TR> </TABLE> <INPUT type="button" value="Add Row" onclick="addRow('dataTable');"/> <button id="bt" onclick="count('dataTable');">Submit</button> </form> </body> </html> 

post2.php

<?php $n1 = $_POST['n1']; $n2 = $_POST['n2']; $country = $_POST['country']; echo $n1[0]; echo $n2[0]; echo $country[0]; $count = $_POST['row1']; echo $count; ?> 
1
  • Please write what does that echos writes, also you can try writing 'var_dump($_POST)' in post2.php to see full content of $_POST (usualy var_dump isnt formated so view source code or add <pre> tag) Commented Apr 22, 2013 at 19:20

2 Answers 2

1

Try changing to 'row' instead of 'row1'

$n1 = $_POST['n1']; $n2 = $_POST['n2']; $country = $_POST['country']; echo $n1[0]; echo $n2[0]; echo $country[0]; $count = $_POST['row']; echo $count; 

In the future, use print_r to see the value of $_POST.

In addition to the above instructions, I would remove the 2nd <script> tag from post1.php and place the following code into the body at the start of the form:

<form action="post2.php" method="post" > <input id="rowNumber" type="hidden" name="row" value="1"/> 

Also, add the following lines to function addRow:

 var rowNumber = document.getElementById('rowNumber'); rowNumber.value = parseInt( rowNumber.value ) + 1; 
Sign up to request clarification or add additional context in comments.

3 Comments

it's posting mistake by me . After changing row1 to row . i got Undefined index: row in C:\xampp\htdocs\PhpProject1\OtherUsableItems\post2.php on line 8
in Notice: Undefined index: row in C:\xampp\htdocs\PhpProject1\OtherUsableItems\post2.php on line 8 Array ( [n1] => Array ( [0] => [1] => ) [n2] => Array ( [0] => [1] => ) [country] => Array ( [0] => in [1] => in ) ) 1
If you are going to use jQuery in your implementation, be sure to actually include the jQuery script: <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
0

The problem is that you are not sending the correct Post value. check if this line :

var rows = document.getElementById(tableId).getElementsByTagName("TR").length;

it returns values something like: {name:'value',name2:'value2'}

after that you will be able to access via php using $_POST['name']...

and this line :

 $.post("post2.php", { 'row' : rows}, function(rows){alert('rows'+rows);}); 

replaced with:

 $.post("post2.php", rows, function(rows){alert('rows'+rows);}); 

else you will be accessing with $_POST['row']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.