Jump to content

foreach help


Go to solution Solved by Barand,

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>";
}
}


Edited by flemingmike
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.