Reputation: 81
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;
?>
Upvotes: 1
Views: 943
Reputation: 357
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;
Upvotes: 1
Reputation:
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']
Upvotes: 0