chris11 Posted October 5, 2011 Share Posted October 5, 2011 Can someone tell me if this is the most pratical way to do this. Its a form with ten inputs and each input goes to a different row in the table. sometimes a user will only fill six or some of the ten fields and therefore i only want the table to have six rows added. So i struggle with this part. the way i have it works but i feel a foreach with and array is better. more dynamic. and less to work with. is it best to turn it into an array with "links[]" rather than what i have and how do i do that. <table width="100%"> <tr> <td width="140" style="padding:4px;">link 1:</td> <td ><input type="text" class="edit bk" name="link_one" style="width:350px"><a href="#" class="hintanchor" onMouseover="showhint('add your url ', this, event, '250px')">[?]</a></td> </tr> <tr> <td width="140" style="padding:4px;">link 2:</td> <td ><input type="text" class="edit bk" name="link_two" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 3:</td> <td ><input type="text" class="edit bk" name="link_three" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 4:</td> <td ><input type="text" class="edit bk" name="link_four" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 5:</td> <td ><input type="text" class="edit bk" name="link_five" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 6:</td> <td ><input type="text" class="edit bk" name="link_six" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 7:</td> <td ><input type="text" class="edit bk" name="link_seven" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 8:</td> <td ><input type="text" class="edit bk" name="link_eight" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 9:</td> <td ><input type="text" class="edit bk" name="link_nine" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 10:</td> <td ><input type="text" class="edit bk" name="link_ten" style="width:350px"></td> </tr> </table> <?php $link_one = htmlspecialchars($_POST['link_one']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$link_one)) { die("URL address not valid"); } $link_two = htmlspecialchars($_POST['link_two']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$link_two)) { die("URL address not valid"); } $link_three = htmlspecialchars($_POST['link_three']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$link_three)) { die("URL address not valid"); } $link_four = htmlspecialchars($_POST['link_four']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$link_four)) { die("URL address not valid"); } $link_five = htmlspecialchars($_POST['link_five']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$link_five)) { die("URL address not valid"); } $link_six = htmlspecialchars($_POST['link_six']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$link_six)) { die("URL address not valid"); } $link_seven = htmlspecialchars($_POST['link_seven']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$link_seven)) { die("URL address not valid"); } $link_eight = htmlspecialchars($_POST['link_eight']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$link_eight)) { die("URL address not valid"); } $link_nine = htmlspecialchars($_POST['link_nine']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$link_nine)) { die("URL address not valid"); } $link_ten = htmlspecialchars($_POST['link_ten']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$link_ten)) { die("URL address not valid"); } if ( trim( $link_one ) != "" ) { $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_one}')" ); } if ( trim( $link_two ) != "" ) { $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_two}')" ); } if ( trim( $link_three ) != "" ) { $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_three}')" ); } if ( trim( $link_four ) != "" ) { $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_four}')" ); } if ( trim( $link_five ) != "" ) { $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_five}')" ); } if ( trim( $link_six ) != "" ) { $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_six}')" ); } if ( trim( $link_seven ) != "" ) { $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_seven}')" ); } if ( trim( $link_eight ) != "" ) { $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_eight}')" ); } if ( trim( $link_nine ) != "" ) { $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_nine}')" ); } if ( trim( $link_ten ) != "" ) { $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_ten}')" ); } ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 5, 2011 Share Posted October 5, 2011 Any time you have a set of related data that is processed identically and only differs in the name or value, you should let the computer do the work for you by using an array/loop to both produce the form and process the data. Quote Link to comment Share on other sites More sharing options...
possien Posted October 5, 2011 Share Posted October 5, 2011 Does the user of the form know before hand how many entries they going to make? If so, I would make two forms, one asking the number (integer) of links and the second generated from the results of the first. The second from could have the name fields correspond to the number and make a for each. This would greatly shorten the amount of html and php code. Quote Link to comment Share on other sites More sharing options...
chris11 Posted October 5, 2011 Author Share Posted October 5, 2011 No the user is mostly me and i am not sure of the number. Okay i figured it was redundant being that its the same data over and over. Thanks very much. ill muck around with an array. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 5, 2011 Share Posted October 5, 2011 if(isset($_POST['links'])) { //Apply trim, remove empty values, apply htmlspecialcharacters $links = array_map(array_filter(array_map($_POST['links'], 'trim')), 'htmlspecialchars'); //Create array to hold insert values $values = array(); //Create array for results messages $results = array(); //Process processed post data foreach($links as $link) { if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $link)) { $results[] = "'{$link}' is an invalid link."; } else { $values[] = "('{$row}', '{$link}')"; $results[] = "'{$link}' was inserted into database."; } } $query = "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES " . implode(', ', $values); $db->query($query); echo implode("<br>\n", $results); } Quote Link to comment Share on other sites More sharing options...
chris11 Posted October 5, 2011 Author Share Posted October 5, 2011 Thanks very much. Thats exactly what i was after . How would i write the html? like this? <tr> <td width="140" style="padding:4px;">link 1:</td> <td ><input type="text" class="edit bk" name="link[]" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 2:</td> <td ><input type="text" class="edit bk" name="link[0]" style="width:350px"></td> </tr> Quote Link to comment Share on other sites More sharing options...
chris11 Posted October 5, 2011 Author Share Posted October 5, 2011 kk thanks to everyone. works well. final result <table width="100%"> <tr> <td width="140" style="padding:4px;">link 1:</td> <td ><input type="text" class="edit bk" name="links[]" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 2:</td> <td ><input type="text" class="edit bk" name="links[]" style="width:350px"></td> </tr> <tr> <td width="140" style="padding:4px;">link 3:</td> <td ><input type="text" class="edit bk" name="links[]" style="width:350px"></td> <tr> </table> <?php if(isset($_POST['links'])) { //Apply trim, remove empty values, apply htmlspecialcharacters $links = array_map(array_filter(array_map($_POST['links'], 'trim')), 'htmlspecialchars'); //Create array to hold insert values $values = array(); //Create array for results messages $results = array(); //Process processed post data foreach($_POST['links'] as $link) { if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $link)) { $results[] = "'{$link}' is an invalid link."; } else { $values[] = "('{$row}', '{$link}')"; $results[] = "'{$link}' was inserted nto database."; } } $query = "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES" . implode(', ', $values); $db->query($query); echo implode("<br>\n", $results); } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 5, 2011 Share Posted October 5, 2011 Why would you "write out" the HTML for the form fields when you can easily create them programatically? $link_count = 10; for($link_no=1; $link_no<=$link_count; $link_no++) { echo "<tr> <td width='140' style='padding:4px;'>link {$link_no}:</td> <td ><input type='text' class='edit bk' name='links[]' style='width:350px;'></td> </tr>\n"; } Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 5, 2011 Share Posted October 5, 2011 //load in jquery <script type="text/javascript"> <!-- $(document).ready(function() { $('.add').click(function() { var len = (parseInt($('tr').length) + 1); var htm = ' <tr> <td width="140" style="padding:4px;">link ' + len + ':</td> <td ><input type="text" class="edit_bk" name="links[]" style="width:350px"></td> </tr>'; $('tr:last').after(htm); }); }); //--> </script> <table width="100%"> <tr> <td width="140" style="padding:4px;">link 1:</td> <td ><input type="text" class="edit bk" name="links[]" style="width:350px"></td> </tr> </table> <a href="javascript:void(0);" class="add">Add FIeld</a> Quote Link to comment Share on other sites More sharing options...
chris11 Posted October 5, 2011 Author Share Posted October 5, 2011 @mjdamato Thank u again. That worked well. One issue im having is if i submit a totally black form i get an error. also if the first field has with something other than a url and i don't fill the other fields i get an error. otherwise it's good! Another part of this I'm struggling with is what if I would like to add another field/column like a name that goes along with the url ( uid ||matchid || name || link ) and have both the 'names[]' field and the 'links[]" field populate the same row. I tried two foreach statements and it printed out 4 of each as one would expect. how do i pass another array through the foreach. I'm not sure if that's even the best method.. Also how would I incorporate that with your replacement for my hard-coded html attack at it? Thanks very much. I have searched the net for ways to do this. I might be over thinking it I'm really not sure. Thanks, again Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 6, 2011 Share Posted October 6, 2011 The script is generating an error if you have no values - or if you only enter invalid values - because there is no validation before the foreach statement that the array still has values. Just do an if/else statement right before the foreach() statement such as if(!count($links)) { //Display error message that no valid values were entered } else { //Perform the processing of the $links } As to your second question. If you are going to have "sets" of fields to be processed together you will want to link the fields to gether. Some people would just tell you that you can associate the first value (0 index) from $_POST['links'] with the first value from $_POST['names'], etc. But, I would advise against it. If you were to ever add checkboxes that will no longer work because only checked checkboxes are passed in post data. Also, I think it just makes it less problematic if you have a more definitive way to link the fields. In this case I would add an index to the form field names. This will be easy using the previous code I provided above. Example: $link_count = 10; for($link_index=1; $link_index<$link_count; $link_index++) { $link_no = $link_index+1 echo "<tr> <td width='140' style='padding:4px;'>link {$link_no}:</td> <td >URL: <input type='text' class='edit bk' name='links[{$link_index}]' style='width:350px;'></td> <td >Name: <input type='text' class='edit bk' name='names[{$link_index}]' style='width:350px;'></td> </tr>\n"; } Now, you can process the sets of fields using something like foreach($_POST['links'] as $index => $link_url) { $link_name = $_POST['names'][$index]; } With this approach you would want to move the validations inside the loop since you need to validate the "sets" of records as a whole. Quote Link to comment Share on other sites More sharing options...
chris11 Posted October 6, 2011 Author Share Posted October 6, 2011 All 's You're a king among men, mjdamato. Thanks very much for taking some time out to help. Yeah I tried array_combine and all sorts of stuff. Even submitting the form twice and updating with a total separate query. Also processing "$_Post as" rather than "$_post[names] as" but that would cause issues with other parts of my form i think. In the end where i stopped I was trying to define the $names = $_post[names] outside of the loop. anyhow. Thanks very much. This is a great example and concept for clean dynamic php forms with multiple $_post Quote Link to comment 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.