avo Posted May 10, 2006 Share Posted May 10, 2006 HI all Can someone be kind enough please to explane how a check box works in phpi 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 Quote Link to comment Share on other sites More sharing options...
micah1701 Posted May 10, 2006 Share Posted May 10, 2006 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! Quote Link to comment Share on other sites More sharing options...
avo Posted May 10, 2006 Author Share Posted May 10, 2006 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 falsebut 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 nameswhile ( $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 Quote Link to comment Share on other sites More sharing options...
ober Posted May 10, 2006 Share Posted May 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.