Can't show another checkbox by clicking a checkbox dynamically & individually - php

I have a first checkbox with a list of several schemas.
Want I want to do is simple:
When I check a schema, I want to make appear his individual div "list-right" right below, which is ANOTHER checkbox.
Here is my code :
<div>
<div>
<h4>Select your schema(s) :</h4>
<div id="list-schemas">
<?php
foreach ($schemas as $elt) {
echo '<input type="checkbox" name="schemas[]" value="' . $elt->getSchema() . '"/>' . $elt->getSchema() . '<br />';
}
?>
</div>
</div>
<h4>Privileges on tables by selected schemas :</h4>
<div id="div-privileges">
<?php
foreach ($schemas as $elt) {
echo '<div class="list-right" id="' . $elt->getSchema() . '">';
echo '<label for="list-right">' . $elt->getSchema() . ' :</label><br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="REVOKE"/> REVOKE ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="ALL"/> ALL PRIVILEGES ? <br />';
echo '<hr>';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="SELECT"/> SELECT ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="INSERT"/> INSERT ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="UPDATE"/> UPDATE ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="DELETE"/> DELETE ? <br />';
echo '<hr>';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="CREATE"/>CREATE ? <br />';
echo '</div>';
}
?>
</div>
</div>
I managed to display = 'none' ALL THE div "list-right".
As you can see :
var listRight=document.getElementsByClassName("list-right");
for (var i = 0; i < listRight.length; i ++) {
listRight[i].style.display = 'none';
}
What I have without the JavaScript function :
https://image.noelshack.com/fichiers/2019/38/3/1568790896-capture2.png
What I have with the JavaScript function :
https://image.noelshack.com/fichiers/2019/38/3/1568790893-capture.png
What I want :
https://image.noelshack.com/fichiers/2019/38/3/1568790898-capture3.png
But I can't make them appear individually, & dynamically...
Can anybody help me..?

If you want to this this dynamically you will have to use a client-side script, like javascript. Add onclick functions to your checkboxes like this:
echo '<input onclick="show_checkboxes(\''.$elt->getSchema().'\');" type="checkbox" name="schemas[]" value="' . $elt->getSchema() . '"/>' . $elt->getSchema() . '<br />';
Also, change your list-right div to this:
echo '<div class="list-right" id="list_right_'.$elt->getSchema().'" style="display:none;">';
Then add the show_checkboxes function like this:
echo'
<script>
function show_checkboxes(schema){
if(this.checked == true){
document.getElementById("list_right_"+schema).style.display = "block";
}
else{
document.getElementById("list_right_"+schema).style.display = "none";
}
}
</script>';

Finally it's correct, here's the code :
PhP:
<div>
<div>
<h4>Select your schema(s) :</h4>
<div id="list-schemas">
<?php
foreach ($schemas as $elt) {
echo '<input id="' . $elt->getSchema() . '" onclick="show_checkboxes(\''.$elt->getSchema().'\');" type="checkbox" name="schemas[]" value="' . $elt->getSchema() . '"/>' . $elt->getSchema() . '<br />';
}
?>
</div>
</div>
<h4>Privileges on tables by selected schemas :</h4>
<div id="div-privileges">
<?php
foreach ($schemas as $elt) {
echo '<div class="list-right" id="list_right_'.$elt->getSchema().'">';
echo '<label for="list-right">' . $elt->getSchema() . ' :</label><br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="REVOKE"/> REVOKE ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="ALL"/> ALL PRIVILEGES ? <br />';
echo '<hr>';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="SELECT"/> SELECT ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="INSERT"/> INSERT ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="UPDATE"/> UPDATE ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="DELETE"/> DELETE ? <br />';
echo '<hr>';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="CREATE"/>CREATE ? <br />';
echo '</div>';
}
?>
</div>
</div>
Javascript :
/*Way to display none all the list-right divs*/
var listRight=document.getElementsByClassName("list-right");
for (var i = 0; i < listRight.length; i ++) {
listRight[i].style.display = 'none';
}
/*function to display block when we click on the right schema*/
function show_checkboxes(schema){
if(document.getElementById(schema).checked){
document.getElementById("list_right_"+schema).style.display = "block";
}
else{
document.getElementById("list_right_"+schema).style.display = "none";
}
}
Thank you for the help.

