Jump to content

foreach help


flemingmike

Recommended Posts

hello, im submitting a form that has 2 text fields.  im using javascript to add in another 2 fields if needed.  im not sure how to use a foreach to get both field twice.  here is my code below

$myInputsd = $_POST["d"];
$myInputsq = $_POST["q"];
foreach ($myInputsd as $eachInputd) {
foreach ($myInputsq as $eachInputq) {


	 $sql = "INSERT INTO test VALUES (
	 NULL, 
	 $eachInputd,
	 $eachInputq
	 )";
	 mysql_query($sql) or die('Error adding.  Check you fields and try again.');
	 
	 
     echo $eachInputd . $eachInputq . "<br>";
}
}


Link to comment
https://forums.phpfreaks.com/topic/282057-foreach-help/
Share on other sites

We'll probably need your full code as to how you are building the text fields and the javascript you're using to make those too.  Usually you don't use foreach loops on text fields but rather checkboxes or radio buttons.  Why are you thinking you need to use a foreach loop on them?  Can you explain your situation in more depth please so we can understand and post the rest of the relevant code.

Link to comment
https://forums.phpfreaks.com/topic/282057-foreach-help/#findComment-1449041
Share on other sites

form:

<script src="addInput.js" language="Javascript" type="text/javascript"></script>
<form method="POST" action="post.php">
     <div id="dynamicInput">
          Entry 1<br><input type='text' name='d[]'> <input type='text' name='q[]'>
     </div>
     <input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
	 <input type="submit" value="Submit" name="submit">
</form>

javascripts:

var counter = 1;
var limit = 6;
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='d[]'> <input type='text' name='q[]'>";
          document.getElementById(divName).appendChild(newdiv);
          counter++;
     }
}

Once I get this test working my full version form will have 8 txt boxes for each "Add Another"

Link to comment
https://forums.phpfreaks.com/topic/282057-foreach-help/#findComment-1449042
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.