Click to See Complete Forum and Search --> : 'Dynamic' checkboxes question


invision
01-22-2008, 05:07 AM
Hi,

I have the following PHP code:

if ($visible=='Y') {
echo '<input type="checkbox" name="current" id="current" value="' . $id . '" class="maillistadd" checked /></td>';
}
else {
echo '<input type="checkbox" name="current" id="current" value="' . $id . '" class="maillistadd" /></td>';
}


Now this shows section puts a check in the checkbox next to each record where visible equals 'Y' in my db.

However, I want to be able to use an UPDATE query to change all the values if the user checks/unchecks the boxes.

I've tried $query = "UPDATE footer_logos SET visible = 'Y', approved = 'Y' WHERE id = '$id'";, but it was unsuccessful.

Any help is greatly appreciated.

abou.hmed
01-22-2008, 05:14 AM
r u getting any error?

invision
01-22-2008, 05:20 AM
When I uncheck No.4, submit the form and output the SQL result, it shows:

UPDATE footer_logos SET visible = 'Y', approved = 'Y' WHERE id = '4'

However, I'd prefer it to say 'visible = 'N','

Is there an easy way to do some kind of IF statement to check for 'checked' in each input field. And if it's unchecked then set visible and approved to 'N' ?

Thanks for the help.

abou.hmed
01-22-2008, 05:25 AM
i beleive this is better

if ($visible=='Y') {$checked="checked";}
echo '<input type="checkbox" name="current" id="current" value="' . $id . '" class="maillistadd" '.$checked.' /></td>';

and in the query use this

if($_REQUEST['current'])
$checkValue="Y";
else $checkValue="N";

$query = "UPDATE footer_logos SET visible = '".$checkValue."', approved = '".$checkValue."' WHERE id = '$id'";

and sure execute the query:)

invision
01-22-2008, 05:33 AM
OK. I've made the changes.

if ($visible=='Y') {$checked="checked";}
echo '<input type="checkbox" name="current" id="current" value="' . $id . '" class="maillistadd" '.$checked.' /></td>';

if($_REQUEST['current'])
$checkValue="Y";
else $checkValue="N";

$query = "UPDATE footer_logos SET visible = '".$checkValue."', approved = '".$checkValue."' WHERE id = '$id'";