Use this to display the correspondent div
<div id="list-schemas">
<?php
foreach ($schemas as $elt) {
echo '<input type="checkbox" name="schemas[]" value="' .$elt->getSchema() . '" class="schema"/>' . $elt->getSchema() . '<br />';
}
?>
</div>
and this in your javascript
<script>
var schmas=document.getElementsByClassName("schema");
for (var i = 0; i < schema.length; i++) {
schema[i].onChange = function(e) {
var schema = e.value;
if (e.checked) {
showMe(schema);
} else {
hideMe(schema);
}
}
}
function showMe(schema) {
document.getElementById(schema).style.display= block;
}
function hideMe(schema) {
document.getElementById(schema).style.display= = none;
}
</script>
But remeber to make all list-right divs hidden by using display:none in css

Related

Problem of link between 2 checkbox, Inserts data when it shouldn't

I need to list to all the schemas in some inputs (checkbox) then we can choose which one(s) we want to manipulate, to give privileges to a specific user.
Anyway, I got this, as you can see:
<div id="list-schemas">
<?php
foreach ($schemas as $elt) {
echo '<input type="checkbox" name="schemas[]" value="' . $elt->getSchema() . '"/>' . $elt->getSchema() . '<br />';
}
?>
</div>
Then, I need also to put some checkbox with the privileges, I did that:
<div id="div-privileges">
<?php
foreach ($schemas as $elt) {
echo '<div class="list">';
echo '<label for="list">' . $elt->getSchema() . ' :</label><br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="REVOKE"/> REVOKE ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="ALL"/> ALL PRIVILEGES ? <br />';
echo '<hr>';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="SELECT"/> SELECT ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="INSERT"/> INSERT ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="UPDATE"/> UPDATE ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="DELETE"/> DELETE ? <br />';
echo '<hr>';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="CREATE"/>CREATE ? <br />';
echo '</div>';
}
?>
</div>
It looks like that: https://image.noelshack.com/fichiers/2019/37/3/1568191628-capture2.png
So, that's being said, here's my function update in my UserManager.class.php:
public static function update(User $newPerso){
$db = DbConnect::getDb();
$newLogin= pg_escape_string($newPerso->getLogin());
$arraySchemas=$newPerso->getSchemas();
$arrayPrivileges=$newPerso->getPrivileges();
if (isset($arraySchemas)){
foreach($arrayPrivileges as $schema => $privileges){
if (isset($arrayPrivileges)){
foreach($privileges as $privilege){
if($privilege=="REVOKE"){
pg_query("{$privilege} ALL ON ALL TABLES IN SCHEMA {$schema} FROM {$newLogin};");
}
else if($privilege=="CREATE"){
pg_query("GRANT {$privilege} ON SCHEMA {$schema} TO {$newLogin};");
}
else if($privilege=="ALL" || $privilege=="INSERT" || $privilege=="SELECT" || $privilege=="UPDATE" || $privilege=="DELETE"){
pg_query("GRANT {$privilege} ON ALL TABLES IN SCHEMA {$schema} TO {$newLogin};");
}
}
}
}
}
}
The fact is that when I do that: https://image.noelshack.com/fichiers/2019/37/3/1568193038-capture3.png
It works, the user will have SELECT + INSERT on "public" & will have UPDATE + DELETE on "schematest"
BUT,
When I do that: https://image.noelshack.com/fichiers/2019/37/3/1568186255-capture.png
My User will have (in that example):
ALL PRIVILEGES in both of these schemas...
It's normal that it works for "public" but how can I prevent for the "schematest"..?
Well guys, I add a "&& in_array($schema, $arraySchemas)"
It seems to work now let me show you :
public static function update(User $newPerso){
$db = DbConnect::getDb();
$newLogin= pg_escape_string($newPerso->getLogin());
$arraySchemas=$newPerso->getSchemas();
$arrayPrivileges=$newPerso->getPrivileges();
if (isset($arraySchemas)){
foreach($arrayPrivileges as $schema => $privileges){
if (isset($arrayPrivileges) && in_array($schema, $arraySchemas)){ /****<- RIGHT HERE****/
foreach($privileges as $privilege){
if($privilege=="REVOKE"){
pg_query("{$privilege} ALL ON ALL TABLES IN SCHEMA {$schema} FROM {$newLogin};");
}
else if($privilege=="CREATE"){
pg_query("GRANT {$privilege} ON SCHEMA {$schema} TO {$newLogin};");
pg_query("GRANT USAGE ON SCHEMA {$schema} TO {$newLogin};");
}
else if($privilege=="ALL" || $privilege=="INSERT" || $privilege=="SELECT" || $privilege=="UPDATE" || $privilege=="DELETE"){
pg_query("GRANT {$privilege} ON ALL TABLES IN SCHEMA {$schema} TO {$newLogin};");
pg_query("GRANT USAGE ON SCHEMA {$schema} TO {$newLogin};");
}
}
}
}
}
}

