Joshua4550 Posted May 16, 2010 Share Posted May 16, 2010 Hey. Just wondering, if I loop through textboxes with PHP, and insert query for each - that if I added a new textbox with javascript, when submitted - would PHP read that one too? I'll write some code, maybe it'll explain with more ease. PHP code (When form submitted) $connection = mysql_connect('***', '***', '***'); $database = mysql_select_db('***'); for ($i = $_POST['txtboxNum']; $i >= 1; $i--) { echo $i; } $number = 0; $query = mysql_query("SELECT * FROM field"); while ($array = mysql_fetch_array($query)) { $number++; echo '<input type="text" name="txtbox'.$number.'" value="hello '.$number.'" />'; } echo '<input type="hidden" name="txtboxNum" value="'.$number.'" />'; If I were to make a Javascript code to add another textbox into the code with a name of the next number, when submitted - would PHP be able to read that? I'm only asking because Javascript is client-side and not sure if PHP reads frfom browser or what. I'm just not 100%, so need reassurance. PS: Ignore any logic errors with the PHP code, if there are any. I just wrote this up in the thread body field, my version of this works fine. Just need the question answered (: Thanks alot. Quote Link to comment https://forums.phpfreaks.com/topic/201965-quick-simple-question-php-with-javascript/ Share on other sites More sharing options...
Adam Posted May 16, 2010 Share Posted May 16, 2010 To answer your question, yes the data would get submitted. Couldn't you have just done a test to find out though? Quote Link to comment https://forums.phpfreaks.com/topic/201965-quick-simple-question-php-with-javascript/#findComment-1059194 Share on other sites More sharing options...
Joshua4550 Posted May 16, 2010 Author Share Posted May 16, 2010 Nope because I've got to make a more complex Javascript code before I can make the one to write another textbox in. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/201965-quick-simple-question-php-with-javascript/#findComment-1059197 Share on other sites More sharing options...
DavidAM Posted May 16, 2010 Share Posted May 16, 2010 Make sure you include the array brackets on the name when you name it using Javascript or it will not be part of the array: <FORM action="" method="POST"> <INPUT type="text" name="txtData[]"> <INPUT type="text" name="txtData[]"> <INPUT type="text" name="txtData[]"> </FORM> When POSTed this will produce an array in PHP: foreach ($_POST['txtData'] as $field) { echo $field . PHP_EOL; } When you add the field in Javascript, you need to make sure you set the name to "txtData[]" so it will be part of the array. Note: I've never actually done this, but my experience scanning through the form elements in Javascript seem to bear this out. Quote Link to comment https://forums.phpfreaks.com/topic/201965-quick-simple-question-php-with-javascript/#findComment-1059222 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.