Jump to content

Recommended Posts

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 />";?> 

Link to comment
https://forums.phpfreaks.com/topic/107988-php-and-the-dreaded-checkbox/
Share on other sites

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.

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.

 

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

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 }}}?>

<?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;

    }

}
?>

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>

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>
?>

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

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

 

:)

 

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

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

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.