Giri J Posted January 20, 2009 Share Posted January 20, 2009 Hi, First thing PHP freaks Rocks. People are helpful, quick, genius and dynamic . Thank you PHP freaks and all the people in here. I'm a beginner in PHP. I have a dynamic form. It generates n no. of text fields at 1 shot. Here is the HTML Code: /// HTML Code Starts here /// <html><head> <title>My Test Page</title> <script type="text/javascript"> function addFieldz() { var i = document.getElementById('rows').value; alert ("i val = "+i); while (i > 0) { txtFields.innerHTML = txtFields.innerHTML+"<br><input type=\"text\" name=\"t"+i+"\" id=\"t"+i+"\" \/><br/>"; i--; } } </script></head> <body> <form method="post" action="test-js-php.php"> No. of Text Fields: <input type="text" name="rows" id="rows" style="width:30px;"> <input type="button" onClick="addFieldz()" name="add" id="add" value="Add"> <div id="txtFields"> </div> <br/> <input type="submit" name="submit" id="submit" value="submit"> <input type="reset" name="reset" id="reset" value="reset"> </form> </body></html> /// HTML Code Ends here /// So Initally I wanted to test it for 3 text fields. Here is my PHP code. <?php $val1 = $_POST['t1']; $val2 = $_POST['t2']; $val3 = $_POST['t3']; echo $val1.$val2.$val3; ?> This works. I was really happy by this achievement. But now comes the real problem. How can I retrieve the values if there 100 text fields ?? . Is there a way in php, so we can get the post values in a loop by some conditions?? Please help. Thanks in Advance. Quote Link to comment https://forums.phpfreaks.com/topic/141623-solved-dynamic-form-and-php-help/ Share on other sites More sharing options...
Maq Posted January 20, 2009 Share Posted January 20, 2009 Make the name for ALL your text fields "t[]" and your PHP will look something like: foreach ($_POST as $key => $value) { echo "key: " . $key . " value: " . $value; } Quote Link to comment https://forums.phpfreaks.com/topic/141623-solved-dynamic-form-and-php-help/#findComment-741313 Share on other sites More sharing options...
Giri J Posted January 21, 2009 Author Share Posted January 21, 2009 Hi Thank you for replying back. As I'm new I'm trying to understand the concept. You mean to say I have to change the following statement from txtFields.innerHTML = txtFields.innerHTML+"<br><input type=\"text\" name=\"t"+i+"\" id=\"t"+i+"\" \/><br/>"; to txtFields.innerHTML = txtFields.innerHTML+"<br><input type=\"text\" name=\"t["+i+"]\" id=\"t["+i+"]\" \/><br/>"; So that I can read it as array in php or something like that? Please let me know. Thanks again Giri Quote Link to comment https://forums.phpfreaks.com/topic/141623-solved-dynamic-form-and-php-help/#findComment-742002 Share on other sites More sharing options...
Giri J Posted January 21, 2009 Author Share Posted January 21, 2009 Hi I've changed my JS code to what is suggested. So My Js dynamic test field looks like this txtFields.innerHTML = txtFields.innerHTML+"<br><input type=\"text\" name=\"t["+i+"]\" id=\"t["+i+"]\" \/><br/>"; and I've used the code suggested for PHP my php code is like this <?php foreach ($_POST as $key => $value) { echo "<br/>key: " . $key . " <br/>value: " . $value . "<br/>"; ?> This is the output I get key: rows value: 2 key: t value: Array key: submit value: submit aah I'm confused. The " key:t value:Array " are actually the text fields that are dynamically generated. I need the values in that text fields. Is there a way we can retrieve them? I've tried $t[0] but it didn't work. Any help?? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/141623-solved-dynamic-form-and-php-help/#findComment-742008 Share on other sites More sharing options...
Giri J Posted January 21, 2009 Author Share Posted January 21, 2009 Hi Maq, Thanks a lot for the help. I've just made some experiments based on the change you told and went through the following link http://www.phpfreaks.com/forums/index.php/topic,216783.0.html and finally my problem is solved. now I generate 100 text fields and I'm able to get all the data in one shot thanks PHP Freaks ! If some one out there having some similar problem like mine here is the solution. MY HTML CODE : (Dynamic Form using JS for generating the textfields) <html><head> <title>My Test Page - Giri JS + PHP + Bynamic Form it works</title> <script type="text/javascript"> function addFieldz() { var i = document.getElementById('rows').value; alert ("i val = "+i); while (i > 0) { txtFields.innerHTML = txtFields.innerHTML+"<br><input type=\"text\" name=\"t[]\" id=\"t[]\" \/><br/>"; i--; } } </script></head> <body> <form method="post" action="test-js-php.php"> No. of Text Fields: <input type="text" name="rows" id="rows" style="width:30px;"> <input type="button" onClick="addFieldz()" name="add" id="add" value="Add"> <div id="txtFields"> </div> <br/> <input type="submit" name="submit" id="submit" value="submit"> <input type="reset" name="reset" id="reset" value="reset"> </form> </body></html> MY PHP CODE: <?php $num = $_POST['rows']; //echo "<br/>num val outside - ".$num; while ($num > 0) { //echo "<br/>num val in while - ".$num; echo "<br/>form value: ".$_POST['t'][$num-1]."<br/>"; $num--; } ?> Hope this helps someone. A 100,000 thanks to Maq and 1,000,000,000 thanks to PHP Freaks ! Giri. Quote Link to comment https://forums.phpfreaks.com/topic/141623-solved-dynamic-form-and-php-help/#findComment-742066 Share on other sites More sharing options...
Maq Posted January 21, 2009 Share Posted January 21, 2009 Yes sorry. What I gave you would get all POST fields that were submitted. You just wanted all the 't array' fields. Glad you figured it out Quote Link to comment https://forums.phpfreaks.com/topic/141623-solved-dynamic-form-and-php-help/#findComment-742166 Share on other sites More sharing options...
Giri J Posted January 21, 2009 Author Share Posted January 21, 2009 that's alright It was great you actually took time and replied me. Thank you. Have a good day. Quote Link to comment https://forums.phpfreaks.com/topic/141623-solved-dynamic-form-and-php-help/#findComment-742338 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.