php and checkboxes

Discussion in 'PHP' started by gilgalbiblewheel, Nov 22, 2007.

  1. #1
    How does PHP pick up from check boxes in previous pages?
    if ($book_spoke="true")?
    	if ($book_spoke="true")//$_REQUEST["Book_Spoke"]
    		{
    			if ($i > 0)
    				{
    					$sql.=  " AND ";
    				}
    			
    		//goes through the 3 cycles of spokes	
    		$sql.= " (";
    		for ($re=0; $re<=2; $re++)
    			{
    				$sql.= "book = ".($spoke + 22*$re);
    				if ($re <2)
    					{
    						$sql.=  " OR ";
    					}			
    			}
    		$sql.= " )";	
    		  $i++;
    		}
    PHP:

     
    gilgalbiblewheel, Nov 22, 2007 IP
  2. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #2
    if ($book_spoke="true")//$_REQUEST["Book_Spoke"]
    PHP:
    You are using assignment operator = in if statement that is not correct.

    Instead of checking ="true" use following statement.

    if isset($book_spoke){ // your code....
    PHP:
    Now if visitor will check the checkbox than your if statement will execute. And if checkbox is not checked than you will not receive any value of checkbox on the submitted page
     
    greatlogix, Nov 22, 2007 IP
  3. SEOWOES

    SEOWOES Peon

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    f ($book_spoke=="true")//$_REQUEST["Book_Spoke"] 
    PHP:
     
    SEOWOES, Nov 23, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Use this:
    if ($book_spoke == true)
    PHP:
    greatlogix, $book_scope is most likely true or false. Using isset() will still match either of these values.

    Above poster's code is also wrong as it is matching against the string "true", not the boolean "true", and has a typo.
     
    krt, Nov 23, 2007 IP
  5. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #5
    hmm, i might be missing something, but i dont see any $_POST or $_GET from the posts above(assuming magic_vars is off which is the default and safer way)

    getting the value of a checkbox is the same as getting the value from any other field, except for an upload field, use the code below

    $checkboxValue = $_POST['checkbox_field_name'];

    or

    $checkboxValue = $_GET['checkbox_field_name'];
     
    serialCoder, Nov 23, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    His code says:
    	if ($book_spoke="true")//$_REQUEST["Book_Spoke"] 
    PHP:
    So I'm assuming $book_spoke comes from the $_REQUEST variable.


    Using isset() is correct in this case though, as we're working with checkboxes. If the box is not checked, it won't be submitted with the form. This means $book_spoke would be null. And isset() returns false on null.

    Plus, comparing a value to a boolean value like this:
    
    if ($book_spoke == true)
    
    PHP:
    ... Is unnecessary, as it will cast the variable automatically to boolean if you use just:
    
    if ($book_spoke)
    
    PHP:

    EDIT:

    To avoid E_NOTICE errors for undefined offsets or variables, I would suggest doing just:
    
    if (isset($_REQUEST["Book_Spoke"]))
    {
        // Do whatever
    }
    
    PHP:
     
    nico_swd, Nov 23, 2007 IP
  7. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #7
    hehe, thanks for pointing that out nico_swd, that's why i put "i might have missed something", i really dont use $_REQUEST to get my vars because that's almost the same as having magic vars turned on :D
     
    serialCoder, Nov 23, 2007 IP