flemingmike Posted September 10, 2013 Share Posted September 10, 2013 (edited) 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 September 10, 2013 by flemingmike Quote Link to comment https://forums.phpfreaks.com/topic/282057-foreach-help/ Share on other sites More sharing options...
fastsol Posted September 10, 2013 Share Posted September 10, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282057-foreach-help/#findComment-1449041 Share on other sites More sharing options...
flemingmike Posted September 10, 2013 Author Share Posted September 10, 2013 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" Quote Link to comment https://forums.phpfreaks.com/topic/282057-foreach-help/#findComment-1449042 Share on other sites More sharing options...
Barand Posted September 10, 2013 Share Posted September 10, 2013 if inputs are always in pairs foreach ($_POST['d'] as $k => $d) { $q = $_POST['q'][$k]; // insert $d and $q into db table } Quote Link to comment https://forums.phpfreaks.com/topic/282057-foreach-help/#findComment-1449043 Share on other sites More sharing options...
flemingmike Posted September 10, 2013 Author Share Posted September 10, 2013 So if I wanted to submit it as 3 fields and the third being r, would I do it as below? foreach ($_POST['d'] as $k => $d) { $q = $_POST['q'][$k]; $r = $_POST['r'][$k]; // insert $d and $q into db table } Quote Link to comment https://forums.phpfreaks.com/topic/282057-foreach-help/#findComment-1449044 Share on other sites More sharing options...
Solution Barand Posted September 10, 2013 Solution Share Posted September 10, 2013 You've got it Quote Link to comment https://forums.phpfreaks.com/topic/282057-foreach-help/#findComment-1449046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.