Jump to content

Foreach error again


flemingmike

Recommended Posts

hello, I don't know how I screwed it up but I did! the following code will only add the first entry of the foreach. 

 

Form:

		<tr>
			<td valign="bottom">
			<div align="center">
				<table border="1" cellpadding="2" style="border-collapse: collapse" width="90%" bordercolorlight="#000000" bordercolordark="#000000">

					<tr>
						<td align="center" width="11%"><b>Q Labour</b></td>
						<td align="center" width="43%"><b>Description</b></td>
						<td align="center" width="13%"><b>Labour</b></td>
						<td align="center" width="12%"><b>Helper</b></td>
						<td align="center" width="19%"><b>Labour Markup</b></td>
					</tr>
					<tr>
						<td align='center' width='11%'>
						<input type='text' name='lqlabour[]' size='4'></td>
						<td align='center' width='43%'>
						<textarea rows='2' name='ldescription[]' cols='30'></textarea></td>
						<td align='center' width='13%'>
						<input type='text' name='llabour[]' size='7'></td>
						<td align='center' width='12%'>
						<input type='text' name='lhelper[]' size='7'></td>
						<td align='center' width='19%'>
						<input type='text' name='lmarkup[]' size='7' value='30%'></td>
					</tr>
					</table>
				<table border="1" cellpadding="2" style="border-collapse: collapse" width="90%" bordercolorlight="#000000" bordercolordark="#000000">					
					<tr>
					<div id="dynamicInput1">
					
					</div>					
					</tr>
					<tr>
						<td colspan="5">
						<input type="button" value="Add Line" onClick="addLabour('dynamicInput1');" style="float: right"></td>
					</tr>
					<tr>
						<td colspan="5">
						<p align="right"> </td>
					</tr>

				</table>
			</div>
			</td>
		</tr>

post.php

<?php
include '../config.php';



foreach ($_POST['lqlabour'] as $k => $lqlabour) {
    $ldescription = $_POST['ldescription'][$k];
	$llabour = $_POST['llabour'][$k];
	$lhelper = $_POST['lhelper'][$k];
	$lmarkup = $_POST['lmarkup'][$k];


	
	 $sql = "INSERT INTO test VALUES (
	 NULL, 
	 '" . mysql_real_escape_string($lqlabour) ."',
	 '" . mysql_real_escape_string($ldescription) ."',
	 '" . mysql_real_escape_string($llabour) ."',
	 '" . mysql_real_escape_string($lhelper) ."',
	 '" . mysql_real_escape_string($lmarkup) ."'
	 )";
	 mysql_query($sql) or die('Error adding.  Check you fields and try again.');
	 
	 
     echo $lqlabour . $ldescription . $llabour . $lhelper . $lmarkup . "<br>";
}





?>





javascript:

var counter = 1;
var limit = 3;
function addLabour(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = " <table border='1' cellpadding='2' style='border-collapse: collapse' width='90%' bordercolorlight='#000000' bordercolordark='#000000'>  <tr> <td align='center' width='11%'> <input type='text' name='lqlabour[]' size='4'></td> <td align='center' width='43%'> <textarea rows='2' name='ldescription[]' cols='30'></textarea></td> <td align='center' width='13%'> <input type='text' name='llabour[]' size='7'></td> <td align='center' width='12%'> <input type='text' name='lhelper[]' size='7'></td> <td align='center' width='19%'> <input type='text' name='lmarkup[]' size='7' value='30%'></td> </tr>  </table>";
          document.getElementById(divName).appendChild(newdiv);
          counter++;
     }
}

Link to comment
https://forums.phpfreaks.com/topic/282063-foreach-error-again/
Share on other sites

You have several things going on there between the form, the PHP and the JavaScript. When trying to debug code you need to remove "variables" (the things that can be different not the variables such as $foo) to see where the actual problem is. It appears you have one set of input fields and are using JavaScript to duplicate the fields. So, I must assume the JavaScript is creating the fields else you would be stating that is the problem. I'm betting that the problem is that the JS is not creating the fields properly and the submitted data is not what you think it is.

 

So, start with doing a print_r($_POST) to verify exactly what is being sent in the post data. If the passed data looks correct then the error is probably in the processing code. If it doesn't then the problem is probably in the JS code that creates the dynamic fields.

Link to comment
https://forums.phpfreaks.com/topic/282063-foreach-error-again/#findComment-1449066
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.