Jump to content


Check box in php

  • Please log in to reply
3 replies to this topic

#1 avo

  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 10 May 2006 - 05:49 PM

HI all

Can someone be kind enough please to explane how a check box works in php

i assumed it was like a button once checked it executed the code within

this is my short bit of code im testing with once i have sorted it i will use it to output html from within a if statement

    <form id="form2" name="form2" method="post" action="<?php echo $PHP_SELF;?>">
        <div align="center">
          <input type="checkbox" name="name_checked" id="id_checked" value="val_checked" />
          View Message As Text       </div>

if($_POST['name_checked'] == "val_checked"){
$name_checked =$_POST['name_checked'];
    echo "check box posted";
    echo "check box not posted";

but i expected this code to work like a button ummmmmmm. it dont.

thanks in advance
Im loving it ........

#2 micah1701

  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 10 May 2006 - 05:55 PM

I'm not sure I follow your problem?

a "checkbox" is an HTML element, not PHP.

a checkbox can be checked, or unchecked. if it is checked, when the form is submitted, the "value" is passed in $_POST with the element's "name". Your above code looks fine.

a "checkbox" however, can not be a "BUTTON"

unless you add a some sort of "onClick" javaScript to the checkbox, it can not behave like a button.

If this doesn't help, perhaps you could explain your problem w/ more detail.

"Confidence in the face of risk."

#3 avo

  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 10 May 2006 - 06:20 PM

HI thanks for your reply

if i press my GET_MESSAGE button i still can not get it to echo the correct message i have put the php code bellow in side a [/code]if ($_POST['get_message'])

meening if i press the get message  button and the box is checked 

it should be true 
if not checked it should be false

but its not working like this  
it will output check box not posted regardless if its checked or not 

part of my code is 

[code]if ($_POST ['get_message']) {

if($_POST['checkbox'] == "checkbox"){
    echo "check box posted";
    echo "check box not posted";
$query_all = "SELECT * FROM member_messages WHERE id='".$_POST ['list_box']."'"; 
$result_all = mysql_query ($query_all) or die ( mysql_error () );

//assign variable to field names
while ( $row = mysql_fetch_assoc($result_all)) {
$frm_username = "{$row['username']}";
$frm_name = "{$row['name']}";
$frm_email = "{$row['email']}";
$frm_date = "{$row['date']}";
$frm_title = "{$row['title']}";
$frm_message = "{$row['admin_message']}";
$ssfrm_message = stripslashes ($frm_message);


<form id="form2" name="form2" method="post" action="<? echo $PHP_SELF ?>">
      <input type="checkbox" name="checkbox" value="checkbox" />
     Output Message As Text 
all help appriciate
Im loving it ........

#4 ober

  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 May 2006 - 06:24 PM

You missed Micah's point. Nowhere in what you've posted does it show a submit button in the form. You MUST have a submit button or an "onclick" event in the checkbox that calls a javascript submit event.

Info: PHP Manual

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users