SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: Checkbox Issue

  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checkbox Issue

    Hi,
    I have an edit review page to edit ride reviews that visitors submit to my site so they can edited by admins if needed. On the edit page i also have a checkbox and in the DB table i have a enum field with Yes and No values. The field is called authorised. All submitted reviews are set to No in the authorised field by default. I wish for the checkbox on the edit page to authorise and deauthorise the reviews when it is checked or unchecked. Another problem is that to get to the edit page for a particular review you first go to a page called reviews which lists all reviews and has a edit and delete link next to each of them. When the edit link is clicked you are taken to the edit page and even if the review is set to Yes in the authorised field in the DB table, the checkbox is either always checked or always unchecked?

    Can you check the following code to see where i am going wrong?

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>ATM Ride CMS: Edit Review</title> 
    <meta http-equiv="content-type" 
        content="text/html; charset=iso-8859-1" /> 
    </head> 
    <body> 
    <?php 

    // connect to mysql 
    $dbcnx mysql_connect("-----""-----""-----"
           or die(
    mysql_error()); 

    // select db 
    $db mysql_select_db("rctneil"
          or die(
    mysql_error()); 

    if (isset(
    $_POST['id'])){ 
      
    // The reviews's details have been updated. 


      
    if (isset($_REQUEST['authorised'])) { 
        
    $authorised "Yes"
    } else { 
        
    $authorised "No"


      
    $name $_POST['name']; 
      
    $reviewtext $_POST['reviewtext'];
      
    $authorised $_POST['authorised'];
         
      
    $id $_POST['id']; 
      
    $sql "UPDATE atm_visitreviews SET 
          name='
    $name', 
          reviewtext='
    $reviewtext',
          authorised='
    $authorised'
             
              WHERE id='
    $id'"
      if (@
    mysql_query($sql)) { 
        echo 
    '<p>Review updated.</p>'
      } else { 
        echo 
    '<p>Error updating review: ' 
            
    mysql_error() . '</p>'
      } 
    ?> 

    <p><a href="reviews.php">Return to reviews list</a></p> 
    <?php // end if isset POST ?> 

    <?php 
    // Allow the user to edit the review

    if(isset($_GET['id'])) { 
      
    $id $_GET['id']; 


      
    $review = @mysql_query
          
    "SELECT name, reviewtext, authorised FROM atm_visitreviews WHERE id='$id'"); 
      if (!
    $review) { 
        exit(
    '<p>Error fetching review details: ' 
            
    mysql_error() . '</p>'); 
      } 

      
    $review mysql_fetch_array($review); 

      
    $name $review['name']; 
      
    $reviewtext $review['reviewtext'];
      
    $authorised $review['authorised'];

      
    // Convert special characters for safe use 
      // as HTML attributes. 
      
    $name htmlspecialchars($name); 
      
    $reviewtext htmlspecialchars($reviewtext);
         
      
    $name nl2br($name); 
      
    $reviewtext nl2br($reviewtext);


    ?> 

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
      <p>Edit the ride:</p> 
      <label>Reviewers Name: 
      <input name="name" type="text" value="<?php echo $name?>" size="40" /> 
      </label><br /> 
      <p> 
        <label>Ride Review: </label> 
      </p> 
      <p> 
        <label> 
        <textarea name="reviewtext" cols="80" rows="20"><?php echo $reviewtext?></textarea> 
        </label> 
        <br /> 
      </p>
      <label>Authorised: 
      <input name="authorised" type="checkbox" id="authorised" checked="<?php echo $authorised?>" />
      </label>
      <br />
      <br /> 
    <input type="hidden" name="id" value="<?php echo $id?>" /> 
      <input type="submit" value="Submit Review Changes" /> 
      </p> 
    </form> 

    <?php ?> 

    </body> 
    </html>
    Thanks

    Neil

  2. #2
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone able to help?

    Thnks

    Neil

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The attribute for checking a checkbox is ...

    Code:
    <input type="checkbox" name="whatever" value="something" checked="checked" />
    Yes or No are not valid attribute values. What you need to do is check for the $authorised = Yes then echo checked="checked" otherwise echo nothing.

    PHP Code:
    <input type="checkbox" name="whatever" value="something"<?php echo ( $authorised == "Yes" ) ? ' checked="checked"' ''?> />
    Ian Anderson
    www.siteguru.co.uk

  4. #4
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have i written this correctly?

    PHP Code:
    <input name="authorised" type="checkbox" id="authorised" checked="<?php if ($authorised) == "Yes";
    echo 
    "checked";
    else
    echo 
    "";

     
    ?>" />
    Thanks

    Neil

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Neil,

    Why don't you post a simple php script that sets a variable equal to 10, and then uses an if statement to check if the value is 10. If the value is 10, display 'yes'. If the value isn't 10, display 'no'. Don't post it until you verify that it works correctly.

  6. #6
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What would that do? I would just end up with a page full of yes's

    Thanks

    Neil

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What would that do? I would just end up with a page full of yes's
    A page full of yes's? How so?

    What it will do is teach you the syntax of how to write an if statement. If you can't write that simple script, then you are dead in the water because you don't know enough php to be writing the script you posted.

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rctneil
    Have i written this correctly?

    PHP Code:
    <input name="authorised" type="checkbox" id="authorised" checked="<?php if ($authorised) == "Yes";
    echo 
    "checked";
    else
    echo 
    "";

     
    ?>" />
    Thanks

    Neil
    No. The if clause needs to echo or not the whole checked="checked" part, not just the value checked. My example showed it completely and is reiterated below using your own names/values ...

    PHP Code:
    <input name="authorised" type="checkbox" id="authorised" value="you need something here surely?"<?php echo ( $authorised == "Yes" ) ? ' checked="checked"' '' ?> />
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right,

    It is now checking and unchecking the box depedning on what is set in the DB table ie Yes or No but one the edit page, if i change the state of the checkbox and click "submit changes", the new value is not changed in the db?

    EDIT: Just found out that the value in the db table is just changing to be blank and not the words "Yes" or "No"?

    Thanks

    Neil

  10. #10
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry 7Stud, I'm just trying to get this site working and open as it has been down for weeks.

    Thanks

    Neil

  11. #11
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Posts
    1,295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <input name="authorised" type="checkbox" id="authorised" value="you need something here surely?"<?php echo ( $authorised == "Yes" ) ? ' checked="checked"' : '' ?> />
    As siteguru said, if you want the database to update to yes, then you need to have the attribute value="yes" in your input tag.
    Alex
    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  12. #12
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works great when i want to authorise reviews ie:check the boxes but when i edit a review and uncheck a review, i press the submit and get this error:

    Notice: Undefined index: authorised in c:\Sites\Single19\rctneil\webroot\future\editreview.php on line 35

    thanks

    Neil

  13. #13
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_POST['authorised'] will only exist if the form was submitted AND the checkbox was checked.
    You need to do:
    PHP Code:
     if(isset($_POST['authorised'])) $authorised 'yes';
     else 
    $authorised 'no'

  14. #14
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats another problem solved!

    Thanks

    Neil


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •