Jump to content

Check box in php


avo

Recommended Posts

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

[code]    <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>

    </form>
[/code]
[code]
if($_POST['name_checked'] == "val_checked"){
$name_checked =$_POST['name_checked'];
    echo "check box posted";
    }else{
    echo "check box not posted";
    }[/code]

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

thanks in advance
Link to comment
Share on other sites

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.
Thanks!
Link to comment
Share on other sites

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'])[code]

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";
    }else{
    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);


}}[/code]

and
[code]<form id="form2" name="form2" method="post" action="<? echo $PHP_SELF ?>">
      <input type="checkbox" name="checkbox" value="checkbox" />
     Output Message As Text
    </form>[/code]
all help appriciate
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.