PHP displays tags in different order

I have following problem, I have list of products in a database and want to display them in table unfortunately it plays some tricks for me because it displays one td before table even begins.
Here are the PHP functions:
<?php
function displayProduct($code,$url)
{
echo '<form method="post" action="cart_update.php"><input type="hidden" name="code" value="' . $code . '"/>';
echo '<input type="hidden" name="return_url" value="' . $url . '" />';
echo '<input type="hidden" name="type" value="add" /><input type="submit" value="Add" /></form>';
}
function displayItem($obj,$url)
{
echo '<tr>';
echo '<td>' . $obj->menuposition . '</td><td>' . $obj->name . '</td><td>' . '£'.$obj->price . '</td><td>' . displayProduct($obj->code,$url) .'</td>';
echo '</tr>';
if(strlen($obj->description) > 2)
{
echo '<tr><td colspan="4" style="font-size: 10px;">' . $obj->description . '</td></tr>';
}
}
?>
And here is the HTML output that I get:
Could someone help me ?
The echo call from displayProduct happens before the echo call of displayItem occurs.
I can see two solutions.
1: displayProduct should return the things to write and not echo them.
2:
echo '<td>' . $obj->menuposition . '</td><td>' . $obj->name . '</td><td>' . '£'.$obj->price . '</td><td>';
displayProduct($obj->code,$url);
echo '</td>';
displayProduct($code,$url) should return the string instead of printing it out:
function displayProduct($code,$url)
{
$result = '<form method="post" action="cart_update.php"><input type="hidden" name="code" value="' . $code . '"/>';
$result .='<input type="hidden" name="return_url" value="' . $url . '" />';
$result .='<input type="hidden" name="type" value="add" /><input type="submit" value="Add" /></form>';
return $result
}
[Edit] I should read better the questions...
But this still applies:
Also as Adrian stated, you should not echo the lines in "displayProducts", but return a string.

While Loop inside While Loop

I have a problem with this code in that the second while loop only runs the first time through the code, and then also it makes it so that the $row['id'] does not get a value. Can you help me figure out where I went wrong with this?
$result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");
$result2 = mysqli_query($con,"SELECT * FROM sections ORDER BY `order`");
while($row = mysqli_fetch_array($result))
{
echo '<form action="../../includes/faqupdate.php" method="post" style="margin:40px;">';
echo '<input type="text" name="order" style="width:20px;text-align:center;" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['order'] . '\':this.value;" value="' . $row['order'] . '">';
echo '<input type="text" name="heading" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['heading'] . '\':this.value;" value="' . $row['heading'] . '">';
echo '<select name="section">';
$section = $row['section'];
while($row = mysqli_fetch_array($result2)) {
$sectionname = $row['sectionname'];
if ($sectionname == $section) {
echo '<option value="' . $sectionname . '" selected="selected">' . $sectionname . '</option>';
} else {
echo '<option value="' . $sectionname . '">' . $sectionname . '</option>';
}
}
echo '</select>';
echo '<input type="text" name="id" style="width:20px;background-color:#CCC;text-align:center;" value="' . $row['id'] . '" readonly>';
echo '<textarea name="content" cols="98" rows="10">' . $row['content'] . '</textarea>';
echo '<input type="submit" name="submission" value="Update">';
echo '<input type="submit" name="submission" value="Delete">';
echo '</form>';
}
Another issue I see with your code is that you will only be able fetch the result set of the query once. So instead get the values beforehand, and store them in a variable like so.
$result2 = mysqli_query($con, "SELECT * FROM sections ORDER BY `order`");
$sectionnames = array();
while($row = mysqli_fetch_array($result2)) {
$sectionnames[] = $row['sectionname'];
}
$result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");
while($row = mysqli_fetch_array($result))
{
echo '<form action="../../includes/faqupdate.php" method="post" style="margin:40px;">';
echo '<input type="text" name="order" style="width:20px;text-align:center;" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['order'] . '\':this.value;" value="' . $row['order'] . '">';
echo '<input type="text" name="heading" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['heading'] . '\':this.value;" value="' . $row['heading'] . '">';
echo '<select name="section">';
$section = $row['section'];
foreach ($sectionnames as $sectionname) {
if ($sectionname == $section) {
echo '<option value="' . $sectionname . '" selected="selected">' . $sectionname . '</option>';
} else {
echo '<option value="' . $sectionname . '">' . $sectionname . '</option>';
}
}
echo '</select>';
echo '<input type="text" name="id" style="width:20px;background-color:#CCC;text-align:center;" value="' . $row['id'] . '" readonly>';
echo '<textarea name="content" cols="98" rows="10">' . $row['content'] . '</textarea>';
echo '<input type="submit" name="submission" value="Update">';
echo '<input type="submit" name="submission" value="Delete">';
echo '</form>';
}
It's because you are redeclaring $row, try:
$result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");
$result2 = mysqli_query($con,"SELECT * FROM sections ORDER BY `order`");
while($row = mysqli_fetch_array($result))
{
echo '<form action="../../includes/faqupdate.php" method="post" style="margin:40px;">';
echo '<input type="text" name="order" style="width:20px;text-align:center;" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['order'] . '\':this.value;" value="' . $row['order'] . '">';
echo '<input type="text" name="heading" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['heading'] . '\':this.value;" value="' . $row['heading'] . '">';
echo '<select name="section">';
$section = $row['section'];
while($row2 = mysqli_fetch_array($result2)) {
$sectionname = $row2['sectionname'];
if ($sectionname == $section) {
echo '<option value="' . $sectionname . '" selected="selected">' . $sectionname . '</option>';
} else {
echo '<option value="' . $sectionname . '">' . $sectionname . '</option>';
}
}
echo '</select>';
echo '<input type="text" name="id" style="width:20px;background-color:#CCC;text-align:center;" value="' . $row['id'] . '" readonly>';
echo '<textarea name="content" cols="98" rows="10">' . $row['content'] . '</textarea>';
echo '<input type="submit" name="submission" value="Update">';
echo '<input type="submit" name="submission" value="Delete">';
echo '</form>';
}
Rename your $row in your second while condition for $row2

