Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checkbox Checked

    I cannot seem to get the value for an if statement if a checkbox is checked,
    I have tried:
    PHP Code:
    if($_POST['allowbb'] == 'checked'
    and
    PHP Code:
    if($_POST['allowbb'])  //a "true" value 
    and
    PHP Code:
    if($_POST['allowbb'] == 'on'//someone suggested it in the forum 
    except none of them seem to work, I know this seems like a really easy thing to do, but for some reason I cant get it to work

    All help is apreciated

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label for="check">Check</label>
    <input type="checkbox" value="1" name="check" />
    </form>
    Then to recieve:

    PHP Code:
    if(isset($_POST['check'])){
    // do whatever

    If the checkbox is not checked the value will not be set.

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    as per missing-score's post though
    PHP Code:
    <?
    if(!empty($_POST['check'])){ }
    ?>
    would probably be better
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks It works great

    And I have a question, what does the <label> tag do?
    and also
    Is it possible to get the contents of a div with php?
    Last edited by stophon4; 05-06-2004 at 08:09 PM.

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The label tag associates some text with an element by the element's id. Typically this is used for radio buttons and checkboxes, so that clicking the label will also check the radio or checkbox..

    Here's an example of that:
    http://www.htmlcodetutorial.com/forms/_LABEL.html


    As for PHP getting the contents of a div... I'm not sure how you mean. Do you mean getting the contents of a div of the previous page (like a page gets submitted to PHP, so you're hoping to the view the contents of a div on that page)? or do you mean a div on the page you're currently constructing with PHP?

    In the first case, it's not possible unless there were form elements in that div so that you posted those form elements to the PHP page. The form has to know what's part of the form and what's part of the HTML, so it knows what stuff to post. If you're displaying a total or something in that div, you should put the total in a hidden form input with a unique name as well, so that it will get posted to PHP and you can grab its value by referencing its name.

    In the second case, if you store the contents of div in a string variable, then you can reference the contents through that string variable, and when it comes time for the div, you can just echo the string variable there.

    It's maybe a bit hard to explain, but if I haven't answered your question, maybe you'd like to clarify which case you're referring to (or if there's another case I haven't thought of..) and also explain a bit about why you'd like to get the content of the div through PHP, and maybe we can present a better solution.

    Hope that helps,
    Sadiq.

  • #6
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by missing-score

    Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label for="check">Check</label>
    <input type="checkbox" value="1" name="check" />
    </form>
    i would just like to add that label will not work in above code as it needs the id of input field to be set to check

    Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label for="check">Check
    <input type="checkbox" value="1" name="check" id="check" /></label>
    </form>

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    of course , my mistake

  • #8
    New Coder
    Join Date
    Apr 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb

    http://www.htmlcodetutorial.com/forms/_LABEL_FOR.html

    That is a better example of <label> in this case as label cannot work without the 'for="????"'
    ...not bad for a thirteen year old

  • #9
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am trying to make a user-freindly edit function for my guestbook, so I want to get the div of ONE line only, I want to refer to the line first, (so it searches the line for the div) and then I want it to record a div into a variable, and somehow I am going to need it to clear the line, write back in the other stuff, write in the improved div, and then write the rest of the line, I know who to do alot of this, but the recording into the div part I am clueless

  • #10
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by stophon4
    I am trying to make a user-freindly edit function for my guestbook, so I want to get the div of ONE line only, I want to refer to the line first, (so it searches the line for the div) and then I want it to record a div into a variable, and somehow I am going to need it to clear the line, write back in the other stuff, write in the improved div, and then write the rest of the line, I know who to do alot of this, but the recording into the div part I am clueless
    So does anybody know the answer? Or at least an idea on what to do?

  • #11
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i think people are unsure what your after could you explain more and may be some examples of what you need to grab

  • #12
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I got some of it done, but it doesn't work at all, but here are the parts of the source that due my edit function (that doesn't work):
    PHP Code:
    else if(isset($_POST['edit0']))
    {
        if(
    $authorized)
        {
            
    $post $_POST['edit0-post'];
            
    $Name $_POST['edit0-name'];
            echo (
    "<form action='gb.php' method='post'><b>Name:</b><INPUT type='text' Name='edit-name' Value='$Name'><b>POST:</b><INPUT type='text' Name='edit-post' Value='$post'><INPUT type='Submit' Name='edit-posted0'></form>");
        }
        else if(!
    $authorized)
        {
            echo (
    "<H1><b><font size='7' color'red'>You Do Not Have Permission To Access This Function</font></b></H1><br>");
        }

    That takes information from the edit function, also the Name and Post will be hidden values on the form made when the post is posted


    Now this does the actual updating:
    PHP Code:
    else if(isset($_POST['edit-posted0']))

        $post0 = '1';
        $lines = count(file("guestbook.php"));
        $array = file("guestbook.php"); 
        $Name = $_POST['edit-name'];
        $Post = $_POST['edit-post'];
        $file = fopen("guestbook.php", "w");
        fputs($file, '<table class="post" Name=$_POST["type"] border=0><tr><td>');
        fputs($file, $_POST['edit-name']);
        fputs($file,'</td><td align="right">');
        fputs($file, $time);
        fputs($file, '</td></tr></table><table class="post" Name=$_POST["type"] border=0><tr><td>'); 
        fputs($file, '<center>'); 
        fputs($file, $_POST['edit-post']); 
        fputs($file, '<br>');
        fputs($file, '<font color="CDCDCD" size=2>');
        fputs($file, $IP);
        fputs($file, '</font><hr>'); 
        fputs($file, "<?php ");
        fputs($file, '$IPP = getenv (REMOTE_ADDR);');
        fputs($file, "
    $admin 'authorized.dat';$IP getenv (REMOTE_ADDR);if (!file_exists ($admin)) { die('Error: \$admin does not exist.');}$h fopen ($admin'r');while (!feof ($h)) { $line fgets ($h4096); $line trim ($line); if ($line == $IP){ $authorized=true;}else{ $authroized=false; }}fclose($h);");
        fputs($file, "
    if($authorized){ ");
        fputs($file, "
    echo ('<center><form style=float:left action=gb.php method=post><input type=Submit Name=delete$lines Value=Delete></form></center>'); }"); 
        fputs($file, "
    ?>"); 
        fputs($file, '<br><hr></td></tr></table>');
        fputs($file, $newline);
        if($lines < '2') // an attempt to put every line after it as the array line
        {
            fputs($file, $array[1]);
        }
        else if($lines < '3') // puts post 2 after it, should use the "for" function
        {
            fputs($file, $array[2]);
        }
        fclose($file);
    }
    So that is all I got and it doesn't work at all

  • #13
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you are trying to give people the ability to edit there posts in your guestbook would it not be simpler to edit the file where posts are being stored

    it looks to me you are trying to edit the script itself

  • #14
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That would require the user to use html, anyway I made a improvement after spending about 20 minutes working on it the edit function now works fine, the only problem is getting the post back into the file without screwing up anything....

    I really cant seem to get this to work, I think I need a while loop, but I am not quite sure how to do it, this is my current code and in the edit tags is my idea on what to do:
    PHP Code:
    else if(isset($_POST['edit-posted0']))

        $_POST['comments'] = stripslashes($_POST['comments']);
    //    $posts = '1';
    //    $lines = count(file("guestbook.php"));
    //    $array = file("guestbook.php"); 
        $file = fopen("guestbook.php", "w");
        fputs($file, '<table class="post" Name=$_POST["type"] border=0><tr><td>');
        fputs($file, $_POST['edit-name']);
        fputs($file,'</td><td align="right">');
        fputs($file, $time);
        fputs($file, '</td></tr></table><table class="post" Name=$_POST["type"] border=0><tr><td>'); 
        fputs($file, '<center>'); 
        fputs($file, $_POST['edit-post']); 
        fputs($file, '<br>');
        fputs($file, '<font color="CDCDCD" size=2>');
        fputs($file, $IP);
        fputs($file, '</font><hr>'); 
        fputs($file, "<?php ");
        fputs($file, '$IPP = getenv (REMOTE_ADDR);');
        fputs($file, "
    $admin 'authorized.dat';$IP getenv (REMOTE_ADDR);if (!file_exists ($admin)) { die('Error: \$admin does not exist.');}$h fopen ($admin'r');while (!feof ($h)) { $line fgets ($h4096); $line trim ($line); if ($line == $IP){ $authorized=true;}else{ $authroized=false; }}fclose($h);");
        fputs($file, "
    if($authorized){ ");
        fputs($file, "
    echo ('<center><form style=float:left action=gb.php method=post><input type=Submit Name=delete$lines Value=Delete></form></center>'); }"); 
        fputs($file, "
    ?>"); 
        fputs($file, '<br><hr></td></tr></table>');
        fputs($file, $newline);
    /*
               while($posts < $lines)
               {
                      fputs($file, $array[$posts]);
               }
    */
        fclose($file);
    }
    except that only solves it if you want the edited post on the top, I am very confused So I need some help

  • #15
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i have no idea also im guessing you want to just edit a section of guestbook.php

    fopen(file,'w') will open the file for writing so will truncate file (deleete all that is ready there)

    so may be you need to read the file first and then preg_replace new data over exsisting section


    as to editing storage file your users would not need to use html if you loaded there post into text fileds they could then edit and save again

    also maybe look at getting some database as this would make life alot simpler


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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