noj75 Posted May 20, 2009 Share Posted May 20, 2009 Hi guys, I have a form that has some jquery added to it so that my user can add new rows of date by adding more form fields. Here is the script: <?php ob_start(); include('connect.php'); $hID = $_REQUEST['ID']; $hname = $_REQUEST['name']; ?> <head> <script language="Javascript" src="jquery.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> var current = 1; function addEntry() { current++; var strToAdd = '<tr><td><p><input id="eracedated'+current+'" name="eracedated'+current+'" size="5" /> <input id="eracedatem'+current+'" name="eracedatem'+current+'" size="5" /> <input id="eracetime'+current+'" name="eracetime'+current+'" size="5" /> <input id="eracetype'+current+'" name="eracetype'+current+'" size="15" /> <input id="eraceage'+current+'" name="eraceage'+current+'" size="5" /> <input id="eentdated'+current+'" name="eentdated'+current+'" size="5" /> <input id="eentdatem'+current+'" name="eentdatem'+current+'" size="5" /> <input id="eracetrack'+current+'" name="eracetrack'+current+'" size="15" /> <input id="eracedistance'+current+'" name="eracedistance'+current+'" size="5" /> <input type="hidden" id="ehid'+current+'" name="ehid'+current+'" value="<?php echo $hID; ?>" /><input type="hidden" id="ehorse'+current+'" name="ehorse'+current+'" value="<?php echo $hname; ?>" /></p></td></tr>' $('#mainField').append(strToAdd) } $(document).ready(function(){ $('#addEntry').click(addEntry) }); </script> </head> <style type="text/css"> body { background-color:#4a0303; } </style> <div style="background-color:#734e4e; border:2px solid #000000; color:#FFFFFF; padding:10px; width:770px;"> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr> <td> <fieldset style="border:none;" id="mainField"> <p> <input id="eracedated1" name="eracedated1" size="5" /> <input id="eracedatem1" name="eracedatem1" size="5" /> <input id="eracetime1" name="eracetime1" size="8" /> <input id="eracetype1" name="eracetype1" size="15" /> <input id="eraceage1" name="eraceage1" size="5" /> <input id="eentdated1" name="eentdated1" size="8" /> <input id="eentdatem1" name="eentdatem1" size="8" /> <input id="eracetrack1" name="eracetrack1" size="15" /> <input id="eracedistance1" name="eracedistance1" size="5" /> <input type="hidden" id="ehid1" name="ehid1" value="<?php echo $hID; ?>" /> <input type="hidden" id="ehorse1" name="ehorse1" value="<?php echo $hname; ?>" /> </p> </fieldset> </td> </tr> <tr> <td> <p> <input type="button" id="addEntry" value="Add Another Entry"> </p> </td> </tr> <tr> <td> <input type="submit" value="Upload Entries"> <input type="submit" name="cancel" onclick="window.parent.location.href=window.parent.location.href;" value="Cancel" /> </td> </tr> </table> </form> </div> If my user clicks the addentry button a new set of fields will appear so that they can add another row. Now comes the tricky part, how the hell do I process this with PHP to add each line as a record into my database? It really is stumping me on how to do it. Any help, as always, is very much appreciated. Kind regards Link to comment https://forums.phpfreaks.com/topic/158951-breaking-up-forms-for-query/ Share on other sites More sharing options...
noj75 Posted May 20, 2009 Author Share Posted May 20, 2009 The more I am looking into this the more I am thinking it is not possible. Anyone have an igloo on this? Link to comment https://forums.phpfreaks.com/topic/158951-breaking-up-forms-for-query/#findComment-838364 Share on other sites More sharing options...
Ken2k7 Posted May 20, 2009 Share Posted May 20, 2009 Okay, change all those names in format of: somename# to somename[] So eracedated1 should be eracedated[] instead. Do that for all your inputs (both in your jQuery and in the pre-set form. Then you can remove the JavaScript reference to current. In PHP, just use $_POST['eracedate'] to get all of them. It will be an array and you can use a foreach loop to get all the data. When in doubt, use var_dump($_POST['eracedate']); to see what's in the array. Good luck! Link to comment https://forums.phpfreaks.com/topic/158951-breaking-up-forms-for-query/#findComment-838374 Share on other sites More sharing options...
noj75 Posted May 20, 2009 Author Share Posted May 20, 2009 That is so annoying! I tried that and it wasnt working because I didnt set the array [] to the javascript names! LOL Thanks very much mate. Now, how would I store each form set into the database? i.e so they insert into seperate rows? Sorry to be a pain! Link to comment https://forums.phpfreaks.com/topic/158951-breaking-up-forms-for-query/#findComment-838388 Share on other sites More sharing options...
Ken2k7 Posted May 20, 2009 Share Posted May 20, 2009 Just loop through the array and run mysql_query on each one. Since you have so many, I'll just show a preview. // do all your validation up here i.e.: the form is submitted and the fields aren't empty, etc. $arr1 = $_POST['eracedate']; // get the rest of the $_POST data... // since they are in blocks, I can assume they're all of the same length, so looping through one count is fine. $arrlen = count($arr1); foreach ($e=0; $e < $arrlen; $e++) { $arr1_val1 = $arr1[$e]; // same for the rest of the arrays mysql_query(...) or trigger_error(mysql_error()); } Link to comment https://forums.phpfreaks.com/topic/158951-breaking-up-forms-for-query/#findComment-838396 Share on other sites More sharing options...
noj75 Posted May 20, 2009 Author Share Posted May 20, 2009 Sorry Ken, I didnt explain myself enough. $rdated = $_REQUEST['eracedated']; $rdatem = $_REQUEST['eracedatem']; $rtime = $_REQUEST['eracetime']; $rtype = $_REQUEST['eracetype']; $rage = $_REQUEST['eraceage']; $redted = $_REQUEST['eentdated']; $redtem = $_REQUEST['eentdatem']; $rtrack = $_REQUEST['eracetrack']; $rdist = $_REQUEST['eracedistance']; $rhid = $_REQUEST['ehid']; $rhorse = $_REQUEST['ehorse']; That is what I want in one row on my database, not individually. Does that make sense? Link to comment https://forums.phpfreaks.com/topic/158951-breaking-up-forms-for-query/#findComment-838400 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 You do realize they are arrays right? Do var_dump($rdated); and you'll see what I mean. Link to comment https://forums.phpfreaks.com/topic/158951-breaking-up-forms-for-query/#findComment-838544 Share on other sites More sharing options...
noj75 Posted May 21, 2009 Author Share Posted May 21, 2009 Hi Ken, Yep, I got it now. The problem I was having was that when I used FOREACH nothing was happening. I changed it to FOR and it works a treat: $rdated = $_REQUEST['eracedated']; $rdatem = $_REQUEST['eracedatem']; $rtime = $_REQUEST['eracetime']; $rtype = $_REQUEST['eracetype']; $rage = $_REQUEST['eraceage']; $redted = $_REQUEST['eentdated']; $redtem = $_REQUEST['eentdatem']; $rtrack = $_REQUEST['eracetrack']; $rdist = $_REQUEST['eracedistance']; $rhid = $_REQUEST['ehid']; $rhorse = $_REQUEST['ehorse']; $arrlen = count($rdated); for($e=0; $e < $arrlen; $e++) { $rdated1 = $rdated[$e]; $rdatem1 = $rdatem[$e]; // same for the rest of the arrays print $rdated1.' '.$rdatem1.'<br>'; } All I need to do now is simply change the print to an sql query and roberts your fathers brother! Thanks for the help Ken, you are a star! Link to comment https://forums.phpfreaks.com/topic/158951-breaking-up-forms-for-query/#findComment-838719 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.