PHP and mySQL "UPDATE" doesn't actually update

So me and my friend came to a conclusion that it's the $_email variable that screws everything up. As long as it's hard coded in, it works. But as soon as it's left as a $_email everywhere, it doesn't. The message goes through as "updated" but it doesn't update.
require_once('appVars6.php');
require_once('connectVars6.php');
$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_id = $_GET['id'];
$_queryOne = "SELECT * FROM midterm WHERE id = '$_id'";
$_resultOne = mysqli_query($_dbc, $_queryOne) or die ('Error Querying Database');
while ($_row = mysqli_fetch_array($_resultOne)) {
echo '<form class="update" method="post" action="MT_vjones_udpateRecord.php?id=' . $_id . '">';
echo '<input type="hidden" name="id" id="id" value="' . $_row['id'] . '" />';
echo '<input type="text" name="firstName" id="firstName" value="' . $_row['firstName'] . '" /><br />';
echo '<input type="text" name="lastName" id="lastName" value="' . $_row['lastName'] . '" /><br />';
echo '<input type="text" name="email" id="email" value="' . $_row['email'] . '" /><br />';
echo '</form>';
}
if ( isset($_GET['firstName']) && isset($_GET['lastName']) && isset($_GET['email'])) {
$_id = $_GET['id'];
$_firstName = $_GET['firstName'];
$_lastName = $_GET['lastName'];
$_email = $_GET['email'];
}
else if ( isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['email'])) {
$_id = $_POST['id'];
$_firstName = mysqli_real_escape_string($_dbc, trim($_POST['firstName']));
$_lastName = mysqli_real_escape_string($_dbc, trim($_POST['lastName']));
$_email = mysqli_real_escape_string($_dbc, trim($_POST['email']));
}
else {
echo '<br />';
echo '<p class="error">Sorry, no record was selected.</p>';
}
if(isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
//$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_query = "UPDATE midterm " .
"SET email = '$_email'" .
"WHERE id = $_id" ;
$_result = mysqli_query($_dbc, $_query) or die (mysqli_error($_dbc));
mysqli_close($_dbc);
echo '<p>The record of ' . $_firstName . ' ' . $_lastName . ' for ' . $_email . ' was successfully updated.';
}
else {
echo '<p class="error">The record was not updated.</p>';
}
}
else if (isset($_id) && isset($_firstName) && isset($_lastName) && isset($_email)) {
echo '<p>Are you sure you want to update the following record?</p>';
/*echo '<form class="update" method="post" action="MT_vjones_updateRecord.php">';
echo '<input type="text" name="firstName" id="firstName" value="' . $_firstName . '" /><br />';
echo '<input type="text" name="lastName" id="lastName" value="' . $_lastName . '" /><br />';
echo '<input type="text" name="email" id="email" value="' . $_email . '" /><br />';
echo '</form>';*/
echo '<form class="update" method="post" action="MT_vjones_updateRecord.php?id=' . $_id . '">';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="Yes" /> Yes </div><br />';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="No" checked="checked" /> No </div><br /><br />';
echo '<input class="applyBtn" type="submit" value="UPDATE" name="submit" />';
echo '<input type="hidden" name="id" value="' . $_id . '" />';
echo '<input type="hidden" name="firstName" value="' . $_firstName . '" />';
echo '<input type="hidden" name="lastName" value="' . $_lastName . '" />';
echo '<input type="hidden" name="email" value="*testBACK2FUN#test.com*" />';
}
echo '<p><< Back to the Admin Page</p>';
As you can see, we put in the email address in there for testing purposes...
check the id matches what you are intending to update.
To be sure print the $_id and $_email prior to the update and after.
#user710502: You don't need to segregate quotes with double-quotes in PHP. It reads it anyway, the only time you might bother is if you are reading from an array
eg:
"UPATE midterm SET email='".$POST['email']."'"
$_query = "UPDATE midterm " .
"SET email = '$_email' WHERE id = '$_id'" ;
should be
$_query = "UPDATE midterm " .
"SET email = $_email".
"WHERE id = $_id " ;
Reason is you are using $_ before variable which is not a valid variable declaration.
Because $_ is reserved for SUPER GLOBAL in php (i.e $_SESSION,$_SERVER,$_POST,$_GET,$_COOKIE etc).
if its not a issue for you then you need to concat your variable as below.
$_query = "UPDATE midterm SET email = '".$_email."' WHERE id = '".$_id."'" ;
SOLVED! Form issues.
SHOULD BE:
<?php
require_once('appVars6.php');
require_once('connectVars6.php');
$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_id = $_GET['id'];
$_queryOne = "SELECT * FROM midterm WHERE id = '$_id'";
$_resultOne = mysqli_query($_dbc, $_queryOne) or die ('Error Querying Database');
while ($_row = mysqli_fetch_array($_resultOne)) {
echo '<form class="update" method="post" action="MT_vjones_udpateRecord.php?id=' . $_id . '">';
echo '<input type="hidden" name="id" id="id" value="' . $_row['id'] . '" />';
echo '<input type="hidden" name="firstName" id="firstName" value="' . $_row['firstName'] . '" />';
echo '<input type="hidden" name="lastName" id="lastName" value="' . $_row['lastName'] . '" />';
echo '<input type="hidden" name="email" id="email" value="' . $_row['email'] . '" />';
echo '</form>';
}
if ( isset($_GET['firstName']) && isset($_GET['lastName']) && isset($_GET['email'])) {
$_id = $_GET['id'];
$_firstName = $_GET['firstName'];
$_lastName = $_GET['lastName'];
$_email = $_GET['email'];
}
else if ( isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['email'])) {
$_id = $_POST['id'];
$_firstName = mysqli_real_escape_string($_dbc, trim($_POST['firstName']));
$_lastName = mysqli_real_escape_string($_dbc, trim($_POST['lastName']));
$_email = mysqli_real_escape_string($_dbc, trim($_POST['email']));
}
else {
echo '<br />';
echo '<p class="error">Sorry, no record was selected.</p>';
}
if(isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
$_query = "UPDATE midterm " .
"SET email = '$_email'" .
"WHERE id = $_id" ;
$_result = mysqli_query($_dbc, $_query) or die (mysqli_error($_dbc));
mysqli_close($_dbc);
echo '<p>The record of ' . $_firstName . ' ' . $_lastName . ' for ' . $_email . ' was successfully updated.';
}
else {
echo '<p class="error">The record was not updated.</p>';
}
}
else if (isset($_id) && isset($_firstName) && isset($_lastName) && isset($_email)) {
echo '<p>Are you sure you want to update the following record?</p>';
echo '<form class="update" method="post" action="MT_vjones_updateRecord.php?id=' . $_id . '">';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="Yes" /> Yes </div><br />';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="No" checked="checked" /> No </div><br /><br />';
echo '<input type="hidden" name="id" value="' . $_id . '" />';
echo '<input type="text" name="firstName" value="' . $_firstName . '" /><br />';
echo '<input type="text" name="lastName" value="' . $_lastName . '" /><br />';
echo '<input type="text" name="email" value="' . $_email . '" />';
echo '<input class="applyBtn" type="submit" value="UPDATE" name="submit" />';
}
echo '<p><< Back to the Admin Page</p>';
?>

