CcXD Posted September 5, 2008 Share Posted September 5, 2008 Hi everybody. I come with a question that has been bothering me for a while, and i cant find a way around it. This is what happens. I have 'n' ammount of input text fields, each on its own row. Each text field is accompanied by a checkbox. The boxes and the fields are created Dinamycally by the following code: $result=mysql_query($query); $numrows=mysql_numrows($result); for($i=0; $i<$numrows; $i++){ echo"<tr>" echo "<td><input type=\"text\" name=\"qty[]\" size=\"5\" maxlength=\"5\"></td>"; echo "<td><input type=\"checkbox\" name=\"selected_pn_id[]\" value=\"$pn_id\"></td>"; echo "</tr>"; } I want to be able to disable the field that is located next to the specific checkbox, and erase whatever was inside this field. The thing is, im not sure how to identify the fields or the boxes individuallly i tried something like : <?php //some code echo "<td><input type=\"checkbox\" name=\"selected_pn_id[]\" value=\"$pn_id\" onclick=\"disableField();\"></td>"; //rest of code ?> <script type="text/javascript"> function disableField(){ if(document.AddPCBParts.selected_pn_id.checked == true) { document.AddPCBParts.qty.disabled = false ; } else { document.AddPCBParts.qty.disabled = true ; } } </script> without any lluck. Any help? Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 5, 2008 Share Posted September 5, 2008 Give them an ID attribute in your for loop ( using $i probably ) then use the javascript function document.getElementById( 'id' ) Quote Link to comment Share on other sites More sharing options...
lemmin Posted September 5, 2008 Share Posted September 5, 2008 Going off of that code, your checkbox could reference its text input like this: $result=mysql_query($query); $numrows=mysql_numrows($result); for($i=0; $i<$numrows; $i++){ echo"<tr>" echo "<td><input type=\"text\" name=\"qty[]\" size=\"5\" maxlength=\"5\"></td>"; echo "<td><input type=\"checkbox\" name=\"selected_pn_id[]\" value=\"$pn_id\" onclick=\"this.parentNode.previousSibling.firstChild.disabled=true\"></td>"; echo "</tr>"; } This is very specific to how your table is set up, though, so if you changed what items were in the cells, you might have to change the code, too. Quote Link to comment Share on other sites More sharing options...
CcXD Posted September 5, 2008 Author Share Posted September 5, 2008 YAAAY it works! ;D the only thing now is that i cant turn it back disabled when i unclick it ??? Quote Link to comment Share on other sites More sharing options...
CcXD Posted September 5, 2008 Author Share Posted September 5, 2008 so what should i do if i want to write something like: <?php //some code onclick="ClickBox()" //rest of code ?> <script> function ClickBox(){ if(this.parentNode.previousSibling.firstChild.disabled==false){ this.parentNode.previousSibling.firstChild.disabled=true; } else{ this.parentNode.previousSibling.firstChild.disabled=false; } } </script> this is not working =( Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 5, 2008 Share Posted September 5, 2008 Won't work like that. PHP has nothing to do with javascript. You can't call JS in PHP and you can't call PHP in JS PHP = Server-side JS = Client-side Quote Link to comment Share on other sites More sharing options...
CcXD Posted September 5, 2008 Author Share Posted September 5, 2008 awww... well i tried what u mentioned, and its not working.... i got the following code <?php //some code echo "<td><input type=\"text\" name=\"qty[]\" id=\"$i\" size=\"5\" maxlength=\"5\" DISABLED></td>"; echo "<td><input type=\"checkbox\" name=\"selected_pn_id[]\" value=\"$pn_id\" id=\"$pn_id\" onclick=\"enableField($i,$pn_id);\"></td>"; //rest of code ?> <script type="text/javascript"> function enableField(id,pn_id){ var qty = document.getElementById( 'id' ); var chk = document.getElementById( 'pn_id' ); chk.checked ? qty.disabled = false : qty.disabled = true; //enable if chk is checked, disabled if chk is unchecked } </script> and its not working... ??? ??? Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 5, 2008 Share Posted September 5, 2008 Use IDs! Much easier IMO <script type="text/javascript"> function toggle ( i ) { var txt = document.getElementById( 'txt_'+i ); var chk = document.getElementById( 'chk_'+i ); if ( txt != null && chk != null ) { if ( chk.checked == true ) { txt.value = ''; txt.disabled = true; } else { txt.disabled = false; } } } </script> <?php for( $i=0; $i<10; $i++ ) { echo '<input type="text" name="qty[]" value="someval" id="txt_'.$i.'" /> '; echo '<input type="checkbox" name="selected_pn_id[]" value="xx" id="chk_'.$i.'" onclick="toggle('.$i.')" /><br /><br />'; } ?> Quote Link to comment Share on other sites More sharing options...
CcXD Posted September 5, 2008 Author Share Posted September 5, 2008 AWESOME!!! man i ussually dont like using Java script, but its becoming complicated to track 2k+ elements of an array Thanks again for the elp =) Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 5, 2008 Share Posted September 5, 2008 I dunno why so many people have the hate-on for javascript. It's a great way to make sites more interactive. Personally, I don't care if the rare corporate/public computer user can't view it properly. They should probably be working anyways Quote Link to comment Share on other sites More sharing options...
lemmin Posted September 5, 2008 Share Posted September 5, 2008 To enable it again, just include this javascript in the header: <script type="text/javascript"> function doDisable(obj) { if (obj.checked) obj.parentNode.previousSibling.firstChild.disabled=true; else obj.parentNode.previousSibling.firstChild.disabled=false; } </script> Then call that function for the onclick event: $result=mysql_query($query); $numrows=mysql_numrows($result); for($i=0; $i<$numrows; $i++){ echo"<tr>" echo "<td><input type=\"text\" name=\"qty[]\" size=\"5\" maxlength=\"5\"></td>"; echo "<td><input type=\"checkbox\" name=\"selected_pn_id[]\" value=\"$pn_id\" onclick=\"doDisable(this)\"></td>"; echo "</tr>"; } Quote Link to comment Share on other sites More sharing options...
kratsg Posted September 5, 2008 Share Posted September 5, 2008 I dunno why so many people have the hate-on for javascript. It's a great way to make sites more interactive. Personally, I don't care if the rare corporate/public computer user can't view it properly. They should probably be working anyways I agree. Honestly, if you're not an expert in JavaScript, you should still be able to create codes and look up functions and easily integrate them. Same thing in PHP, you should be able to think of the logic behind PHP, and look up functions, and put it all together. You don't need to be an expert to create good codes, just well-thought out logic. 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.