Jump to content

Using Jquery Dynamically To Check Checboxes From PHP LOOP .. help request


Failing_Solutions

Recommended Posts

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:

example_results_move_part_page.jpg

 

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.