doug007 Posted July 8, 2009 Share Posted July 8, 2009 Hi Guys, I have the below code that returns a checkbox as checked if it matches a certain condition. i have embaded the php into the html opening and closing tags like below, but whenever somthing is checked instead of making a checkbox just check it is aslo echoing this "checked> " next to the checkbox. does anyone please know what i am doing wrong? echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\""; $query2 ='SELECT tag_options_id, tag_id, tag_name, user_id FROM tags_options'; $result2 = mysql_query($query2); if (!$result){ echo mysql_error(); } while ($row2 = mysql_fetch_array($result2)) { if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]){ //global $checked; $checked = 'checked'; echo " $checked> </input>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/ Share on other sites More sharing options...
WolfRage Posted July 8, 2009 Share Posted July 8, 2009 You included that at the bottom of your script. If you do not want it then remove it, else if it should do something else then modify your echo statement appropriately. <?php $checked = 'checked'; echo " $checked> </input>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871037 Share on other sites More sharing options...
defeated Posted July 8, 2009 Share Posted July 8, 2009 Your while loop is returning two positive results (2 rows) Check your query. Alternatively (which is not ideal) you could get rid of your while loop to give: $row2 = mysql_fetch_array($result2); if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]){ //global $checked; $checked = "checked='checked'"; echo " $checked> </input>"; }// end if That's really not a good way of doing it though. What is good is the way I replaced $checked='checked'; with $checked="checked='checked'"; That makes it valid xhtml 1.0 Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871052 Share on other sites More sharing options...
doug007 Posted July 8, 2009 Author Share Posted July 8, 2009 You included that at the bottom of your script. If you do not want it then remove it, else if it should do something else then modify your echo statement appropriately. <?php $checked = 'checked'; echo " $checked> </input>"; ?> yes i need my while loop there, but what do you mean with modifying my echo statement appropriatley? Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871058 Share on other sites More sharing options...
WolfRage Posted July 8, 2009 Share Posted July 8, 2009 Well you made an echo statement if the output is incorrect change it. For instance the way deafeted modified what you set your varible to in order to produce valid xhtml. Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871069 Share on other sites More sharing options...
defeated Posted July 8, 2009 Share Posted July 8, 2009 WolfRage made a mistake. The echo statement is not the problem. The problem is that the while loop is causing your echo statement to execute twice. The code you end up with in your html is then... <input name="c_options[$i]" style="border-style: solid; border:0px solid; border-color: #FFFFFF" type="checkbox" border="0" value="tag_desc_id" checked></input> checked></input> check it out in your source. Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871070 Share on other sites More sharing options...
doug007 Posted July 8, 2009 Author Share Posted July 8, 2009 WolfRage made a mistake. The echo statement is not the problem. The problem is that the while loop is causing your echo statement to execute twice. The code you end up with in your html is then... <input name="c_options[$i]" style="border-style: solid; border:0px solid; border-color: #FFFFFF" type="checkbox" border="0" value="tag_desc_id" checked></input> checked></input> check it out in your source. this sounds possible, but how do i fix this? i need while loop to only loop through what is checked and if checked mark each checkbox as checked? cheers Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871071 Share on other sites More sharing options...
WolfRage Posted July 8, 2009 Share Posted July 8, 2009 Hopefully I can redeem my self in Defeated's Eyes. <?php $query2 ='SELECT tag_options_id, tag_id, tag_name, user_id FROM tags_options'; $result2 = mysql_query($query2); if (!$result){ echo mysql_error(); } while ($row2 = mysql_fetch_array($result2)) { if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]){ //global $checked; echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\""; $checked = "checked='checked'"; echo " $checked> </input>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871077 Share on other sites More sharing options...
doug007 Posted July 8, 2009 Author Share Posted July 8, 2009 Hopefully I can redeem my self in Defeated's Eyes. <?php $query2 ='SELECT tag_options_id, tag_id, tag_name, user_id FROM tags_options'; $result2 = mysql_query($query2); if (!$result){ echo mysql_error(); } while ($row2 = mysql_fetch_array($result2)) { if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]){ //global $checked; echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\""; $checked = "checked='checked'"; echo " $checked> </input>"; } } ?> thanks WolfRage, this works, but the type type=\"checkbox\" is no longer wokring? Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871098 Share on other sites More sharing options...
doug007 Posted July 8, 2009 Author Share Posted July 8, 2009 sorry i meant to say it only shows checked checkboxes now not unchecked ones? Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871103 Share on other sites More sharing options...
WolfRage Posted July 8, 2009 Share Posted July 8, 2009 The problem is you are doing more than what you are showing me in code. Until you present the rest of the code I am not a mind reader and can't guess what your code was doing before. Post all of the code and we will straighten it out. k. Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871113 Share on other sites More sharing options...
defeated Posted July 8, 2009 Share Posted July 8, 2009 Nice... but I suspect that you will end up with two inputs for each checked checkbox. I think that what you are trying to do would be better achieved with a WHERE statement in your mysql. The trouble will still be that you have two pieces of data that satisfy your if clause. $tag_desc_id = $row['tag_desc_id']; $user_info = $user_info[user_id]; $query2 ="SELECT tag_id FROM tags_options WHERE tag_id='$tag_desc_id' AND user_id='$user_info'"; $result2 = mysql_query($query2) or die(mysql_error()); while ($row2 = mysql_fetch_array($result2)) { if(!empty($row2['tag_id'])) { echo "<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$tag_desc_id."\" checked='checked' />"; }//end if else { echo "<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$tag_desc_id."\" />"; }//end else }//end while Ah, Sorry WolfRage. I stand corrected. This should work in that case. Scratch that. It won't work. Like WolfRage said... need more code. or just try adding else { //global $checked; echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\""; echo "> </input>"; }// end else after your if statement Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871114 Share on other sites More sharing options...
doug007 Posted July 8, 2009 Author Share Posted July 8, 2009 Nice... but I suspect that you will end up with two inputs for each checked checkbox. I think that what you are trying to do would be better achieved with a WHERE statement in your mysql. The trouble will still be that you have two pieces of data that satisfy your if clause. $tag_desc_id = $row['tag_desc_id']; $user_info = $user_info[user_id]; $query2 ="SELECT tag_id FROM tags_options WHERE tag_id='$tag_desc_id' AND user_id='$user_info'"; $result2 = mysql_query($query2) or die(mysql_error()); while ($row2 = mysql_fetch_array($result2)) { if(!empty($row2['tag_id'])) { echo "<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$tag_desc_id."\" checked='checked' />"; }//end if else { echo "<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$tag_desc_id."\" />"; }//end else }//end while Ah, Sorry WolfRage. I stand corrected. This should work in that case. Scratch that. It won't work. Like WolfRage said... need more code. or just try adding else { //global $checked; echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\""; echo "> </input>"; }// end else after your if statement same problem as above. here is my entire code, its little long, but really would appriciate if anyone cared to look at it. function do_checkboxes($userid){ global $profile_info, $setting, $smarty_object, $user_info;; if(isset($_POST['set_options'])){ if(!$_POST['c_options']==""){ foreach($_POST['c_options'] as $key=>$value) { $tag_name=mysql_real_escape_string($_POST['tag_name'][$key]); $id=mysql_real_escape_string($value); $delete = "DELETE FROM tags_options where user_id = '".$user_info[user_id]."' and tag_id <> '".$id."'"; $deleteSQL = mysql_query($delete) or die(mysql_error()); //echo $deleteSQL.'<br />'; } foreach($_POST['c_options'] as $key=>$value) { $tag_name=mysql_real_escape_string($_POST['tag_name'][$key]); $id=mysql_real_escape_string($value); $oaction = "INSERT INTO tags_options (tag_id, tag_name, user_id) VALUES('$id', '".$tag_name."', '".$user_info[user_id]."')"; // echo $oaction.'<br />'; $optionsSQL = mysql_query($oaction) or die(mysql_error()); } if($optionsSQL){ echo '<font color=red>Your options have been saved</font>'; } } } $query ="SELECT tag_desc_id, tag_name, parent_id FROM tags_description ORDER BY tag_name ASC"; $result = mysql_query($query); if (!$result){ echo mysql_error(); } elseif(mysql_num_rows($result)<=0){ //echo "No entries"; }else{ echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"10\">"; echo "<form method=\"post\">"; $i=0; while ($row = mysql_fetch_array($result)) { $i++; $incr = ($incr == 3) ? 1 : $incr + 1; if($incr == 1) echo "<tr>"; echo "<td class=\"formcheckbox2\">"; $query2 ='SELECT tag_options_id, tag_id, tag_name, user_id FROM tags_options'; $result2 = mysql_query($query2); if (!$result2){ echo mysql_error(); } echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\""; $checked = ''; while ($row2 = mysql_fetch_array($result2)) { if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]){ //global $checked; $checked = 'checked="checked"'; echo " $checked> </input>"; } } echo"<input type=\"hidden\" name=\"tag_name[$i]\" value=\"".$row['tag_name']."\"></input>"; echo $row['tag_name']; echo "</td>"; if($incr == 3) echo "</tr>"; } echo "<tr>"; echo "<td> <input type=\"hidden\" name=\"set_options\" value=\"set_options\"> <input name=\"GO\" type=\"submit\" id=\"GO\" value=\"Save Changes\" class=\"button\">"; echo"</td>"; echo "</tr>"; echo "</form>"; echo "</table>"; } }/* Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871139 Share on other sites More sharing options...
defeated Posted July 8, 2009 Share Posted July 8, 2009 Play suspended due to having to pick up my daughter from the creche. I'll have a look later tonight if nobody else solves it for you first. Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871181 Share on other sites More sharing options...
defeated Posted July 8, 2009 Share Posted July 8, 2009 aha! Try this. There seems to be an extra curly bracket at the end.... but perhaps it's not extra. All I have done is put the echo statement outside the if statement and structured the code a bit so that I could see what was going on. If it doesn't work I'll get to it later. function do_checkboxes($userid){ global $profile_info, $setting, $smarty_object, $user_info;; if(isset($_POST['set_options'])) { if(!$_POST['c_options']=="") { foreach($_POST['c_options'] as $key=>$value) { $tag_name=mysql_real_escape_string($_POST['tag_name'][$key]); $id=mysql_real_escape_string($value); $delete = "DELETE FROM tags_options where user_id = '".$user_info[user_id]."' and tag_id <> '".$id."'"; $deleteSQL = mysql_query($delete) or die(mysql_error()); //echo $deleteSQL.'<br />'; }//end foreach foreach($_POST['c_options'] as $key=>$value) { $tag_name=mysql_real_escape_string($_POST['tag_name'][$key]); $id=mysql_real_escape_string($value); $oaction = "INSERT INTO tags_options (tag_id, tag_name, user_id) VALUES('$id', '".$tag_name."', '".$user_info[user_id]."')"; echo $oaction.'<br />'; $optionsSQL = mysql_query($oaction) or die(mysql_error()); }//end foreach if($optionsSQL) { echo '<font color=red>Your options have been saved</font>'; }//end if }//end if no c_options }//end if set_options $query ="SELECT tag_desc_id, tag_name,parent_id FROM tags_description ORDER BY tag_name ASC"; $result = mysql_query($query); if (!$result) { echo mysql_error(); }//end if elseif(mysql_num_rows($result)<=0) { //echo "No entries"; }//end elseif else { echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"10\">"; echo "<form method=\"post\">"; $i=0; while ($row = mysql_fetch_array($result)) { $i++; $incr = ($incr == 3) ? 1 : $incr + 1; if($incr == 1) echo "<tr>"; echo "<td class=\"formcheckbox2\">"; $query2 ='SELECT tag_options_id, tag_id,tag_name,user_id FROM tags_options'; $result2 = mysql_query($query2); if (!$result2) { echo mysql_error(); }//end if echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\""; $checked = ''; while ($row2 = mysql_fetch_array($result2)) { if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]) { //global $checked; $checked = 'checked="checked"'; }//end if echo " $checked> </input>"; }//end while echo"<input type=\"hidden\" name=\"tag_name[$i]\" value=\"".$row['tag_name']."\"></input>"; echo $row['tag_name']; echo "</td>"; if($incr == 3) echo "</tr>"; }//end while echo "<tr>"; echo "<td> <input type=\"hidden\" name=\"set_options\" value=\"set_options\"> <input name=\"GO\" type=\"submit\" id=\"GO\" value=\"Save Changes\" class=\"button\">"; echo"</td>"; echo "</tr>"; echo "</form>"; echo "</table>"; }//end else // What's this curly bracket for? }/* Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871186 Share on other sites More sharing options...
doug007 Posted July 8, 2009 Author Share Posted July 8, 2009 Ok the problem with placing the echo statement outside the if statement now is that it echos this: > > > > > > > > > > > > > > > > > > > checked="checked"> checked="checked"> next to each checkbox that is checked, but does not check the actual checkbox. Quote Link to comment https://forums.phpfreaks.com/topic/165188-embaded-php-into-html-problem/#findComment-871209 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.