runnerjp Posted May 30, 2008 Share Posted May 30, 2008 how would this be correct <?php if (isset($_POST["edit"])) { $title = $_POST['title']; if(isset($_POST['forumlock'])){ $forumlock=1; } $query = "UPDATE forumtutorial_posts SET forumlock = '$forumlock', title = '$title' WHERE postid='$id' "; mysql_query($query) or die('Error, query failed'); echo "Your title is ". $title . "<br />";?> Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/ Share on other sites More sharing options...
obsidian Posted May 30, 2008 Share Posted May 30, 2008 We need a little more information to be able to help with that. First off, in the code you posted, there is no $id variable being set, so that may cause part of your problem. Can you describe your markup and what it is you are trying to do? You will probably be able to get a more thoughtful answer that way. Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553475 Share on other sites More sharing options...
blackcell Posted May 30, 2008 Share Posted May 30, 2008 Need a little bit of an explanation as to what you are trying to do. If your trying make something locked depending on whether the user has a checkbox checked or not, you need to test what you $_POST from that checkbox name. A good learning point would be create a checkbox (within a form) and on your php, simple echo $_POST['checkboxname'] within a submit test. Once you have established what you are testing for you have solved your problem. Â With checkboxes, I believe your testing for "on" if it is checked and nothing if it is not. Â Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553476 Share on other sites More sharing options...
runnerjp Posted May 30, 2008 Author Share Posted May 30, 2008 ok this is my whole form  <?php error_reporting(E_ALL); require_once '../settings.php'; checkLogin ('1'); $id=$_GET['id']; $query = "SELECT * FROM forumtutorial_posts where postid='$id'"; if ($result = mysql_query($query)){   if (mysql_num_rows($result)) {     $array = mysql_fetch_assoc($result);     $title = $array['title'];    ?> <?php if (isset($_POST["edit"])) { $title = $_POST['title']; if(isset($_POST['forumlock'])){ $forumlock=1; } $query = "UPDATE forumtutorial_posts SET forumlock = '$forumlock', title = '$title' WHERE postid='$id' "; mysql_query($query) or die('Error, query failed'); echo "Your title is ". $title . "<br />"; } else { ?> <h4>test</h4> <form action='<?php "$_SERVER[php_SELF]" ?>' method="post"> Title:  <input name="title" type="text" value="<?php echo $title;?>" />  <input type="checkbox" name="forumlock"> Lock a Room <input type="submit" name="edit" value="edit"/> </form> </body></html> <?php }}}?>  basicly all i want to do is if checkbox is checked then add the value of 1 into locked in my db Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553481 Share on other sites More sharing options...
blackcell Posted May 30, 2008 Share Posted May 30, 2008 I don't think your form action was quite right. It should be <? echo $_SERVER['PHP_SELF'] ?> instead of <?php "$_SERVER [php_SELF]" ?> Try placing an echo test inside of your isset $_POST[''] to make sure that code within the brackets is being executed. <?php error_reporting(E_ALL); require_once '../settings.php'; checkLogin ('1'); $id=$_GET['id']; $query = "SELECT * FROM forumtutorial_posts where postid='$id'"; if ($result = mysql_query($query)){   if (mysql_num_rows($result)) {     $array = mysql_fetch_assoc($result);     $title = $array['title']; ?> <?php if (isset($_POST["edit"])) { $title = $_POST['title']; if(isset($_POST['forumlock'])){ $forumlock=1; } $query = "UPDATE forumtutorial_posts SET forumlock = '$forumlock', title = '$title' WHERE postid='$id' "; mysql_query($query) or die('Error, query failed'); echo "Your title is ". $title . "<br />"; } else { ?> <h4>test</h4> <form name="input" action='<? echo $_SERVER['PHP_SELF'] ?>' method="post"> Title:  <input name="title" type="text" value="<?php echo $title;?>" />  <input type="checkbox" name="forumlock"> Lock a Room <input type="submit" name="edit" value="edit"/> </form> </body></html> <?php }}}?> Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553486 Share on other sites More sharing options...
runnerjp Posted May 30, 2008 Author Share Posted May 30, 2008 i think this is not right   if (isset($_POST["edit"])) { $title = $_POST['title']; if(isset($_POST['forumlock'])){ $forumlock=1; } Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553489 Share on other sites More sharing options...
blackcell Posted May 30, 2008 Share Posted May 30, 2008 <?php //Besides your bracketing being done is a manner that is so hard to read, I would try this. if (isset($_POST["edit"])) { Â Â $title = $_POST['title']; Â Â if($_POST['forumlock'] == "on") Â Â Â Â $forumlock = 1 Â Â }else{ Â Â Â Â $forumlock = 0; Â Â } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553510 Share on other sites More sharing options...
runnerjp Posted May 30, 2008 Author Share Posted May 30, 2008 for the code you provided i get unexpected } here }else{  Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553520 Share on other sites More sharing options...
blackcell Posted May 30, 2008 Share Posted May 30, 2008 No offense but have you ever had to track down a loose bracket before? Here it is and cleaned up. <?php error_reporting(E_ALL); require_once '../settings.php'; checkLogin ('1'); $id=$_GET['id']; $query = "SELECT * FROM forumtutorial_posts where postid='$id'"; if ($result = mysql_query($query)){   if (mysql_num_rows($result)) {     $array = mysql_fetch_assoc($result);     $title = $array['title'];     if (isset($_POST["edit"])) {       $title = $_POST['title'];       if($_POST['forumlock'] == "on")         $forumlock = 1       }else{         $forumlock = 0;       }     }   $query = "UPDATE forumtutorial_posts SET forumlock = '$forumlock', title = '$title' WHERE postid='$id' ";   mysql_query($query) or die('Error, query failed');   echo "Your title is ". $title . "<br />"; } else { echo " <h4>test</h4> <form name='input' action='".$_SERVER['PHP_SELF']."' method='post'> Title:  <input name='title' type='text' value='$title' />  <input type='checkbox' name='forumlock'> Lock a Room <input type='submit' name='edit' value='edit'/> </form>   "; } ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553552 Share on other sites More sharing options...
sasa Posted May 30, 2008 Share Posted May 30, 2008 you don't set up variable id in your form action url Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553573 Share on other sites More sharing options...
blackcell Posted May 30, 2008 Share Posted May 30, 2008 I just did sasa. May I ask why not? Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553582 Share on other sites More sharing options...
runnerjp Posted May 30, 2008 Author Share Posted May 30, 2008 even with this nice and tidy script  i still get unexpected } here   $forumlock = 1        }else{          $forumlock = 0; Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553595 Share on other sites More sharing options...
blackcell Posted May 30, 2008 Share Posted May 30, 2008 Forgot a semicolon <?php error_reporting(E_ALL); require_once '../settings.php'; checkLogin ('1'); $id=$_GET['id']; $query = "SELECT * FROM forumtutorial_posts where postid='$id'"; if ($result = mysql_query($query)){   if (mysql_num_rows($result)) {     $array = mysql_fetch_assoc($result);     $title = $array['title'];     if (isset($_POST["edit"])) {       $title = $_POST['title'];       if($_POST['forumlock'] == "on");         $forumlock = 1;       }else{         $forumlock = 0;       }     }   $query = "UPDATE forumtutorial_posts SET forumlock = '$forumlock', title = '$title' WHERE postid='$id' ";   mysql_query($query) or die('Error, query failed');   echo "Your title is ". $title . "<br />"; } else { echo " <h4>test</h4> <form name='input' action='".$_SERVER['PHP_SELF']."' method='post'> Title:  <input name='title' type='text' value='$title' />  <input type='checkbox' name='forumlock'> Lock a Room <input type='submit' name='edit' value='edit'/> </form>   "; } </body></html> ?> Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553609 Share on other sites More sharing options...
runnerjp Posted May 30, 2008 Author Share Posted May 30, 2008 nah that didnt work either as my query failed... but it should not fail as it shouldjust show  echo " <h4>test</h4> <form name='input' action='".$_SERVER['PHP_SELF']."' method='post'> Title:  <input name='title' type='text' value='$title' />  <input type='checkbox' name='forumlock'> Lock a Room <input type='submit' name='edit' value='edit'/> </form>   "; } as i didnt even submit any form Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553620 Share on other sites More sharing options...
BlueSkyIS Posted May 30, 2008 Share Posted May 30, 2008 you don't require that the form is submitted. all you require is: Â if ($result = mysql_query($query)){ Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553653 Share on other sites More sharing options...
runnerjp Posted May 30, 2008 Author Share Posted May 30, 2008 doesnt  if (isset($_POST["edit"])) { mean if form is submited do this Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553670 Share on other sites More sharing options...
blackcell Posted May 30, 2008 Share Posted May 30, 2008 Like we said at the beginning of this topic, what are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553684 Share on other sites More sharing options...
runnerjp Posted May 30, 2008 Author Share Posted May 30, 2008 ok i will talk you through it  <?php error_reporting(E_ALL); require_once '../settings.php'; checkLogin ('1'); $id=$_GET['id']; $query = "SELECT * FROM forumtutorial_posts where postid='$id'"; if ($result = mysql_query($query)){   if (mysql_num_rows($result)) {     $array = mysql_fetch_assoc($result);     $title = $array['title'];     if (isset($_POST["edit"])) {       $title = $_POST['title'];       if($_POST['forumlock'] == "on");         $forumlock = 1;       }else{         $forumlock = 0;       }     }   $query = "UPDATE forumtutorial_posts SET forumlock = '$forumlock', title = '$title' WHERE postid='$id' ";   mysql_query($query) or die('Error, query failed');   echo "Your title is ". $title . "<br />"; } else { echo " <h4>test</h4> <form name='input' action='".$_SERVER['PHP_SELF']."' method='post'> Title:  <input name='title' type='text' value='$title' />  <input type='checkbox' name='forumlock'> Lock a Room <input type='submit' name='edit' value='edit'/> </form>   "; } ?> </body></html>  so the code above should make it so i can edit a thread in a forum... so at the moment i have this  Title:  <input name='title' type='text' value='$title' /> <-- gets the current name of the forum thread via  $id=$_GET['id']; $query = "SELECT * FROM forumtutorial_posts where postid='$id'"; if ($result = mysql_query($query)){   if (mysql_num_rows($result)) {     $array = mysql_fetch_assoc($result);     $title = $array['title'];  so then i can chnage the name of the thread  and also my check box  <input type='checkbox' name='forumlock'> Lock a Room   so if i tick this check box it will chance forumlock in my database from 0 to 1 so then the thread is locked  just like on here!  so once its filled in i want to post it all into my db via  $query = "UPDATE forumtutorial_posts SET forumlock = '$forumlock', title = '$title' WHERE postid='$id' ";  with id being the thread id   Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553691 Share on other sites More sharing options...
runnerjp Posted May 30, 2008 Author Share Posted May 30, 2008 ok im missing a } sum where but i can seem to find it  PHP Code: <?php error_reporting(E_ALL); require_once '../settings.php';   checkLogin ('1'); $id=$_GET['id']; $query = "SELECT * FROM forumtutorial_posts where postid='$id'"; if ($result = mysql_query($query)){   if (mysql_num_rows($result)) {     $array = mysql_fetch_assoc($result);     $title = $array['title'];  $forumlock=0;// default value if it's not set if (isset($_POST['edit'])) { $title = $_POST['title']; } if(isset($_POST['forumlock'])){ $forumlock=1; } $query = "UPDATE forumtutorial_posts SET forumlock = '$forumlock', title = '$title' WHERE postid='$id' "; mysql_query($query) or die('Error, query failed'); echo "Your title is ". $title . "<br />"; } else { echo " <h4>test</h4> <form name='input' action='".$_SERVER['PHP_SELF']."' method='post'> Title:  <input name='title' type='text' value='$title' />  <input type='checkbox' name='forumlock'> Lock a Room <input type='submit' name='edit' value='edit'/> </form>   "; } ?> i thin its if ($result = mysql_query($query)){ but im not shore where to put it runnerjp is online now Add to runnerjp's Reputation Report Post Thanks Edit/Delete Message Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553789 Share on other sites More sharing options...
sasa Posted May 31, 2008 Share Posted May 31, 2008 you don't set up variable id in your form action url I just did sasa. May I ask why not? you have $i = $_GET['id']; in your script it's mean thet your URL looks like some_file.ph?id= blah but in your form tag you have <form name='input' action='".$_SERVER['PHP_SELF']."' method='post'> it's mean that url is sone_file.php miss ?id=blah part Quote Link to comment https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/#findComment-553968 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.