coupe-r Posted October 3, 2010 Share Posted October 3, 2010 Hello All, Attached are 2 screenshots of the form and output for better reference. What I am trying to do is select all or some checkboxes and for those boxes that are checked, echo out their Payment Method. ---If a checkbox is checked but the Payment Method box == select, give an error. ---If checkbox is checked and Payment Method != select, echo out the ID and selection from the drop down box. Currently, I am echoing the correct ID(s) when certain boxes are checked, but I'm getting every drop down box selection when the corresponding box is not selected. Any help would be greatly appreciated. This is the HTML / PHP Form: $results .= '<table align="center" width="70%" border="0" bgcolor="'.$bg.'" cellpadding="2" cellspacing="0" class="resultsUnderline">'; $results .= '<tr>'; $results .= '<td width="30%" align="left">'.'<input type="checkbox" name="checkbox[]" value="'.$app_pmt_id.'" id="checkbox" />' .$full.'<FONT size="1" color="maroon"> ( '.$app_id.' )</FONT></td>'; $results .= '<td width="30%" align="center">'.$created.'</td>'; $results .= '<td width="20%" align="center"> <select name="pay_method[]" class="drop" id="pay_method"> <option value="select">-- Method --</option> <option value="check">Check</option> <option value="credit">Credit Card</option></td>'; $results .= '<td width="10%" align="center">'.$paid.'</td>'; $results .= '<td width="10%" align="right">'.$fee.'</td>'; $results .= '</tr>'; $results .= '</table>'; Here is the PHP getting the results: if(isset($_POST['action_box']) && $_POST['action_box'] == 'mark_paid') { $checked = array(); $checked2 = array(); if(!isset($_POST['checkbox'])) { $val_error[] = 'You have not selected any applications.'; } elseif($_POST['pay_method'] == 'select') { $val_error[] = 'Please select a Payment Method.'; } else { foreach($_POST['checkbox'] as $value) { $checked[] = $value; } foreach($_POST['pay_method'] as $value2) { $checked2[] = $value2; } echo implode(",", $checked); echo implode(",", $checked2); //$result = mysql_query("UPDATE app_pmt SET paid = '1' WHERE app_pmt_id IN(" . implode(",", $checked) . ")"); //header('Location: bal_due.php?id='.$_GET['id']); } } [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
coupe-r Posted October 3, 2010 Author Share Posted October 3, 2010 So now I know the problem is because i'm using 2 foreach loops. But I don't know how to fix it . Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 3, 2010 Share Posted October 3, 2010 if(isset($_POST['action_box']) && $_POST['action_box'] == 'mark_paid') { $checked = array(); $checked2 = array(); $err = false; if(!empty($_POST['checkbox'])) { $val_error[] = 'You have not selected any applications.'; } else { foreach($_POST['checkbox'] as $key => $value) { $checked[] = $value; if ($_POST['pay_method'][$key] != 'select') { $checked2[] = $_POST['pay_method'][$key]; } else { $err = true; $val_error[] = 'Payment method not updated for one or more selected application.'; break; } } echo implode(",", $checked); echo implode(",", $checked2); if (!$err) { //$result = mysql_query("UPDATE app_pmt SET paid = '1' WHERE app_pmt_id IN(" . implode(",", $checked) . ")"); //header('Location: bal_due.php?id='.$_GET['id']); } } } Quote Link to comment Share on other sites More sharing options...
coupe-r Posted October 3, 2010 Author Share Posted October 3, 2010 SWEET!! thank you for taking the time to do that. Last thing. How should my query be to catch each of the IDs payment method? Right now I have $result = mysql_query("UPDATE app_pmt SET method = '".."', paid = '1' WHERE app_pmt_id IN(" . implode(",", $checked) . ")"); What should go in method=''? Thanks again Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 3, 2010 Share Posted October 3, 2010 The only way to do so accurately is run multiple query's in a for loop as the key's will correspond for each selected value. Other than that the only other way is only allowing them to be updated in seperate groups of the same payment method. Quote Link to comment Share on other sites More sharing options...
coupe-r Posted October 3, 2010 Author Share Posted October 3, 2010 OK. Where should I put my UPDATE query so it loops through on the payment method? That seems simple enough. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 3, 2010 Share Posted October 3, 2010 Had an idea. This should work: if(isset($_POST['action_box']) && $_POST['action_box'] == 'mark_paid') { $check = array(); $credit = array(); $err = false; if( empty($_POST['checkbox']) ) { $val_error[] = 'You have not selected any applications.'; } else { foreach($_POST['checkbox'] as $key => $value) { if ($_POST['pay_method'][$key] == 'check') { $check[] = $value; } elseif ($_POST['pay_method'][$key] == 'credit') { $credit[] = $value; } else { $err = true; $val_error[] = 'Payment method not updated for one or more selected application.'; break; } } if (!$err) { mysql_query("UPDATE app_pmt SET method = 'check', paid = '1' WHERE app_pmt_id IN(" . implode(",", $check) . ")"); mysql_query("UPDATE app_pmt SET method = 'credit', paid = '1' WHERE app_pmt_id IN(" . implode(",", $credit) . ")"); header('Location: bal_due.php?id='.$_GET['id']); } } } P.S. I think cheque is spelled like that. Not sure tho. Quote Link to comment Share on other sites More sharing options...
coupe-r Posted October 3, 2010 Author Share Posted October 3, 2010 YOU ARE THE MAN!!!!!!!!!!! Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 3, 2010 Share Posted October 3, 2010 haha, glad I could help 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.