How to show which form elements are already selected in PHP

How can I get this code to show which option is already selected?
This is basically an edit page and it is pulling info from the database and populating the relative fields
I have a drop-down menu, multiple select box, and radio buttons on a page along with some elements. The info is getting displayed in the elements fine, but I can't work out how to get the s and radio buttons to display selected if they match the info from the database.
code:
<select name="client">
<option value="empty">Change Client...</option>
<?php
$result2 = mysql_query("SELECT name FROM clients") or die("Database query failed: " . mysql_error());
while($row = mysql_fetch_assoc($result2)) {
$clientlist = $row['name'];
$clientname = htmlspecialchars($row['name']);
if ($_POST['client'] == $clientlist)
{
echo '<option value="' . $clientlist . '" selected="selected" >' . $clientname . '</option>' . '\n';
}
else{
echo '<option value="' . $clientlist . '" >' . $clientname . '</option>' . '\n';
}
}
?>
</select>
</p>
<p class="subheadsmall">Core Classification</p>
<?php
switch ($niche) {
case "brand":
echo '<input type="radio" name="niche" value="Brand" checked="checked" />Brand';
echo '<input type="radio" name="niche" value="Marketing" />Marketing';
echo '<input type="radio" name="niche" value="Communication" />Communication';
break;
case "marketing":
echo '<input type="radio" name="niche" value="Brand" />Brand';
echo '<input type="radio" name="niche" value="Marketing" checked="checked" />Marketing';
echo '<input type="radio" name="niche" value="Communication" />Communication';
break;
case "communication":
echo '<input type="radio" name="niche" value="Brand" />Brand';
echo '<input type="radio" name="niche" value="Marketing" />Marketing';
echo '<input type="radio" name="niche" value="Communication" checked="checked" />Communication';
break;
default;
echo '<input type="radio" name="niche" value="Brand" />Brand';
echo '<input type="radio" name="niche" value="Marketing" />Marketing';
echo '<input type="radio" name="niche" value="Communication" />Communication';
break;
}
?>
<p class="subheadsmall">Strategies</p>
<p class="sidebargrey">
<?php
$result = mysql_query("SELECT strategies FROM studies WHERE id = '$id';
if (!$result) {
die("Database query failed: " . mysql_error());
}
while($row = mysql_fetch_array($result)) {
$strategyname = $row['strategies'];
echo $strategyname.'<br />';
}
?>
<p class="subheadsmall">Add a strategy... (hold down command key to select more than one)</p>
<select name="strategies[]" multiple="multiple">
<?php
$result = mysql_query("SELECT * FROM strategies");
if (!$result) {
die("Database query failed: " . mysql_error());
}
while($row = mysql_fetch_array($result)) {
$strategylist = $row['name'];
$strategyname = htmlspecialchars($row['name']);
$pagelink = str_replace(" ","_",$strategylist);
echo '<option value="<a href="strategies.php?strategy=' . $pagelink . '">'.$strategyname.'</a>" >' . $strategyname . '</option>' . '\n';
}
?>
</p>
OPTION HTML Spec
Change selected="selected" to just selected. Looks like that attribute doesn't need an assignment.
You might also want to check the HTML that's being output just to make sure your assignment is evaluating to true.
You could use javascript to do this. My example uses jquery
First give each of your checkboxes an id so
echo '<input type="radio" name="niche" id="brand" value="Brand" />Brand';
echo '<input type="radio" name="niche" id="marketing" value="Marketing" />Marketing';
echo '<input type="radio" name="niche" id="communication" value="Communication" />Communication';
Then your JS would be
$( "brand" ).attr( "checked", true ); // this would check the brand box
So you can just write out these as needed.
You should -- if possible -- change that group of radio prints to a for loop. Then you can do something like this:
foreach ($possibleRadios as $key => $val)
{
echo '<input type="radio" name="' . $val->name . '" value="' . $val->id . '" ' . ($isSelected($val->id) ? 'selected="selected' : '') . ' />$val->prettyName';
}

Categories