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.
Results 1 to 6 of 6
  1. #1
    piz
    piz is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Barcelona
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checking post variables from checkboxes

    Hi,

    I have a script that saves atomatically all submitted POST-Variables in a MySQL Table, if they match with the columns, updating a row.

    Now I have following Problem. One of the Variables is submitted with a checkbox in the previous HTML Form. If the checkbox is checked, there is no problem. The value of the checkbox is submitted. But if the checkbox isn't checked, the Variable isn't submitted. It even doesn't respond on isset().

    Is there any possibility to assign a value to an unchecked checkbox?
    Or to check the empty POST-Variables?
    ...without checking the variable by his name, because I am using this script for several forms.

    Thx very much.

    Saludo
    piz

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, you can't assign a variable or anything to an unchecked check box, it merely doesn't exist in your server side script. Why don't you wana put a name for that?? You can call it by a name, like "whatever" and then check it wherever you want:

    PHP Code:
    if ( !empty($_POST["whatever"]) )
        
    // do something 
    But I think I know your problem! You are having your checkbox in the first page whose action is the 2nd page, when this checkbox is submitted, you can see it in the 2nd page, but when the 2nd page is itself submitted, you see nothing, right?!! Well, you have several solutions for that:

    1- when the 2nd page is loaded for the first time, you can easily check if the checkbox is checked or not, then you can write it into a hidden field that you recieve it whenever the 2nd page is submitted.

    2- this one is better and more systematic I think! I always use $_SERVER["PHP_SELF"] for the action of my forms. Then I check the validity of info in the same page and only if the data is valid, I transfer the user to the 2nd page. What do I do with my first page data?? Well, you can either $_SESSION them, or put them in a query string and send it to your 2nd page. Then in your 2nd page to get your data back whenever that page is submitted, you have to write an action like this:
    PHP Code:
    <form action="<?=$_SERVER["PHP_SELF"] . "?" $_SERVER["QUERY_STRING"]?>">
    In case you use sessions, be careful that you empty the session when your job is finished with your forms/database, because the 2nd time that you come to the same form, you see that the data is still there!

    For the 2nd solution you can make multi-paged registration forms, in which you don't get all the info in only one page.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Another (perhaps dodgy) workaround would be to set a hidden input prior to every checkbox and set a default value in it

    <input type="hidden" name="checkbox1" value="off" />
    <input type="checkbox" name="checkbox1" value="proper value" />

    then the receiving page would set $_POST['checkbox1'] to 'off' and only reassign that to 'proper value' if the checkbox was sent.

    *** untested -
    also note: arrays of checkboxes (or radio buttons) would be trickier to manage.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe i misunderstood, but why not simply use !isset()? (that's what i always do --> unchecked checkboxes aren't posted so they aren't known in the $_POST collection)
    If it isn't set (so if it is unchecked) then you set some variable to a defaultvalue. If it is set, you set the variable to the chkbox value.

    You then nuse that variable inside the insertstatement

    PHP Code:
     if (!isset($_POST['var']))) {
             
    $var '0';
     } else {
             
    $var $_POST['var'];
     } 
    If you want it to be completely generic, then you need to include a hidden formfield with a (pipe)delimited list of the names of all checkboxes as a value

    like
    value="var2|var3|var4"

    Then you can use explode() to split the value up and get all checkboxes names. With a foreach-loop, you can then check if the checkbox isset

    PHP Code:
    $chk explode("|"$_POST['checkboxes']);

    foreach (
    $chk as $var) {
        if (!isset(
    $_POST[$var])) {
             
    $var_sql '0';
         } else {
             
    $var_sql $_POST[$var];
         }
        
    // append $var and $var_sql to the field and valuelist of the sql_statament


  • #5
    piz
    piz is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Barcelona
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts
    @ConfusedOfLife: No, that isn't my problem. My problem is on the page, where the variables are postet to the first time. This is a page where I redirect several forms to. So I can't name the variable, because I am using different tables, too, without check which table I am using - and that would be a lot of script more.

    @raf: see below... ;-)

    @Ökii: Yes, I got this idea, too - I'll try it. Hope it works.

    Thx to all.

  • #6
    piz
    piz is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Barcelona
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes - it works!
    Great.
    Thx.


  •  

    Posting Permissions

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