Jump to content

Checkbox Displaying Boolean Data From Database


skyler_sdf

Recommended Posts


//The ckeckbox type has only one possible value per input: off(unchecked) or on(checked). The database field which records this information is almost always going to be a samll integer or bit type with values 0 and 1 corresponding to unchecked or checked check boxes.

//The following example demonstrates how to use a check box to DISPLAY and CHANGE a BOOLEAN value.


<?php

//Open connection to the database

mysql_connection("localhost", "phpuser", "sesame") or die("Failure to communicate with database");

mysql_select_db("test");

//If the form has been submitted, record the preference and redisplay

if($_post['submit']=='Submit')

{$email=$_post['email'];

$as_email=addslashes($_post['email']);

if(isset($_post['optout'] && $_post['optout']==1)

{

$optout=1;

}

else{

$optout=0;

}

//Update value

$query="update checkbox set boxvalue=$optout where boxname='optout' and email='$as_email'";

$result=mysql_query($query);

if(mysql_error()==" ")

{

$succes_msg='<p>Your preference has been updated.</p>';

}

else{

error_log(mysql_error());

}

//Get the value

$query="select boxvalue from checkbox where boxname='optout' and email=='$as_email'";

$result=mysql_query($query);

$optout=mysql_result($result, 0, 0);

if($optout==0)

{

$checked=" ";

}

elseif($optout==1)

{

$checked='CHECKED';

}

}

//Now display the page

$thispage=$_server['php_self'];

//Have to do this for heredoc

$form_page=<<<EOFORMPAGE

<html>

<head>

<title></title>

</head>

$succes_msg

<form method="post" action="$thispage">

Email address:

<input type="text" name="email" size=25 value="$email">

<br>

<br>

<font size=+4>please send me lots of e-mail bulletins!</font>

<br>

<font size=-2>Opt out by clicking this tiny checkbox</font>

<input type="checkbox" name="OptOut" value=1 $checked>

<br>

<br>

<input type="submit" name="submit" value="Submit">

</form>

</body>

</html>

EOFORMPAGE;

echo $form_page;

?> 


Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • 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.