Failing_Solutions Posted September 5, 2012 Share Posted September 5, 2012 I'm trying to come up with a way to have input textfields automatically check a checkbox once you start typing in them. The issue is I'm getting the results from a php while loop and I need to somehow relate each set of input text fields to a specific checkbox This is how the results show up: This is how I coded the statement.. <?php ///QUERY FOR MATCHING PARTS if(isset($part_id) && strlen($part_id)>0){ $sql2="SELECT t.transaction_id, p.part_number, p.part_id, t.quantity, t.quality_id, r.received_date, r.received_id,r.ir_number, o.operator, p.part_description ,a.action, a.action_id, l.location, g.group_id, g.merge_id FROM transactions as t JOIN operators as o on o.operator_id = t.operator_id JOIN parts as p on p.part_id=t.part_id JOIN actions as a on a.action_id = t.action_id JOIN locations as l on l.location_id = t.location_id JOIN received as r on r.received_id = t.received_id JOIN groups as g ON g.part_id = r.part_id WHERE p.part_id='$part_id' AND a.action_id IN('1','3','5','7','8','10','12','14') AND l.consumption='Storage' AND t.quantity > 0 ORDER BY r.received_date ASC, t.quantity ASC"; $id_results2 = mysql_query($sql2) or die(mysql_error()); $count='0'; $count = mysql_num_rows($id_results2); $message_a.='<div><span class="switchRed">Found ' . $count . ' Part Location(s)</span><br> <a href="inventory.php?part='.urlencode($part).'"><br>View ' .$part . ' Transactions History</a></div>'; //Initialize a Counter $i='2'; //Create A Form For Results $mesage_b.='<form id="form2" action="move_part.php" method="post" onKeyPress="return event.keyCode!=13;" onSubmit="return validateForm2()">'; //Start To Loop Through Results while($form_data=mysql_fetch_array($id_results2)){ //Create Date For Formating $date= date_create($form_data['received_date']); //SET SPAN CLASS FOR LOST PARTS TO RED! if ($form_data['location']=="LOST"){ $spanOpen='<span class="switchRed">'; $spanClose='</span>'; }else{ $spanOpen='<span class="switchGreen">'; $spanClose='</span>'; } //Quality Checked if Not Then Display N/A Else Show The IR Number if ($form_data['ir_number'] ==0){ $quality='<span class="switchRed">N/A</span>'; }else{ $quality='<span class="switchGreen">'.$form_data['ir_number'].'</span>'; } $mesage_b.= '<table border="0" width="100%"> <th width="14%">To Location</th><th width=15%">Quantity</th><th width="15%">Received</th><th width="14%">Record ID</th> <th width="14%">Location</th><th width="14%">IR</th><th width="14%">Move</th> <br><br> <input type="hidden" name="partid" value="'.$form_data['part_id'].'"/> <tr><td><input id="newloc'.$i.'" onKeyUp="sugg'.$i.'(this.value);" size="6" type="text" name="newloc'.$i.'" style="text-transform: uppercase;"/><br> </td> <td><input id="quantity" size="8" type="text" name="quantity" style="text-transform: uppercase;" onfocus="doClear(this)" onKeyUp="fixQty(this.value);" /> <br>' . $form_data['quantity'] . '<span class="smalltxt"><b>available</b></span></td> <td>' . date_format($date, 'm/d/Y'). '</td><td>'. $form_data['received_id'] .'</td> <td>'.$spanOpen . $form_data['location'] . $spanClose.'</td><td>'.$quality. '</td> <td> <input type="checkbox" name="move_record" value="'.$form_data['received_id'].'"/></td> </tr> <tr><td colspan="4" style="text-align:left;"> <input type="submit" value="Submit" name="poo" class="button"/></td></tr> <div id="suggest'.$i.'"><div id="suggestions'.$i.'" class="suggestionsBox'.$i.'a" style="display: none;"> <img style="position: relative; top: -12px; left: 30px;" src="arrow.png" alt="upArrow" /> <div id="suggestionsList'.$i.'" class="suggestionList'.$i.'"></div> </div> </div> </table> <br> <hr> '; $i++; }//end while $mesage_b.='</form>'; }//end if ?> I just can't think of a way to dynamically relate the inputs to each individual checkbox. I could give them a class but then anything done in any checkbox would check them all, I can give them a unique name like the record_id (its unique), but then I have no way of knowing what records I'm showing to reference it with jquery. Any advice, very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/268038-using-jquery-dynamically-to-check-checboxes-from-php-loop-help-request/ Share on other sites More sharing options...
Failing_Solutions Posted September 6, 2012 Author Share Posted September 6, 2012 Had to sleep on it but finally figured it out. Solution, jquery function in the loop give all elements a counter I used $i The Function (Inside PHP LOOP) <script> function Change'.$i.'(){ $("#checkbox'.$i.'").prop("checked",true); } </script> I ended up with this: $mesage_b.= '<table border="0" width="100%"> <th width="14%">To Location</th><th width=15%">Quantity</th><th width="15%">Received</th><th width="14%">Record ID</th> <th width="14%">Location</th><th width="14%">IR</th><th width="14%">Move</th> <br><br> <input type="hidden" name="partid" value="'.$form_data['part_id'].'"/> <tr><td><input id="newloc'.$i.'" onKeyUp="sugg'.$i.'(this.value);" size="6" type="text" name="newloc'.$i.'" style="text-transform: uppercase;" onChange="Change'.$i.'();"/> <br> </td> <td><input id="quantity'.$i.'" size="8" type="text" name="quantity" style="text-transform: uppercase;" onChange="Change'.$i.'();" onKeyUp="fixQty(this.value);" /> <script> function Change'.$i.'(){ $("#checkbox'.$i.'").prop("checked",true); } </script> <br>' . $form_data['quantity'] . '<span class="smalltxt"><b>available</b></span></td> <td>' . date_format($date, 'm/d/Y'). '</td><td>'. $form_data['received_id'] .'</td> <td>'.$spanOpen . $form_data['location'] . $spanClose.'</td><td>'.$quality. '</td> <td> <input type="checkbox" id="checkbox'.$i.'" name="move_record" value="'.$form_data['received_id'].'"/></td> </tr> <tr><td colspan="4" style="text-align:left;"> <input type="submit" value="Submit" name="poo" class="button"/></td></tr> <div id="suggest'.$i.'"><div id="suggestions'.$i.'" class="suggestionsBox'.$i.'a" style="display: none;"> <img style="position: relative; top: -12px; left: 30px;" src="arrow.png" alt="upArrow" /> <div id="suggestionsList'.$i.'" class="suggestionList'.$i.'"></div> </div> </div> </table> <br> <hr> '; $i++; }//end while Works well enough. Quote Link to comment https://forums.phpfreaks.com/topic/268038-using-jquery-dynamically-to-check-checboxes-from-php-loop-help-request/#findComment-1375718 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.