This is my code :
<td>
<?php
if ($adPropertyPayment == "Direct") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
if ($adPropertyPayment == "CPC") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
if ($adPropertyPayment == "CPM") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
?>
<input type="radio" id="radioPaymentDirect" name="payment" value="Direct" <?php echo $checked ?> onclick="showAmount('Direct');" />Direct
<input type="radio" id="radioPaymentCPC" name="payment" value="CPC" <?php echo $checked ?> onclick="showAmount('CPC');" />CPC
<input type="radio" id="radioPaymentCPM" name="payment" value="CPM" <?php echo $checked ?> onclick="showAmount('CPM');" />CPM
</td>
Checked is not working. I am getting the value of $aspropertypayment in POST.
Use this
<?php
$adPropertyPayment = $_POST['payment'];
if ($adPropertyPayment == "Direct") {
$checkedDir = "checked = 'checked'";
} else {
$checkedDir = "";
}
if ($adPropertyPayment == "CPC") {
$checkedCpc = "checked = 'checked'";
} else {
$checkedCpc = "";
}
if ($adPropertyPayment == "CPM") {
$checkedCpm = "checked = 'checked'";
} else {
$checkedCpm = "";
}
?>
<input type="radio" id="radioPaymentDirect" name="payment" value="Direct" <?php echo $checkedDir;?> onclick="showAmount('Direct');" />Direct
<input type="radio" id="radioPaymentCPC" name="payment" value="CPC" <?php echo $checkedCpc;?> onclick="showAmount('CPC');" />CPC
<input type="radio" id="radioPaymentCPM" name="payment" value="CPM" <?php echo $checkedCpm;?> onclick="showAmount('CPM');" />CPM
Try writting only $checked = 'checked'; in your if statement.
Related
I have code:
<?php
if ($user['x'] == 1) { $x_checked = ' checked'; } else { $x_checked = ''; }
if ($user['y'] == 1) { $y_checked = ' checked'; } else { $y_checked = ''; }
if ($user['a'] == 1) { $a_checked = ' checked'; } else { $a_checked = ''; }
if ($user['b'] == 1) { $b_checked = ' checked'; } else { $b_checked = ''; }
if ($user['c'] == 1) { $c_checked = ' checked'; } else { $c_checked = ''; }
[...]
?>
<input name="a" type="checkbox"<?php echo $a_checked; ?> />
<input name="b" type="checkbox"<?php echo $b_checked; ?> />
<input name="c" type="checkbox"<?php echo $c_checked; ?> />
[...]
and i have too long code (others same lines). How shortcode to this?
Just check in the input HTML:
<input name="a" type="checkbox" <?php echo ($user['a'] == 1) ? 'checked' : '' ?> />
<input name="b" type="checkbox" <?php echo ($user['b'] == 1) ? 'checked' : '' ?> />
<input name="c" type="checkbox" <?php echo ($user['c'] == 1) ? 'checked' : '' ?> />
If the values can only be 0 or 1 (or maybe more than 1 if you want that checked) then it is shorter:
<?php echo $user['c'] ? 'checked' : '' ?>
If you're going to have a $user element for each checkbox then loop it:
<?php foreach($user as $key => $val) { ?>
<input name="<?php echo $key ?>" type="checkbox" <?php echo $val ? 'checked' : '' ?> />
<?php } ?>
From your comment it appears you may be echoing, if so then just:
foreach($user as $key => $val) {
$checked = $val ? 'checked' : '';
echo '<input name="'.$key.'" type="checkbox" '.$checked.'/>';
}
Welcome to Stackoverflow!
Foreach loops and arrays are in this case your best friends, this is how I usually do it.
<?php
$input_name = array('a', 'b', 'c', 'd');
input_data = '';
foreach ($input_name as $value) {
if ($user[$value] == 1) {
$input_data .= '<input name="'.$value.'" type="checkbox" checked>';
} else {
$input_data .= '<input name="'.$value.'" type="checkbox">';
}
}
?>
Echo the results in the HTML part:
<?=$input_data?>
<?php
$fields = [
'a',
'b',
'etc'
];
foreach ($fields as $field){
if($user[$field] == 1){
$checked = 'checked';
}else{
$checked = '';
}
print('<input name="'.$field.'" type="checkbox" '.$checked.' />');
}
?>
I need help with enableing/disableing parts of my website.
<p>Disabled Sections:</p>
<form id="e-d-check" method="post">
<input type="checkbox" name="SRS" onchange="document.getElementById('e-d-check').submit()">SkiRegionSimulator 2012</input>
<input type="checkbox" name="Bilder" onchange="document.getElementById('e-d-check').submit()">Bilder</input>
<input type="checkbox" name="KWS" onchange="document.getElementById('e-d-check').submit()">Klimawandel-Stunde</input>
</form>
<table>
<tr>
<th>Name</th>
<th>Größe</th>
<th>Datum</th>
<th>Download</th>
</tr>
<?php
if (isset($_POST['SRS'])) {
echo 'Disabled SkiRegionSimulator 2012';
} else {
include'SRS.php';
}
?>
Everytime I uncheck the box it checks itself again...
Check if this works for you:
<?php
$disabled = array();
$disabled['SRS'] = isset($_POST['SRS']) && (int)$_POST['SRS'] === 1;
$disabled['Bilder'] = isset($_POST['Bilder']) && (int)$_POST['Bilder'] === 1;
$disabled['KWS'] = isset($_POST['KWS']) && (int)$_POST['KWS'] === 1;
?>
<p>Disabled Sections:</p>
<form id="e-d-check" method="post">
<input type="checkbox" value="1" <?php if($disabled['SRS'] === true) echo "checked"; ?> name="SRS" onchange="document.getElementById('e-d-check').submit()">SkiRegionSimulator 2012</input>
<input type="checkbox" value="1" <?php if($disabled['Bilder'] === true) echo "checked"; ?> name="Bilder" onchange="document.getElementById('e-d-check').submit()">Bilder</input>
<input type="checkbox" value="1" <?php if($disabled['KWS'] === true) echo "checked"; ?> name="KWS" onchange="document.getElementById('e-d-check').submit()">Klimawandel-Stunde</input>
</form>
<table>
<tr>
<th>Name</th>
<th>Größe</th>
<th>Datum</th>
<th>Download</th>
</tr>
<?php
if ($disabled['SRS'] === true) {
echo 'Disabled SkiRegionSimulator 2012';
} else {
include'SRS.php';
}
?>
We control what checkbox is checked by using the (guess!) checked attribute. If you are using XHTML then change checked to checked="checked"
A different but similar approach with submit onclick.
<?php
$check_SRS = "";
$check_Bilder = "";
$check_KWS = "";
if (isset($_POST['SRS']) && intval($_POST['SRS']) == 1) { $check_SRS = "checked";}
if (isset($_POST['Bilder']) && intval($_POST['Bilder']) == 1) { $check_Bilder = "checked";}
if (isset($_POST['KWS']) && intval($_POST['KWS']) == 1) { $check_KWS = "checked";}
?>
<p>Disabled Sections:</p>
<form id="e-d-check" method="post">
<input type="checkbox" name="SRS" value="1" onclick="submit()" <?php echo htmlspecialchars($check_SRS); ?>>SkiRegionSimulator 2012</input>
<input type="checkbox" name="Bilder" value="1" onclick="submit()" <?php echo htmlspecialchars($check_Bilder); ?>>Bilder</input>
<input type="checkbox" name="KWS" value="1" onclick="submit()" <?php echo htmlspecialchars($check_KWS); ?>>Klimawandel-Stunde</input>
</form>
<table>
<tr>
<th>Name</th>
<th>Größe</th>
<th>Datum</th>
<th>Download</th>
</tr>
<?php
if (isset($_POST['SRS']) && intval($_POST['SRS']) == 1) {
echo 'Disabled SkiRegionSimulator 2012';
} else {
include'SRS.php';
}
?>
A version of elseif statements to accommodate what are assumed to be the other includes depending on the state of the checkboxes - if one only can be checked:
<?php
if(intval($_POST['SRS']) == 1){
echo 'Disabled SkiRegionSimulator 2012';
}elseif(intval($_POST['SRS']) != 1){
include'SRS.php';
}elseif(intval($_POST['Bilder']) == 1){
echo 'Disabled Bilder';
}elseif(intval($_POST['Bilder']) != 1){
include'Bilder.php';
}elseif(intval($_POST['KWS']) == 1){
echo 'Disabled KWS';
}elseif(intval($_POST['KWS']) != 1){
include'KWS.php';
}
?>
if more than one can be checked:
<?php
if(intval($_POST['SRS']) == 1){
echo 'Disabled SkiRegionSimulator 2012';
}else{
include'SRS.php';
}
if(intval($_POST['Bilder']) == 1){
echo 'Disabled Bilder';
}else{
include'Bilder.php';
}
if(intval($_POST['KWS']) == 1){
echo 'Disabled KWS';
}else{
include'KWS.php';
}
?>
I have written a page that displays a load of data from MySQL database and all works perfect EXCEPT that when i click the Home link (the title of the page) it logs out and i need to log back in, im probably missing something stupid or not doing something i need, code below
<?php
session_start();
?><title>Vend365 Monitor (Beta test)</title>
<h1><u>Vend 365 online monitor (Beta test)<p></p></u></h3>
<?php
require_once ("V365Connect.php");
//Following if 'update' is clicked
if (isset($_POST['lastseen'])){?><p>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 1px;
text-align: left;
}
</style>
<h1>Vend 'Last Seen' times: Page loaded at <?php echo date("d/m/Y G:i:s"); ?></h1>
<?php
$result1 = mysql_query("SELECT distinct customer FROM vends order by customer") or die(mysql_error());
while($row = mysql_fetch_assoc($result1))
{ echo '<table><th>';
?><font size = "5">Customer - '<?php echo $row[customer];?>'</font><?php
echo '</th>';
$result2 = mysql_query("SELECT * FROM vends where customer ='".$row[customer]."'") or die(mysql_error());
while($row1 = mysql_fetch_assoc($result2))
{
echo '<tr><td>';
?>Machine <b>'<?php echo $row1[machine];?>'</b> last seen online at <b>'<?php echo $row1[lastseen];?>'</b> running version <b>'<?php echo $row1[myversion];?>'</b><?php
$today = date("d/m/Y G:i:s");
$lastseentime = $row1[lastseen];
$diff = $today-$lastseentime;
if ($diff == "1"){
?><b> <font color = "red"> -- Last seen online yesterday</font></b> <?php ;}
if ($diff > "1"){
?> <b><font color = "red"> -- Last seen online BEFORE yesterday</font></b> <?php ;}
echo '</td></tr><p>' ;
}
}?></table>
<?php
}else{
if (isset($_POST['update'])){
if ($_POST['checkbox']=='checked'){
$isalive = 1;
} else {
$isalive = 0;
}
if ($_POST['checkbox1']=='checked'){
$hb800 = 1;
} else {
$hb800 = 0;
}
if ($_POST['checkbox2']=='checked'){
$hb1100 = 1;
} else {
$hb1100 = 0;
}
if ($_POST['checkbox3']=='checked'){
$hb1400 = 1;
} else {
$hb1400 = 0;
}
if ($_POST['checkbox4']=='checked'){
$hb1700 = 1;
} else {
$hb1700 = 0;
}
if ($_POST['checkbox5']=='checked'){
$gcmreboot = 1;
} else {
$gcmreboot = 0;
}
if ($_POST['checkbox6']=='checked'){
$emailreboot = 1;
} else {
$emailreboot = 0;
}
if ($_POST['checkbox7']=='checked'){
$hbgcm = 1;
} else {
$hbgcm = 0;
}
if ($_POST['checkbox8']=='checked'){
$hbemail = 1;
} else {
$hbemail = 0;
}
if ($_POST['checkbox9']=='checked'){
$edigcm = 1;
} else {
$edigcm = 0;
}
if ($_POST['checkbox10']=='checked'){
$ediemail = 1;
} else {
$ediemail = 0;
}
if ($_POST['checkbox11']=='checked'){
$reportgcm = 1;
} else {
$reportgcm = 0;
}
if ($_POST['checkbox12']=='checked'){
$reportemail = 1;
} else {
$reportemail = 0;
}
if ($_POST['checkbox13']=='checked'){
$pingmailgcm = 1;
} else {
$pingmailgcm = 0;
}
if ($_POST['checkbox14']=='checked'){
$pingmailemail = 1;
} else {
$pingmailemail = 0;
}
if ($_POST['checkbox15']=='checked'){
$internetgcm = 1;
} else {
$internetgcm = 0;
}
if ($_POST['checkbox16']=='checked'){
$internetemail= 1;
} else {
$internetemail = 0;
}
if ($_POST['checkbox17']=='checked'){
$sqlgcm = 1;
} else {
$sqlgcm = 0;
}
if ($_POST['checkbox18']=='checked'){
$sqlemail = 1;
} else {
$sqlemail = 0;
}
if ($_POST['checkbox19']=='checked'){
$backupgcm = 1;
} else {
$backupgcm = 0;
}
if ($_POST['checkbox20']=='checked'){
$backupemail = 1;
} else {
$backupemail = 0;
}
$sqlupdate = "update vends set isalive='".$isalive."',customer='".$_POST['customer']."',machine='".$_POST['machine']."',mailserver='".$_POST['smtp']."',emails='".$_POST['emails']."',gcm='".$_POST['gcm']."',hb800='".$hb800."',hb1100='".$hb1100."',hb1400='".$hb1400."',hb1700='".$hb1700."',sqlserver='".$_POST['sqlserver']."',sqlport='".$_POST['sqlport']."',sqlinstance='".$_POST['sqlinstance']."',sqldatabase='".$_POST['sqldatabase']."',sqlname='".$_POST['sqlname']."',sqlpassword='".$_POST['sqlpassword']."',rebootgcm='".$gcmreboot."',rebootemail='".$emailreboot."',hbgcm='".$hbgcm."',hbemail='".$hbemail."',edigcm='".$edigcm."',ediemail='".$ediemail."',reportgcm='".$reportgcm."',reportemail='".$reportemail."',mailpinggcm='".$pingmailgcm."',mailpingemail='".$pingmailemail."',internetgcm='".$internetgcm."',internetemail='".$internetemail."',sqlgcm='".$sqlgcm."',sqlemail='".$sqlemail."',backupgcm='".$backupgcm."',backupemail='".$backupemail."',lastseen='".$lastseen."' where mac='".$_SESSION['mac']."'";
mysql_query($sqlupdate) or die(mysql_error());?><h1>
--->Customer - <font color = blue><?php echo $_SESSION['customer'];?></font>
<br />
--->Machine - <font color = blue><?php echo $_SESSION['machine'];?></font>
<p>
<?php
echo "Request sent";
}
else
{
//First load screen to select customer when correct details are entered
if (!isset($_POST['update'])){
if (!isset($_POST['customer'])){
if (!isset($_POST['machine'])){
if (isset($_POST['submit'])){
$result = mysql_query("SELECT * FROM users where user='".$_POST['user']."' and pass='".$_POST['pass']."'") or die(mysql_error());
$count = mysql_num_rows($result);
if ($count == 1){
?><p><p> <table style="border:1px solid black;"><tr><td><h1>Welcome '<?php echo $_POST['user'];?>'</td></tr></table><?php
$_SESSION['customer'];
$_SESSION['machine'];
$_SESSION['mac'];
if (!isset($_POST['customer'])) {
if (!isset($_POST['machine'])) {
echo "<h1><form action ='' method='post'>";
echo "Please Select Your Customer<br />";
$result1 = mysql_query("SELECT distinct customer FROM vends order by customer") or die(mysql_error());
echo "<select name='customer'>";
while($row = mysql_fetch_assoc($result1))
{
echo "<option value = '".$row[customer]."'>".$row[customer]."</option>";
}
echo "</select>";
echo "<input type='submit' value='Go'>";
echo "</form>";
echo "";
echo "Show all 'Last Seen' times";
?>
<form method='post'>
<input type='submit' value='Show Last Seen Status' name ='lastseen' />
</form>
<?php
}
}}else {
// If wrong details entered
echo "Sorry, wrong username or password, please go back and try again";
}
} else {
// Following is first time load screen
?>
<!DOCTYPE HTML> <html>
<head>
<link rel="stylesheet" type="text/css" href="style-sign.css">
</head><h1>
<title>Vend 365 Monitor</title>
<body id="body-color">
<div id="Sign-In">
<fieldset style="width:30%">
<legend>LOG-IN HERE</legend>
<form method="POST"> User <br><input type="text" style="font-size: 30px;" name="user" size="20"><br> Password <br><input type="password" style="font-size: 30px;" name="pass" size="20"><br>
<input id="button" type="submit" style="font-size: 30px; "name="submit" value="Log-In"> </form> </fieldset>
</div>
</body>
</html>
<?php
}}}
} else {
}
?>
<?php
// Select a vending machine
if (!isset($_POST['submit'])){
if (isset($_POST['customer'])) {
$example = $_POST['customer'];
$_SESSION['customer'] = $example;
$result2 = mysql_query("SELECT * FROM vends where customer='".$example."'") or die(mysql_error());
?><h1>
<font color = black>--->Customer - <font color = blue><?php
echo $_SESSION['customer'];?><p></font></font><?php
echo "<form action ='' method='post'>";
echo "Please Select Your Machine<br />";
echo "<select name='machine'>";
while($row = mysql_fetch_assoc($result2))
{
echo "<option value = '".$row[machine]."'>".$row[machine]."</option>";
}
echo "</select>";
echo "<input type='submit' value='Go'>";
echo "</form>";
}}
// show all customer/machine info
if (isset($_POST['machine'])) {
$example1 = $_POST['machine'];
$_SESSION['machine'] = $example1;?>
<h1><font color = black>--->Customer - <font color = blue><?php
echo $_SESSION['customer'];?><br /></font></font><br /><font color = black>--->Machine - <font color = blue><?php
echo $_SESSION['machine']; ?><p><?php
$result3 = mysql_query("SELECT * FROM vends where customer='".$_SESSION['customer']."' and machine ='".$_SESSION['machine']."'") or die(mysql_error());
while ($rows = mysql_fetch_assoc($result3))
{
$tag1 = $rows['hb800'];
$checkedstatus1 = '';
if($tag1 == '1')
{
$checkedstatus1 = 'checked';
} else {
$checkedstatus1 = 'unchecked';
}?><font color = black><form method='post'>
0800 Heartbeat check - <input type='checkbox' value='checked' name='checkbox1' <?php echo $checkedstatus1; ?> />
<br /><?php
$tag2 = $rows['hb1100'];
$checkedstatus2 = '';
if($tag2 == '1')
{
$checkedstatus2 = 'checked';
} else {
$checkedstatus2 = 'unchecked';
}?>
1100 Heartbeat check- <input type='checkbox' value='checked' name='checkbox2' <?php echo $checkedstatus2; ?> />
<br />
<?php $tag3 = $rows['hb1400'];
$checkedstatus3 = '';
if($tag3 == '1')
{
$checkedstatus3 = 'checked';
} else {
$checkedstatus3 = 'unchecked';
}?>
1400 Heartbeat check - <input type='checkbox' value='checked' name='checkbox3' <?php echo $checkedstatus3; ?> />
<br />
<?php
$tag4 = $rows['hb1700'];
$checkedstatus4 = '';
if($tag4 == '1')
{
$checkedstatus4 = 'checked';
} else {
$checkedstatus4 = 'unchecked';
}?>
1700 Heartbeat check - <input type='checkbox' value='checked' name='checkbox4' <?php echo $checkedstatus4; ?> /><br />
<?php
$tag5 = $rows['rebootgcm'];
$checkedstatus5 = '';
if($tag5 == '1')
{
$checkedstatus5 = 'checked';
} else {
$checkedstatus5 = 'unchecked';
}?>
Send GCM when rebooted - <input type='checkbox' value='checked' name='checkbox5' <?php echo $checkedstatus5; ?> /><br />
<?php
$tag6 = $rows['rebootemail'];
$checkedstatus6 = '';
if($tag6 == '1')
{
$checkedstatus6 = 'checked';
} else {
$checkedstatus6 = 'unchecked';
}?>
Send email when rebooted - <input type='checkbox' value='checked' name='checkbox6' <?php echo $checkedstatus6; ?> /><br />
<?php
$tag7 = $rows['hbgcm'];
$checkedstatus7 = '';
if($tag7 == '1')
{
$checkedstatus7 = 'checked';
} else {
$checkedstatus7 = 'unchecked';
}?>
Heartbeat check GCM - <input type='checkbox' value='checked' name='checkbox7' <?php echo $checkedstatus7; ?> /><br />
<?php
$tag8 = $rows['hbemail'];
$checkedstatus8 = '';
if($tag8 == '1')
{
$checkedstatus8 = 'checked';
} else {
$checkedstatus8 = 'unchecked';
}?>
Heartbeat check Email - <input type='checkbox' value='checked' name='checkbox8' <?php echo $checkedstatus8; ?> /><br />
<?php
$tag9 = $rows['edigcm'];
$checkedstatus9 = '';
if($tag9 == '1')
{
$checkedstatus9 = 'checked';
} else {
$checkedstatus9 = 'unchecked';
}?>
EDI fail check GCM - <input type='checkbox' value='checked' name='checkbox9' <?php echo $checkedstatus9; ?> /><br />
<?php
$tag10 = $rows['ediemail'];
$checkedstatus10 = '';
if($tag10 == '1')
{
$checkedstatus10 = 'checked';
} else {
$checkedstatus10 = 'unchecked';
}?>
EDI fail check Email - <input type='checkbox' value='checked' name='checkbox10' <?php echo $checkedstatus10; ?> /><br />
<?php
$tag11 = $rows['reportgcm'];
$checkedstatus11 = '';
if($tag11 == '1')
{
$checkedstatus11 = 'checked';
} else {
$checkedstatus11 = 'unchecked';
}?>
Report fail GCM - <input type='checkbox' value='checked' name='checkbox11' <?php echo $checkedstatus11; ?> /><br />
<?php
$tag12 = $rows['reportemail'];
$checkedstatus12 = '';
if($tag12 == '1')
{
$checkedstatus12 = 'checked';
} else {
$checkedstatus12 = 'unchecked';
}?>
Report fail Email - <input type='checkbox' value='checked' name='checkbox12' <?php echo $checkedstatus12; ?> /><br />
<?php
$gcm = $rows[gcm];
$tag13 = $rows['mailpinggcm'];
$checkedstatus13 = '';
if($tag13 == '1')
{
$checkedstatus13 = 'checked';
} else {
$checkedstatus13 = 'unchecked';
}?>
Ping email server GCM - <input type='checkbox' value='checked' name='checkbox13' <?php echo $checkedstatus13; ?> /><br />
<?php
$tag14 = $rows['mailpingemail'];
$checkedstatus14 = '';
if($tag14 == '1')
{
$checkedstatus14 = 'checked';
} else {
$checkedstatus14 = 'unchecked';
}?>
Ping email server Email - <input type='checkbox' value='checked' name='checkbox14' <?php echo $checkedstatus14; ?> /><br />
<?php
$tag15 = $rows['internetgcm'];
$checkedstatus15 = '';
if($tag15 == '1')
{
$checkedstatus15 = 'checked';
} else {
$checkedstatus15 = 'unchecked';
}?>
Ping internet fail GCM - <input type='checkbox' value='checked' name='checkbox15' <?php echo $checkedstatus15; ?> /><br />
<?php
$tag16 = $rows['internetemail'];
$checkedstatus16 = '';
if($tag16 == '1')
{
$checkedstatus16 = 'checked';
} else {
$checkedstatus16 = 'unchecked';
}?>
Ping internet fail email - <input type='checkbox' value='checked' name='checkbox16' <?php echo $checkedstatus16; ?> /><br />
<?php
$tag17 = $rows['sqlgcm'];
$checkedstatus17 = '';
if($tag17 == '1')
{
$checkedstatus17 = 'checked';
} else {
$checkedstatus17 = 'unchecked';
}?>
Failed SQL ping GCM - <input type='checkbox' value='checked' name='checkbox17' <?php echo $checkedstatus17; ?> /><br />
<?php
$tag18 = $rows['sqlemail'];
$checkedstatus18 = '';
if($tag18 == '1')
{
$checkedstatus18 = 'checked';
} else {
$checkedstatus18 = 'unchecked';
}?>
Failed SQL ping email - <input type='checkbox' value='checked' name='checkbox18' <?php echo $checkedstatus18; ?> /><br />
<?php
$tag19 = $rows['backupgcm'];
$checkedstatus19 = '';
if($tag19 == '1')
{
$checkedstatus19 = 'checked';
} else {
$checkedstatus19 = 'unchecked';
}?>
Backup fail GCM - <input type='checkbox' value='checked' name='checkbox19' <?php echo $checkedstatus19; ?> /><br />
<?php
$tag20 = $rows['backupemail'];
$checkedstatus20 = '';
if($tag20 == '1')
{
$checkedstatus20 = 'checked';
} else {
$checkedstatus20 = 'unchecked';
}?>
Backp fail email - <input type='checkbox' value='checked' name='checkbox20' <?php echo $checkedstatus20; ?> /><br />
<u><b><p>Details</b></u><p>
<?php $_SESSION['mac'] = $rows[mac]; ?>
Mac address - '<?php echo $_SESSION['mac']; ?>'<br />
Customer name - '<?php echo $rows[customer]; ?>'<br />
Machine name - '<?php echo $rows[machine]; ?>'<br />
Current version - '<?php echo $rows[myversion]; ?>'<br />
GCM app code - <input type='text' value='<?php echo $gcm; ?>' size=150 rows=4 name='gcm' />......<br />
SMTP server - '<?php echo $rows[mailserver]; ?>'<br />
Email addresses (seperate by commas) - '<?php echo $rows[emails]; ?>'<br /><p>
<u><b>SQL Credentials</b></u><p>
SQL server - '<?php echo $rows[sqlserver]; ?>'<br />
SQL Port - '<?php echo $rows[sqlport]; ?>'<br />
SQL Instance - '<?php echo $rows[sqlinstance] ?>'<br />
SQL Database - '<?php echo $rows[sqldatabase]; ?>'<br />
SQL User name - '<?php echo $rows[sqlname]; ?>'<br />
SQL Password - '<?php echo $rows[sqlpassword]; ?>' <br /><p>
<p>
<u><b>Alive status</u></b><p>
Last seen online - '<?php echo $rows[lastseen]; ?>'<br />
<?php
$tag = $rows['isalive'];
$checkedstatus = '';
if($tag == '1')
{
$checkedstatus = 'checked';
} else {
$checkedstatus = 'unchecked';
}?>
Request 'IsAlive' status - <input type='checkbox' value='checked' name='checkbox' <?php echo $checkedstatus; ?> /> - This will send a GCM and Email if alive.<p>
<input type='submit' value='Update account details' name ='update' /><p>
</form><p>
<?php
}}}}
?>
I'm trying to check a check box, if the value for that field is 1 in the database.
I have:
<?php
$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK) {
$Priorityquery = "SELECT Priority FROM Data WHERE SPKCustNo = '$selectedSPK' ";
$Priorityresult = mysql_query($Priorityquery);
$row = mysql_fetch_array($Priorityresult);
$checked = $Priorityresult['Priority'];
}
?>
<input name="PriorityCheckBox" type="checkbox" value="1"
<?php if ($checked == 1) echo ' checked'; ?> />
but not getting any joy, any ideas?
Try this:
You were not using the row returned by the query...
<?php
$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK)
{
$Priorityquery = "SELECT Priority FROM Data WHERE SPKCustNo = '$selectedSPK' ";
$Priorityresult = mysql_query($Priorityquery);
$row = mysql_fetch_array($Priorityresult);
//$checked = $Priorityresult['Priority']; // <------ this is where you went wrong...
$checked = $row['Priority']; // <------ this will fix where u went wrong!
}
?>
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1){echo ' checked'; }?>
You should use
<?php if ($checked == 1){echo "checked='checked'"; }
and also
$checked = $Priorityresult['Priority'];
to
$checked = $row['Priority'];
I think you have one mistake... Try this
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($row['Priority'] == 1) echo ' checked'; ?> />
Try it this way
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1) echo "checked='checked'"; ?> />
change
<?php if ($checked == 1) echo ' checked'; ?>
to
<?php if ($checked == 1) echo ' checked="checked"'; ?>
and $checked = $Priorityresult['Priority']; to $checked = $row['Priority'];
It should be checked="checked"
<?php if ($checked == 1) echo "checked='checked'"; ?>
Please how can I get the status of radio buttons based on their value in the database?
I want to set them to either checked or unchecked based on their respective values in the database. field_type is int(1 or 0).
Is there a more efficient way to do this?
$news_alert_status_a = "unchecked";
$news_alert_status_k = "unchecked";
$news_alert_status_n = "unchecked";
$events_alert_status_a = "unchecked";
$events_alert_status_k = "unchecked";
$events_alert_status_n = "unchecked";
$questions_alert_status_a = "unchecked";
$questions_alert_status_k = "unchecked";
$questions_alert_status_n = "unchecked";
$editorials_alert_status_a = "unchecked";
$editorials_alert_status_k = "unchecked";
$editorials_alert_status_n = "unchecked";
if ($news_alert == 1)
{
$news_alert_status_a = "checked";
}
elseif ($news_alert == 2)
{
$news_alert_status_k = "checked";
}
elseif($news_alert == 3)
{
$news_alert_status_n = "checked";
}
if ($events_alert == 1)
{
$events_alert_status_a = "checked";
}
elseif ($events_alert == 2)
{
$events_alert_status_k = "checked";
}
elseif($events_alert == 3)
{
$events_alert_status_n = "checked";
}
if ($questions_alert == 1)
{
$questions_alert_status_a = "checked";
}
elseif ($questions_alert == 2)
{
$questions_alert_status_k = "checked";
}
elseif($questions_alert == 3)
{
$questions_alert_status_n = "checked";
}
if ($editorials_alert == 1)
{
$editorials_alert_status_a = "checked";
}
elseif ($editorials_alert == 2)
{
$editorials_alert_status_k = "checked";
}
elseif($editorials_alert == 3)
{
$editorials_alert_status_n = "checked";
}
The PHP code:
<?PHP
$male_status = 'unchecked';
$female_status = 'unchecked';
if (isset($_POST['Submit1'])) {
$selected_radio = $_POST['gender'];
if ($selected_radio = = 'male') {
$male_status = 'checked';
}
else if ($selected_radio = = 'female') {
$female_status = 'checked';
}
}
?>
The HTML FORM code:
<FORM name ="form1" method ="post" action ="radioButton.php">
<Input type = 'Radio' Name ='gender' value= 'male'
<?PHP print $male_status; ?>
>Male
<Input type = 'Radio' Name ='gender' value= 'female'
<?PHP print $female_status; ?>
>Female
<P>
<Input type = "Submit" Name = "Submit1" VALUE = "Select a Radio Button">
</FORM>
<?php
if($_POST)
{
if($_POST['gender'] != "")
{
$gender = $_POST['gender'] == "male" ? 1 : 0;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Radio Button</title>
</head>
<body>
<form method="post">
<label for="radio_female">Female</label><input type="radio" name="gender" id="radio_female" value="female"/>
<label for="radio_male">Male</label><input type="radio" name="gender" id="radio_male" value="male"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>