But get the same error :( Doesn't seem to uncheck them :(

Thanks again.

abou.hmed
01-22-2008, 05:38 AM
please send all the code, to see what is the missing

invision
01-22-2008, 05:40 AM
Sounds good.


<?php # second stage of editing offers

require_once('../dbconn/connect/hidden/cnxfile.php');

$page_title = "Footer Logos";

include ('../include_files_files/aheaderfile.html');

if (($_SESSION['usercat']!='a')) {
echo 'Sorry, you are not logged in';
exit;
}

if (isset($_POST['submitted'])) {

$id = $_POST['current'];

if (($_SESSION['user_id'] == 0) || ($_SESSION['user_id'] == 2)) {

if($_REQUEST['current'])
$checkValue="Y";
else $checkValue="N";

$query = "UPDATE footer_logos SET visible = '".$checkValue."', approved = '".$checkValue."' WHERE id = '$id'";

echo $query;

if ($result = mysql_query($query)) { // Everything Worked.

echo '<hr />';

echo '<h2>"Footer Logos" area updated !</h2>';

echo '<p>The Footer Logos have been updated on the site.</p>';

include ('../include_files/thefooterfile.html');

exit;

}

}

if ($result = mysql_query($query)) { // Everything Worked.

echo $query;

echo '<h2>Footer Logos updated !</h2>';

echo '<p>The Footer Logos have been updated on the database.</p>';

echo '<ul><li><a href="index.php">Go back to Admin</a></li>';

echo '<li><a href="javascript:history.go(-1)">Go back to the previous page</a></li></ul>';

include ('../include_files/thefooterfile.html');

exit;

}

else {

echo 'Error!';

// echo '<br />Sorry, you have to be one of the main Administrators to make changes here.<br />';

// trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
}

}

















if (isset($_POST['submitted2'])) {

$link = strip_tags(stripslashes($_POST['link']));
$alt_text = strip_tags(stripslashes($_POST['alt_text']));
$user_id = $_SESSION[user_id];

if (is_uploaded_file ($_FILES['thumb']['tmp_name'])) {

if (move_uploaded_file($_FILES['thumb']['tmp_name'], "../images/{$_FILES['thumb']['name']}")) { // Moves the files over, for security.
$MIMEtype = $_FILES['thumb']['type'];
echo '';
} else { // If the file couldn't be moved over.
echo '';
$t = FALSE;
}
$t = $_FILES['thumb']['name'];
} else {
$t = FALSE;
}

if (!$_SESSION['usercat']) {
echo '<p>Sorry, you are not logged in</p>';
echo '<div class="alert">';
echo '<b>Options :</b>';
echo '<br /><a href="javascript:history.go(-1)">Go back to previous page</a><br />';
echo '<br /><a href="login.php">Login</a><br />';
echo '<br /><a href="forgot_password.php" title="">Forgotten Password ?</a><br />';
echo '</div>';
exit();
}

$query = "INSERT INTO footer_logos(link,alt,src,visible) VALUES('$link','$alt_text','$t','N')";
// $result = mysql_query($query);

echo $n;

echo $query;

if ($result = mysql_query($query)) { // Everything Worked.

echo '<hr />';

echo '<p><b>New Logo Added !</b></p><br />';

echo '<p>The Logo has been added to the system.<br /></p>';

include ('../include_files/thefooterfile.html');
exit;
}

}

$query1 = "SELECT * FROM footer_logos";
$result1 = @mysql_query($query1); // Run the Query.


echo '<p><b>Edit "Footer Logos"</b></p>';

echo '<form name="form1" method="POST" action="update_logos.php" class="book">';

echo '<table width="550" border="0" style="padding:5px">';
echo '<tr style="background-color:#eaeaea;">
<td>Visible</td>
<td>Image</td>
<td>Link</td>
<td>ALT Text</td>
</tr>';
while ($row1 = mysql_fetch_array($result1)) {

$visible = $row1['visible'];
$id = $row1['id'];

echo '<tr><td>';

if ($visible=='Y') {$checked="checked";}
echo '<input type="checkbox" name="current" id="current" value="' . $id . '" class="maillistadd" '.$checked.' /></td>';

/*if ($visible=='Y') {
echo '<input type="checkbox" name="current" id="current" value="' . $id . '" class="maillistadd" checked /></td>';
}
else {
echo '<input type="checkbox" name="current" id="current" value="' . $id . '" class="maillistadd" /></td>';
}*/

echo '<td><img src="' . $row1[1] . '" /></td>
<td>' . $row1[2] . '</td><td>' . $row1[4] . '</td>';
echo '</tr>';
}

echo '</table>';

?>
<br>
<input name="submit" type="submit" class="submit" value="Update the 'Logos' box">
<br>
<br>
<input type="hidden" value="form1">
<input type="hidden" name="submitted" value="TRUE" />
</p>
</form>

<hr size="2" />

<br />

<p><b>Add new "Footer Logos" box</b></p>

<form name="form2" action="update_logos.php" method="post">

Link<br />
<input type="text" name="link" id="link" value="<?php if (isset($_POST['link'])) echo $_POST['link']; ?>" />
<br /><br />
Alt Text<br />
<input type="text" name="alt_text" id="alt_text" value="<?php if (isset($_POST['alt_text'])) echo $_POST['alt_text']; ?>" />
<br /><br />
Image<br />
<input type="file" name="thumb" id="thumb" />
<br /><br />
<input type="submit" name="submit" value="Add New Logo" />
<input type="hidden" value="form2">
<input type="hidden" name="submitted2" value="TRUE" />
</form>

<?

include ('../include_files/thefooterfile.html');

?>

abou.hmed
01-22-2008, 06:07 AM
does the database change?

invision
01-22-2008, 06:11 AM
No, because either way it still just makes the 'visible' and 'approved' values equal to Y.

Plus once I uncheck one or two and submit it seems to 'check' all of them, even though some in the db have the 'visible' value set to 'N'??

abou.hmed
01-22-2008, 06:14 AM
ok just change this
if ($visible=='Y') {$checked="checked";}
to be like this
if ($visible=='Y') {$checked="checked";}
else{$checked="";}

invision
01-22-2008, 06:17 AM
OK. It now displays the checkboxes showing those that are 'visible' etc. correctly.

However, when I check a box, and submit it, it makes no change to the db.

It must be the query at fault.

$query = "UPDATE footer_logos SET visible = '".$checkValue."', approved = '".$checkValue."' WHERE id = '$id'";

abou.hmed
01-22-2008, 06:20 AM
ok when you make echo to the query, what are you getting
and what is the property of the fields visible and approved in the database

invision
01-22-2008, 06:27 AM
If I check the one that is not visible and submit :
UPDATE footer_logos SET visible = 'Y', approved = 'Y' WHERE id = '6'

It just doesn't seem to understand if any fields are unchecked, set it to 'N' :(

abou.hmed
01-22-2008, 06:33 AM
right i ve just notice this
in the check box u dnt need to put value='$id' just delete this part and let me know if it works

invision
01-22-2008, 06:38 AM
OK. I've taken that out, but it still doesn't work.


<form name="form1" method="POST" action="update_logos.php" class="book"><table width="550" border="0" style="padding:5px"><tr style="background-color:#eaeaea;">
<td>Visible</td>
<td>Image</td>
<td>Link</td>
<td>ALT Text</td>
</tr><tr><td><input type="checkbox" name="current" id="current" class="maillistadd" checked /></td><td><img src="../images/logo_scotexec.gif" /></td>
<td>http://www.test.com</td>
<td>test alt text </td>
</tr><tr><td><input type="checkbox" name="current" id="current" class="maillistadd" checked /></td><td><img src="../images/logo_show.gif" /></td>
<td>http://www.test1.com</td>
<td>http://www.test.com</td>
</tr><tr><td><input type="checkbox" name="current" id="current" class="maillistadd" checked /></td><td><img src="../images/scot_blood_logo.gif" /></td>
<td>http://www.test2.com</td>
<td>Find out more about giving blood</td></tr><tr><td><input type="checkbox" name="current" id="current" class="maillistadd" checked /></td><td><img src="../images/heart_logo.gif" /></td>
<td>http://www.test3.com=1072</td>
<td>Find out about organ donation</td></tr><tr><td><input type="checkbox" name="current" id="current" class="maillistadd" checked /></td><td><img src="../images/nhs24_logo.gif" /></td>
<td>http://www.test4.com</td>
<td>http://www.test.com</td>
</tr><tr><td><input type="checkbox" name="current" id="current" class="maillistadd" checked /></td><td><img src="" /></td>
<td>http://www.test5.com</td>
<td>Tester</td></tr></table>
<br>
<input name="submit" type="submit" class="submit" value="Update the 'Logos' box">
<br>
<br>
<input type="hidden" value="form1">
<input type="hidden" name="submitted" value="TRUE" />
</p>
</form>


Does my query need some attention so it updates ALL records, not just one:

$query = "UPDATE footer_logos SET visible = '".$checkValue."' WHERE id = '$id'";

abou.hmed
01-22-2008, 06:40 AM
the current query will update just one row because you have where condition
if you want to update all the rows just delete the where

invision
01-22-2008, 06:42 AM
But do I not need the 'WHERE' clause to update those that change?

Thanks for your patience with this, appreciate it.

abou.hmed
01-22-2008, 06:46 AM
i start to be confused now
do u want to update all the records or just the record related to a specific id
if all the record delete the where
if just for those that change you need to keep it
and if you mean to update all the records for one id also you need to keep it , cz your current query do this thing

invision
01-22-2008, 07:12 AM
I want to update the ones that change.

Can you help me with this?

Sorry for the delay in my response.

abou.hmed
01-22-2008, 08:01 AM
sorry i was in break as well :P
ok to understand exaclty what is happening you have multi checkbox and u want to update it in database after submiting
if yest just do it by this way

echo '<input type="checkbox" name="current[]" id="current[]" value="' . $id . '" class="maillistadd" '.$checked.' /></td>';
by this way u will get the list of all the checkboxes then you will make the database changes using loop

foreach($_REQUEST['current'] as $key=>$value)
{
if($key)
$checkValue="Y";
else $checkValue="N";

$query = "UPDATE footer_logos SET visible = '".$checkValue."', approved = '".$checkValue."' WHERE id = '$value'";
}

so by this way it will check the list of all the checkboxes and change it
hope that i have understand correctly what you are asking!

invision
01-22-2008, 08:09 AM
Thanks for the reply and your patience once more.


foreach($_REQUEST['current'] as $key=>$value)
{
if($key)
$checkValue="Y";
else $checkValue="N";

$query = "UPDATE footer_logos SET visible = '".$checkValue."' WHERE id = '$value'";

}


And the checkboxes PHP:


while ($row1 = mysql_fetch_array($result1)) {

$visible = $row1['visible'];
$id = $row1['id'];

echo '<tr><td>';

if ($visible=='Y') {$checked="checked";}
else{$checked="";}

echo '<input type="checkbox" name="current[]" id="current[]" value="' . $id . '" class="maillistadd" '.$checked.' /></td>';

echo '<td><img src="' . $row1[1] . '" /></td>
<td>' . $row1[2] . '</td><td>' . $row1[4] . '</td>';
echo '</tr>';
}


Is the above correct? As when I submit it, it says my query is empty :(