3 Replies - 2746 Views - Last Post: 05 November 2012 - 12:00 PM Rate Topic: -----

#1 identifyrayz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-November 12

How to make Checkbox checked manually if retrieve data from mysql?

Posted 05 November 2012 - 11:33 AM

What i trying to ask is how to make the "checkbox auto checked" if the data is retrieve from mysql?
Sorry if i have bad grammar... :withstupid:

<?php		
session_start();
echo "<form id = 'form' action = '' method = 'get'>";
$db = 'database';
$cou = 'counseling';
$id = $_GET['studentid'];
			$conn = @mysqli_connect('localhost', 'root', '') or die ("Connection To $db Time Out");
			@mysqli_select_db($conn, $db) or die ("Cannot Find $sd");

			$sql = "SELECT * FROM $cou where StudentID = $id";
			$query = @mysqli_query($conn, $sql) or die ("Cannot Find $cou");
			$row = mysqli_fetch_array($query);
echo "<table>";
[b]echo "<tr>";
		echo "<td><input type='checkbox' name='data[]' value ='row[ReasonForReferral]' readonly>Depression</td>";
		echo "<td><input type='checkbox' name='data[]' value ='row[ReasonForReferral]' readonly>Perfectionist</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td><input type='checkbox' name='data[]' value ='row[ReasonForReferral]' readonly>Family Issues</td>";
		echo "<td><input type='checkbox' name='data[]' value ='row[ReasonForReferral]' readonly>Self-development</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td><input type='checkbox' name='data[]' value ='row[ReasonForReferral]' readonly>Fighting</td>";
		echo "<td><input type='checkbox' name='data[]' value ='row[ReasonForReferral]' readonly>Skipping Class</td>";
	echo "</tr>";[/b]



Is This A Good Question/Topic? 0
  • +

Replies To: How to make Checkbox checked manually if retrieve data from mysql?

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: How to make Checkbox checked manually if retrieve data from mysql?

Posted 05 November 2012 - 11:41 AM

Hello,

We can check a check box by simply adjusting the HTML your are outputting, below is a small example.

$isDataThere = true; // This should be decided by your database checks, this is just for demonstration.
	echo '<input type="checkbox" ' . ($isDataThere ? ' checked' : '') . '>';


See this documentation page on different methods of checking check boxes and for more information on the short-hand if statement check here.
Was This Post Helpful? 0
  • +
  • -

#3 identifyrayz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-November 12

Re: How to make Checkbox checked manually if retrieve data from mysql?

Posted 05 November 2012 - 11:56 AM

Hi there,
:unsure: Sorry but i don't get it :no: :(
i'm new to php... >.<
Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: How to make Checkbox checked manually if retrieve data from mysql?

Posted 05 November 2012 - 12:00 PM

What part don't you get exactly?

This is how we make a checkbox in HTML.
<input type="checkbox"/>

This is how we make a checked checkbox.
<input type="checkbox" checked/>

The shorthand if statement works like this: (statement ? outcomeIfTrue : outcomeIfFalse)

So, a quick example..

$test = true;
	echo ($test ? 'Test is true' : 'Test is false');

This post has been edited by Kruithne: 05 November 2012 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1