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 15 of 15
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problems with php processing of form

    I am having problems getting my php script to process all of the passed fields in my array. I have a form, with 8 check boxes, that have values assigned to them. This gets passed as an array. However, only the check boxes taht get checked are being processed by the php script. Here is the php script:

    PHP Code:
    <?php
    session_start
    ();
    include 
    'config.php';
    $userid $_SESSION['userid'];

    if(!empty(
    $_POST['check'])) {
    echo 
    'Check is NOT empty</br>';

        
    $query1 "SELECT `userid` FROM `interests` WHERE `userid`= '$userid '";
       
    $result mysql_query($query1);
       while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))
            {
            echo 
    'Now we found the user id..'.$row['userid'].'</br>';
            foreach(
    $_POST['check'] as $check) {
            echo 
    'Check = '.$check'</br>';            
                    
    $change="no";
                    echo 
    $check' is NULL..</br>';
                    
                    if(
    $check != "") {                    
                    
    $change="yes";
                    echo 
    $check' is NOT NULL..</br>';
                    }
                    
                
    $query "UPDATE interests SET $check='$change' WHERE userid='$userid'";
                
    mysql_query($query) or die(mysql_error(). '<br>'.$query);        
                echo 
    'We just did a query..'.$query.'</br>';    
            }
            }     
        }


    echo 
    'Your interests have been updated into the database, and those files will now</br>';
    echo 
    'begin showing up in the files area on your files sections.';
    ?>
    It is only processing the checked boxes. I have the echo statements in the code simply so I can see what is going on. Can anyone understand why it would only work with the checked boxes, and not the unchecked boxes?

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    That's the way that php handles checkboxes and radio buttons. So I end up using if(!isset statements for the unchecked boxes and else statements for the checked ones and have to give each one an individual name. And using select boxes wherever possible, since they always provide a value. If someone has a better way I would sure like to hear about it.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Which is normal.
    This is a part of the html standard. Checkboxes that are not checked are not considered successful and therefore should not be set to the server.
    The only way you can tell what hasn't been checked is if you have a list of what could be checked and cross reference it with what is checked. Thats simply taking an array of choice and using an array_diff to remove an array of checked. What's left over is not checked.
    You don't need multiple updates either. Give it each and every property in a single update by simply concat the strings together.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, so those informative reasons as to the fact that it isn't working, are not helping me to understand what i can do to get this to work. I already know that it is not working. I need help figuring out how I can do the actual code to make it work.

    Any help with that would be appreciated, as I have already spent well over a week straight pounding my head against the wall trying to figure this out.

  • #5
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    The way that I was talking about.
    PHP Code:
    <input type="checkbox" name="chk0" />
    <input type="checkbox" name="chk1" />
    <input type="checkbox" name="chk2" />
    <input type="hidden" name="myin" />
    <?php 
    if(isset($_POST['myin']))
    {
    if(!isset(
    $_POST['chk0'])) Do what you want to do if it's unchecked.;
    if(isset($_POST['
    chk0'])) Do what you do if it's checked;
    etc.
    }

    ?>
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    Quote Originally Posted by iceregent View Post
    Any help with that would be appreciated, as I have already spent well over a week straight pounding my head against the wall trying to figure this out.
    and you will continue with a sore head unless you actually read the answers

    a checkbox does not really exist unless it is checked so its impossible for it to tell you it exists.

    DrDos shows you what FOu-Lou was suggesting.

    another simple answer is to use a pair of yes/no radio buttons instead, this was you can get a different value for yes and no which your code can check
    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)

  • #7
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, so obviously this is a very hostile forum if that is the kind of response users get when they ask for help with actual coding. Your basic response of "do it yourself" is a very hostile response, and will lead me and other to leave this forum and seek help elsewhere. Thanks for nothing.

  • #8
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Quote Originally Posted by iceregent View Post
    Ok, so obviously this is a very hostile forum if that is the kind of response users get when they ask for help with actual coding. Your basic response of "do it yourself" is a very hostile response, and will lead me and other to leave this forum and seek help elsewhere. Thanks for nothing.
    You didn't even bother to show us your HTML, but it was easy to deduce that you lacked an understanding of how php handles certain form elements. You showed us some pretty fancy php, so we must have assumed that you had some understanding of it. Was that a mistake on our part ?
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #9
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,285
    Thanks
    4
    Thanked 201 Times in 198 Posts
    Quote Originally Posted by iceregent View Post
    Can anyone understand why it would only work with the checked boxes, and not the unchecked boxes?
    Nobody is being hostile at all. Your question was answered. Whether or not you like the answer does not change the fact that that is how PHP interacts with forms and it is not something you, nor anyone else, can change. An alternative has been offered.
    Dave .... HostMonster for all of your hosting needs

  • #10
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    @iceregent, We have given you the reasons why your code does not work. It's the "teach a man to fish" story. Do you want us to code for you? We could and sometimes do but most people learn by doing it themselves. What is being said is that check boxes that airnt checked are not passed. See for your self: in the php put this at the top:
    PHP Code:
     <?php
    session_start
    ();
    $love $_POST["Check"];
    print_r($love);
    die;
    So your foreach does not work. And the update query is being done too much. If all 8 boxes are checked you'll update 8 times.
    Evolution - The non-random survival of random variants.

  • #11
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here is the html portion:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title> Interests Form </title>
    <meta name="Generator" content="Alleycode HTML Editor">
    <meta name="Description" content="Your description here...">
    <meta name="Keywords" content="Your keywords here...">
    </head>
    
    <body>
    <form method="post" action="interests.php" name="interests">
    
    <table width="500" cellspacing="3" cellpadding="3" bgcolor="#FF80FF" bordercolor="#800000">
    <tr>
    	<td>One</td>
    	<td>Two</td>
    	<td>Three</td>
    	<td>Four</td>	
    </tr>
    <tr>
    	<td><input type="checkbox" name="check0"></td>
    	<td><input type="checkbox" name="check1"></td>
    	<td><input type="checkbox" name="check2"></td>
    	<td><input type="checkbox" name="check3"></td>	
    </tr>
    <tr>
    	<td>Five</td>
    	<td>Six</td>
    	<td>Seven</td>
    	<td>Eight</td>	
    </tr>
    <tr>
    	<td><input type="checkbox" name="check4"></td>
    	<td><input type="checkbox" name="check5"></td>
    	<td><input type="checkbox" name="check6"></td>
    	<td><input type="checkbox" name="check7"></td>
    </tr>
    <tr>
    	<td colspan="2"><input type="submit"></td>
    	<td colspan="2"><input type="reset"></td>
    </tr>
    </table>
    
    </form>
    </body>
    </html>
    And no, I do not have a complete grasp on how to do this. I have been trying and trying and trying. I am asking for help with the code. I am not a genius. I need to be able to figure out how to get all 8 responses parsed out into a set where I can then do an update. The database table was not quite the same, I just finished changing the table. I used to be a key id, with a user id, with and interest id, but now, it is a userid with 8 interest fields, so now i can do a single update, but originally i started out wanting to change only the fields that changed from the last time they used the form. Their interests can change from time to time, so I need to allow for them to refill the form and change even a single interest or all 8.

    So now, I have completely gutted and lost my php script because I received advice which i tried to follow, and now nothing is working at all. Nothing is being passed, and now my script is completely hosed and my brain is about fried trying to figure this out.

    So, I need to have the form with 8 fields, pass this to the processor, come up with the values for all 8 fields, and then update the data base with these values.

  • #12
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    Took some time to write an example for 4 check boxes:
    This is the HTML:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    </head>
    
    <body>
    <form action="test.php" method="POST">
    <input type="checkbox" name="check[]" value="box1"  />BOX 1
    <input type="checkbox" name="check[]" value="box2"  />BOX 2
    <input type="checkbox" name="check[]" value="box3"  />BOX 3
    <input type="checkbox" name="check[]" value="box4"  />BOX 4
    <input type="submit" />
    </form>
    </body>
    </html>
    And this is the PHP:
    PHP Code:
     <?php
    session_start
    ();
    $a 'no';
    $b 'no';
    $c 'no';
    $d 'no';
    include 
    'config.php';
    $userid $_SESSION['userid'];

    if(!empty(
    $_POST['check'])) {
    echo 
    'Check is NOT empty</br>';
    $query1 "SELECT `userid` FROM `interests` WHERE `userid`= '$userid '";
    $result mysql_query($query1);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC))
    {
        echo 
    'Now we found the user id..</br>';
        foreach(
    $_POST['check'] as $check) {
            switch (
    $check) {
                case 
    'box1':
                    
    $a "yes";
                    break;
                case 
    'box2':
                    
    $b "yes";
                    break;
                case 
    'box3':
                    
    $c "yes";
                    break;
                case 
    'box4':
                    
    $d "yes";
                    break;
            }
        }
    $query "UPDATE interests SET  box1 = '$a', box2 = '$b', box3 = '$c', box4 = '$d' where userid='$userid'";
    mysql_query($query) or die(mysql_error(). '<br>'.$query);
    echo 
    'We just did a query..'.$query.'</br>';
        }
    }
    echo 
    'Your interests have been updated into the database, and those files will now</br>';
    echo 
    'begin showing up in the files area on your files sections.';
    ?>
    It should work.

    Notice => name="check[]" the [] to make these boxes an array
    Evolution - The non-random survival of random variants.

  • #13
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you sunfighter I will try to understand and translate that to what I need it to do. I will let you know what successes I have. And no, i am not looking for anyone to write the code for me, however, I have been trying to write the code myself, and have had no success, so, yes, I obviously need the help, and i appreciate it. What I really want to do is understand it, also I can learn and do it myself. Once again, thank you and i will see if I can successfully implement that, and I will let you know how it goes.

  • #14
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you sunfighter. I was able to take what you gave me, study it, and write a workable set of code that now successfully allows for and updates the database successfully. I really appreciate your help on this matter. Now I can take some aspirin and get on with the rest of the functions i need to program in.. Once again, thank you very very much sunfighter.

  • #15
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    No problem. Have fun, we're here if you need help.
    Evolution - The non-random survival of random variants.


  •  

    Posting Permissions

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