I'm trying to auto submit a form,(below it's only the form, its wrapped with a while{} for each DB entry to display).
It's working for all days of the week, but I can't submit the value for $totalhours, $holidayhours, $wagegross without a js event like onfocus().
Basically I want to submit this 3 inputs without an click or any other manual action.
any idea ?
<form id="weeklysheet" name="weeklysheet" method="post" action="hourly-function.php">
<tbody>
<tr style="line-height:0px;">
<td class="large">
<p class="employee_name"><?php echo $fullname?></p>
</td>
<td class="small">
<input name="mon" id="mon" type="text" class="days_input" value="<?php echo $data['mon'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="tue" id="tue" type="text" class="days_input" value="<?php echo $data['tue'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="wed" id="wed" type="text" class="days_input" value="<?php echo $data['wed'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="thu" id="thu" type="text" class="days_input" value="<?php echo $data['thu'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="fri" id="fri" type="text" class="days_input" value="<?php echo $data['fri'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="sat" id="sat" type="text" class="days_input" value="<?php echo $data['sat'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="sun" id="sun" type="text" class="days_input" value="<?php echo $data['sun'];?>"onchange="this.form.submit();"/>
</td >
<td class="small">
<input name="totalhours" id="totalhours" type="text" class="cumul_week_input" value="<?php echo $totalhours ?>"onfocus="this.form.submit();"/>
</td>
<td class="small">
<input name="holidayhours" type="text" class="cumul_week_input" value="<?php echo $holidayhours ?>"onfocus="this.form.submit();"/>
</td>
<td class="small">
<input name="wagegross" type="text" class="cumul_week_input" value="<?php echo $wagegross ?>"onfocus="this.form.submit();"/>
</td>
<td>
<input type="submit"name="save" id="save" value="Confirm" style="display:none;" />
</tr>
</tbody>
</form>
You could use jQuery to trigger the submit button's click action after the document has loaded like so:
1.) You can link the jQuery library from Google's Hosted Libraries:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
or, download it from jQuery.com to, say a "js" folder, and include it
<script src="js/jQuery.js"></script>
2.) Place this script tag at below the </form> tag:
<script type="text/javascript">
$(document).ready(function(){ //When the document is done loading
$("input#save").trigger('click'); //"Click" on the input button with an ID of "save"
});
</script>
Or you could do it the pure javascript way without jQuery like so:
<html>
<body onLoad="submitform()">
<form id="weeklysheet" name="weeklysheet" method="post" action="hourly-function.php">
Then place this script tag below the </form> tag:
<script type="text/javascript">
function submitform()
{
document.weeklysheet.submit();
}
</script>
Or, to shorten the method above:
<html>
<body onLoad="Javascript: document.weeklysheet.submit();">
<form id="weeklysheet" name="weeklysheet" method="post" action="hourly-function.php">
Are you looking to submit them individually or all at once? If you want to do it all at once you could give the last element an id and in jquery use $('#div').moueleave
that's the js I'm using, I've try submitting with a timeout and it's working.
at the end my issue is not there, it appear that when I'm submit the form, the 3 php variables push the value of the previous input entry.
I don't know if I'm clear with my explanation...
<script type="text/javascript">
function submitform()
{
//document.getElementById("weeklysheet");
var form = document.getElementById("weeklysheet")
form.submit();
}
function submit()
{
if (document.getElementById("weeklysheet")) {
setTimeout("submitform()", 5000); // set timout
}
return false;
}
</script>
(sorry if it's a long explanation)
I'm coming back with my solution... in case that can help anyone.
so the idea it's to have a table that display a list of employees where you can input the days worked, holidays, sick. Then do the sum of each and calculate the gross depending on a wages frequency (i.e: Weekly, 4 Weekly or Monthly).
Because the user won't complete the all form at once, I needed to make the submission automatic for all inputs at all the time of the current week.
this is the JS:
<script type="text/javascript">
function grab<?=$empnum?>(){
// Monday
var mon<?=$empnum?> = document.getElementById('mon<?=$empnum?>').value;
if ((mon<?=$empnum?> == "h" || mon<?=$empnum?> == "H")){
var Hmon<?=$empnum?> = 1;
var Wmon<?=$empnum?> = 0;
var Smon<?=$empnum?> = 0;
}
else if((mon<?=$empnum?> == "w" || mon<?=$empnum?> == "W")){
var Wmon<?=$empnum?> = 1;
var Hmon<?=$empnum?> = 0;
var Smon<?=$empnum?> = 0;
}
else if((mon<?=$empnum?> == "s" || mon<?=$empnum?> == "S")){
var Smon<?=$empnum?> = 1;
var Hmon<?=$empnum?> = 0;
var Wmon<?=$empnum?> = 0;
}
else{
var Smon<?=$empnum?> = 0;
var Hmon<?=$empnum?> = 0;
var Wmon<?=$empnum?> = 0;
var Xmon<?=$empnum?> = 0;
}
// Tuesday
var tue<?=$empnum?> = document.getElementById('tue<?=$empnum?>').value;
if ((tue<?=$empnum?> == "h" || tue<?=$empnum?> == "H")){
var Htue<?=$empnum?> = 1;
var Wtue<?=$empnum?> = 0;
var Stue<?=$empnum?> = 0;
}
else if((tue<?=$empnum?> == "w" || tue<?=$empnum?> == "W")){
var Wtue<?=$empnum?> = 1;
var Htue<?=$empnum?> = 0;
var Stue<?=$empnum?> = 0;
}
else if((tue<?=$empnum?> == "s" || tue<?=$empnum?> == "S")){
var Stue<?=$empnum?> = 1;
var Htue<?=$empnum?> = 0;
var Wtue<?=$empnum?> = 0;
}
else{
var Stue<?=$empnum?> = 0;
var Htue<?=$empnum?> = 0;
var Wtue<?=$empnum?> = 0;
}
// Wednesday
var wed<?=$empnum?> = document.getElementById('wed<?=$empnum?>').value;
if ((wed<?=$empnum?> == "h" || wed<?=$empnum?> == "H")){
var Hwed<?=$empnum?> = 1;
var Wwed<?=$empnum?> = 0;
var Swed<?=$empnum?> = 0;
}
else if((wed<?=$empnum?> == "w" || wed<?=$empnum?> == "W")){
var Wwed<?=$empnum?> = 1;
var Hwed<?=$empnum?> = 0;
var Swed<?=$empnum?> = 0;
}
else if((wed<?=$empnum?> == "s" || wed<?=$empnum?> == "S")){
var Swed<?=$empnum?> = 1;
var Hwed<?=$empnum?> = 0;
var Wwed<?=$empnum?> = 0;
}
else{
var Swed<?=$empnum?> = 0;
var Hwed<?=$empnum?> = 0;
var Wwed<?=$empnum?> = 0;
}
// Thurday
var thu<?=$empnum?> = document.getElementById('thu<?=$empnum?>').value;
if ((thu<?=$empnum?> == "h" || thu<?=$empnum?> == "H")){
var Hthu<?=$empnum?> = 1;
var Wthu<?=$empnum?> = 0;
var Sthu<?=$empnum?> = 0;
}
else if((thu<?=$empnum?> == "w" || thu<?=$empnum?> == "W")){
var Wthu<?=$empnum?> = 1;
var Hthu<?=$empnum?> = 0;
var Sthu<?=$empnum?> = 0;
}
else if((thu<?=$empnum?> == "s" || thu<?=$empnum?> == "S")){
var Sthu<?=$empnum?> = 1;
var Hthu<?=$empnum?> = 0;
var Wthu<?=$empnum?> = 0;
}
else{
var Sthu<?=$empnum?> = 0;
var Hthu<?=$empnum?> = 0;
var Wthu<?=$empnum?> = 0;
}
// Friday
var fri<?=$empnum?> = document.getElementById('fri<?=$empnum?>').value;
if ((fri<?=$empnum?> == "h" || fri<?=$empnum?> == "H")){
var Hfri<?=$empnum?> = 1;
var Wfri<?=$empnum?> = 0;
var Sfri<?=$empnum?> = 0;
}
else if((fri<?=$empnum?> == "w" || fri<?=$empnum?> == "W")){
var Wfri<?=$empnum?> = 1;
var Hfri<?=$empnum?> = 0;
var Sfri<?=$empnum?> = 0;
}
else if((fri<?=$empnum?> == "s" || fri<?=$empnum?> == "S")){
var Sfri<?=$empnum?> = 1;
var Hfri<?=$empnum?> = 0;
var Wfri<?=$empnum?> = 0;
}
else{
var Sfri<?=$empnum?> = 0;
var Hfri<?=$empnum?> = 0;
var Wfri<?=$empnum?> = 0;
}
// Saturday
var sat<?=$empnum?> = document.getElementById('sat<?=$empnum?>').value;
if ((sat<?=$empnum?> == "h" || sat<?=$empnum?> == "H")){
var Hsat<?=$empnum?> = 1;
var Wsat<?=$empnum?> = 0;
var Ssat<?=$empnum?> = 0;
}
else if((sat<?=$empnum?> == "w" || sat<?=$empnum?> == "W")){
var Wsat<?=$empnum?> = 1;
var Hsat<?=$empnum?> = 0;
var Ssat<?=$empnum?> = 0;
}
else if((sat<?=$empnum?> == "s" || sat<?=$empnum?> == "S")){
var Ssat<?=$empnum?> = 1;
var Hsat<?=$empnum?> = 0;
var Wsat<?=$empnum?> = 0;
}
else{
var Ssat<?=$empnum?> = 0;
var Hsat<?=$empnum?> = 0;
var Wsat<?=$empnum?> = 0;
}
// Sunday
var sun<?=$empnum?> = document.getElementById('sun<?=$empnum?>').value;
if ((sun<?=$empnum?> == "h" || sun<?=$empnum?> == "H")){
var Hsun<?=$empnum?> = 1;
var Wsun<?=$empnum?> = 0;
var Ssun<?=$empnum?> = 0;
}
else if((sun<?=$empnum?> == "w" || sun<?=$empnum?> == "W")){
var Wsun<?=$empnum?> = 1;
var Hsun<?=$empnum?> = 0;
var Ssun<?=$empnum?> = 0;
}
else if((sun<?=$empnum?> == "s" || sun<?=$empnum?> == "S")){
var Ssun<?=$empnum?> = 1;
var Hsun<?=$empnum?> = 0;
var Wsun<?=$empnum?> = 0;
}
else{
var Ssun<?=$empnum?> = 0;
var Hsun<?=$empnum?> = 0;
var Wsun<?=$empnum?> = 0;
}
var W<?=$empnum?> = Wmon<?=$empnum?>*1+Wtue<?=$empnum?>*1+Wwed<?=$empnum?>*1+Wthu<?=$empnum?>*1+Wfri<?=$empnum?>*1+Wsat<?=$empnum?>*1+Wsun<?=$empnum?>*1;
var H<?=$empnum?> = Hmon<?=$empnum?>*1+Htue<?=$empnum?>*1+Hwed<?=$empnum?>*1+Hthu<?=$empnum?>*1+Hfri<?=$empnum?>*1+Hsat<?=$empnum?>*1+Hsun<?=$empnum?>*1;
var S<?=$empnum?> = Smon<?=$empnum?>*1+Stue<?=$empnum?>*1+Swed<?=$empnum?>*1+Sthu<?=$empnum?>*1+Sfri<?=$empnum?>*1+Ssat<?=$empnum?>*1+Ssun<?=$empnum?>*1;
var totals<?=$empnum?> = document.getElementById('totalhrs<?=$empnum?>').value = W<?=$empnum?>;
var holiday<?=$empnum?> = document.getElementById('holihrs<?=$empnum?>').value = H<?=$empnum?>;
var sick<?=$empnum?> = document.getElementById('sickdays<?=$empnum?>').value = S<?=$empnum?>;
var wage<?=$empnum?> = document.getElementById('wagefreq<?=$empnum?>').value;
if(wage<?=$empnum?> == 'Weekly'){
document.getElementById('gross<?=$empnum?>').value = <?=$empsalarywage?>;
}
else if(wage<?=$empnum?> == '4 Weekly'){
document.getElementById('gross<?=$empnum?>').value = (<?=$empsalarywage?> / 4);
}
else if(wage<?=$empnum?> == 'Monthly'){
document.getElementById('gross<?=$empnum?>').value = ((<?=$empsalarywage?> * 12) / 52);
}
document.getElementById('weeklysheet<?=$empnum?>').submit('save<?=$empnum?>');
}
</script>
this the form:
<form id="weeklysheet<?=$empnum?>" name="weeklysheet" method="post" action="salary-function.php" >
<tbody>
<tr style="line-height:0px;">
<td class="large">
<p class="employee_name"><?=$fullname?></p>
</td>
<td class="small">
<input name="mon" id="mon<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $mon?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="tue" id="tue<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $tue?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="wed" id="wed<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $wed?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="thu" id="thu<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $thu?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="fri" id="fri<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $fri?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="sat" id="sat<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $sat?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="sun" id="sun<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $sun?>"onchange="grab<?=$empnum?>()"/>
</td >
<td class="small">
<input name="totalhrs" id="totalhrs<?=$empnum?>" type="text" class="cumul_week_input" readonly value="<?php echo $totalhrs?>"/>
</td>
<td class="small">
<input name="holihrs" id="holihrs<?=$empnum?>" type="text" class="cumul_week_input" readonly value="<?php echo $holihrs?>"/>
</td>
<td class="small">
<input name="sickdays" id="sickdays<?=$empnum?>" type="text" class="cumul_week_input" readonly value="<?php echo $sickdays?>"/>
</td>
<td class="small">
<input name="gross" id="gross<?=$empnum?>" type="text" class="cumul_week_input" readonly value="<?php echo $gross?>"/>
</td>
<td>
<input type="submit"name="save" id="save<?=$empnum?>" value="Confirm" style="display:none"/>
<input type="hidden"name="enddate" value="<?php echo $data['currentWeekStart']; ?>"/>
<input type="hidden"name="wagefreq"id="wagefreq<?=$empnum?>" value="<?=$wagefreq?>"/>
</td>
</tr>
</tbody>
</form>
that's something that worked like I want, but if anyone see anything that could be done in a better way, I would really apreciate any corrections...
thank's
Related
I used an array to print the data in several dynamically generated drop-down lists, but the result is very disappointing and does not bring the expected values from the database in the drop-down lists that I generate when needed, noting that the first drop-down list, which is outside the dynamic generation context, works well and brings the expected data, but the problem is Any new list generated dynamically, the values will only appear with the word "undefined" Any solution will be greatly appreciated...
here problem as GIF amage
php code:
`
<td class="td"><select class="form-control select_acount" name="account[]"required>
<option value="">--الحساب--</option>
<?php
$query = "select * from sah7atmain_acouts WHERE company like '".$multi."'";
$result = mysqli_query($con,$query);
$data = array();
if($result)
{
while($row = mysqli_fetch_assoc($result)){
$data[] = $row;
?>
<?php echo '<option value="'.$row['acount_mounte'].'">'.$row['acount_name'].'</option>'?>;
<?php
}
} else{
}
?>
</select>
</td>
jquery dynamic genaration code:
var data = '<?php echo $data; ?>';
var product_dd = "";
product_dd += '<select class="form-control select_acount" name="account[]"required>';
product_dd += '<option value="">--الحساب--</option>';
if(data.length > 0){
for(let i = 0; i < data.length; i ++) {
product_dd += `<option value="${data[i]['acount_mounte']}">${data[i]['acount_name']}</option>`;
}
}
product_dd += "</select>";
var i = 0;
$("#add-btn").click(function() {
++i;
$("#dynamicAddRemove").append('<tr><td class="td">'+product_dd+'</td> <td class="td"><input type="number" name="debtor[' + i + ']" id="fname"class="form-control debtor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="number" name="creditor[' + i + ']" id="james" class="form-control creditor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="text" name="description[' + i + ']" class="form-control" required></td> <td> <input type="text" name="mount[' + i + ']" class="form-control mount" required> </td><button type="button" name="add" class="btn btn-danger remove-tr"><i class="fas fa-trash-alt"></i></button></td></tr>');
});
$(document).on('click', '.remove-tr', function() {
$(this).parents('tr').remove();
});
`
1.can someone find why the form doesnt add/submit the values to database ?
i have tried adding values by the form and testing but its not getting in the database
2.and on calculation i get soms values with more than 2 decimals how to fix that because at the script/code i have decimals on 2 maybe someone ?
screenshot of decimals http://prntscr.com/5fbedf
<html>
<head>
</head><body>
<?php require "config.php" ; ?>
<?php require "admin_menu.php" ;?>
<?php
if(isset($_POST['verzenden']))
{
$id = addslashes($_POST['id']);
$flesnummer = addslashes($_POST['flesnummer']);
$begin_gewicht = addslashes($_POST['begin_gewicht']);
$verbruikt = addslashes($_POST['verbruikt']);
$eind_gewicht = addslashes($_POST['eind_gewicht']);
$verschil = addslashes($_POST['verschil']);
$tara_gewicht = addslashes($_POST['tara_gewicht']);
$over = addslashes($_POST['over']);
mysql_query("INSERT INTO Flesnummer (id, flesnummer, begin_gewicht, verbruikt, eind_gewicht, verschil, tara_gewicht, over) VALUES ('".$id."', '".$flesnummer."', '".$begin_gewicht."', '".$verbruikt."', '".$eind_gewicht."', '".$verschil."', '".$tara_gewicht."', '".$over."')");
echo '<br><br><br>Fles succesvol toegevoegd';
}
else
{
?>
<script>
function totaalIt() {
var x = document.getElementById("begin_gewicht").value;
var y = document.getElementById("tara_gewicht").value;
var z = document.getElementById("verbruikt").value;
var w = document.getElementById("eind_gewicht").value;
if ((isNumeric(x, true)) && (isNumeric(y, true)) && (isNumeric(z, true))&& (isNumeric(w, true))) {
x = parseFloat(x);
y = parseFloat(y);
z = parseFloat(z);
w = parseFloat(w);
var wverschil = x - y - z + y - w
var xyzover = x - y - z
var xyztotaal = x - y - z + y;
document.getElementById("verschil").value = wverschil;
document.getElementById("over").value = xyzover;
document.getElementById("totaal").value = xyztotaal;
if (confirm ("Formule: Begin gewicht - tara gewicht = vulling - verbruikt = vulling over + tara gewicht = totaal - eind gewicht = verschil(verlies) ?")) {
document.getElementById("form1").submit();
}
} else {
alert("Numeric Values must be entered.");
}
}
function isNumeric(sText, decimalAllowed) {
if (sText.length == 0) return false;
var validChars = "";
if (decimalAllowed) {
validChars = "0123456789.";
} else {
validChars = "0123456789";
}
var isNumber = true;
var charA;
var decimalCount = 0;
for (i = 0; i < sText.length && isNumber == true && decimalCount < 2; i++) {
charA = sText.charAt(i);
if (charA == ".") {
decimalCount += 1;
}
if (validChars.indexOf(charA) == -1) {
isNumber = false;
}
}
return isNumber;
}
</script>
<form name="form1" id="form1" action=" <?=$_SERVER['PHP_SELF']?> " method="POST">
<table>
<input type="hidden" name="id" >
<tr><td>Flesnummer:</td><td><input name="flesnummer" id="flesnummer" type="text" size"4">
<tr><td>Begin gewicht:</td><td><input name="begin_gewicht" type="text" id="begin_gewicht" size="4" />
Kg</td></tr>
<tr><td>Tara gewicht:</td><td><input name="tara_gewicht" type="text" id="tara_gewicht" size="4" />
Kg</td></tr>
<tr><td>Verbruik:</td><td><input name="verbruikt" type="text" id="verbruikt" size="4" />
Kg</td></tr>
<tr><td>Vulling over:</td><td><input name="over" type="text" id="over" size="4" readonly="true" />
Kg (automatisch berekend)</td></tr>
<tr><td>Eind gewicht:</td><td><input name="eind_gewicht" type="text" id="eind_gewicht" size="4" />
Kg</td></tr>
<tr><td>Eind gewicht: <img title="Eindgewicht volgens berekening" src="img/question.gif" width="15" height="15" onMouseDown="Eindgewicht volgens berekening" alt="Eindgewicht volgens berekening." /></td><td><input name="totaal" type="text" id="totaal" size="4" readonly="true" />
Kg (automatisch berekend)</td></tr>
<tr><td>Verschil(verlies):</td><td><input name="verschil" type="text" id="verschil" size="4" readonly="true" />
Kg (automatisch berekend)</td></tr>
<tr><td><input type="button" name="verzenden" id="verzenden" value="Verzenden" onClick="totaalIt()" /></td></tr>
</table></form>
<?
}
?>
On your form, try turning your button input to submit:
<input type="button" name="verzenden" id="verzenden" value="Verzenden" onClick="totaalIt()" />
TO:
<input type="submit" name="verzenden" id="verzenden" value="Verzenden" onClick="totaalIt()" />
Your button is not registering the name so it's not getting to the $_POST['verzenden'] in your check:
// There is no $_POST['verzenden'] with the button.
if(isset($_POST['verzenden'])) {
I have a function static who can calculate all my static input:
$('document').ready(function() {
$('#input5,#tempsam ').each(function () {
$(this).on('change',recalculate);
});
});
function recalculate () {
if(isNaN(this.value))
alert("Please enter a number");
else {
var b = 0;
if (($('#input5').val() !== b) && ($('#input2').val() !== b) && ($('#tempsam').val() !== b )) {
var a = 40;
var value1 = $('#input1').val() == "" ? 0 : parseFloat($('#input1').val());
var value7 = $('#tempsam').val() == "" ? 0 : parseFloat($('#tempsam').val());
var total = value5 + value2 + value7
$('#result').val(total);
}
}
}
How I calculate my dynamic input and my static one?
Here is how I calculate my static one each day:
//jeudi calcul
function checkfieldjeudi()
{
var dynamicjeudi = document.getElementsByClassName("dynamicjeudi");
var itemCount5 = dynamicjeudi.length;
var total = 0;
for(var i = 0; i < itemCount5; i++) {
total = total + parseFloat(dynamicjeudi[i].value);
total = total + parseFloat(document.getElementById('input5').value);
}
document.getElementById('totaljeudi').value = total;
}
checkfieldjeudi();
How I make dynamic jeudi
$('#calculTempsdivjeu').append(
( '<input id="calculTempsdivjeu' + counterjeudi + '" name="calculTempsdivjeu[]' + '" type="number" onchange="checkfieldjeudi();" size="10" min="0" max="24" value="0" class="dynamicjeudi" onblur="autre();" onfocus="enter();"/>')
) //désactive champs précédent.
Here my static jeudi
<div id="calculTempsdivjeu">
<input step="any" type="number" id="totaljeudi" class="totaljeudi" onchange="checkfieldjeudi();" name="totaljeudi" size="10" min="0" max="24" value="0" onblur="autre();" onfocus="enter();"/>
<input step="any" type="number" onblur="autre();" onfocus="enter();" onchange="checkfieldjeudi();" id="input5" class="temps" name="tempsje" max="24" min="0" value="0" />
</div>
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;
?>
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;
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']
For some reasons my javascript form validation does not go through every row in the table.
I created the table with the codes below:
The $usr variable is just a result of another process:
<form name="myForm" action="addAccessories.php" method="post" onsubmit="return validateForm(this);">
<table border="1" id="IT">
<tr>
<th>Barcode<em>*</em></th>
<th>Current stock</th>
</tr>
<?php
for($id=1; $id<=$usr; $id++){
?>
<tr>
<td><input type="hidden" name="id[]" value="<?php echo $id; ?>" />
<input type="text" name="bar_code<?php echo $id; ?>" id="bar_code<?php echo $id; ?>" value="" /></td>
<td><input type="text" name="num_stock<?php echo $id; ?>" value="0" id="num_stock<?php echo $id; ?>"/></td>
<?php
}
?>
<tr>
<td> </td>
<td> <button data-theme="b" input type="submit" name="Submit" value="Submit">Add accessories</button></td>
</tr>
</table>
</form>
The number of rows from this table is: rows = $usr + 1
My validation form codes:
<script type="text/javascript">
function validateForm(){
var errors = [];
for(i = 1; i < document.getElementById("IT").rows.length; i++){
var barcode = document.getElementById('bar_code'+i).value;
var des = document.getElementById('description'+i).value;
var num_stock = document.getElementById('num_stock'+i).value;
if(barcode == null || barcode == ""){
errors[errors.length] = "You must enter barcode.";
}
if(isNaN(num_stock)){
errors[errors.length] = "Current stock must be an integer";
}
if(num_stock < 0){
errors[errors.length] = "Current stock must be a positive number";
}
if (errors.length > 0) {
reportErrors(errors);
return false;
}
return true;
}
}
function reportErrors(errors){
var msg = "There were some problems...\n";
for (var i = 0; i<errors.length; i++) {
var numError = i + 1;
msg += "\n" + numError + ". " + errors[i];
}
alert(msg);
}
</script>
This process only validates the 1st row in the table then stop. Can anyone show me what went wrong and how to fix it?
Thank you very much for your help
Your code is making life way more complex than it needs to be. A simpler approach is to just deal with the form controls, e.g. since you are already passing a reference to the form from the submit listener:
function validateForm(form) {
var errors = [];
var c, cs = form.elements;
var reB = /^bar_code/;
var reN = /^num_stock/;
var reD = /^[0-9]+$/;
for (var i=0, iLen=cs.length; i<iLen; i++) {
c = cs[i];
if (reB.test(c.name) && c.value == '') {
errors.push('You must enter barcode.'];
} else if (reN.test(c.name) && !reD.test(c.value)) {
errors.push('Stock number must be a positive integer.'];
}
}
// deal with errors...
}
Note that in the test:
> barcode == null || barcode == ""
barcode is a string, so the first test will never return true and the second is sufficient if you want to see if it's empty.