Jump to content

[SOLVED] Finding out checkbox's value


Nexy

Recommended Posts

Why Hello There!  :) Just wanted how I would find out a checkbox's value, name/id. Any would do just fine. Here is a part of my code:

 

if($_POST['delete']) {

if($_POST['$del'] == 1) {
    echo "Are you sure?";
}
}


while($edit = mysql_fetch_array($nres)) {
	$del = $edit['id'];

	echo "<div id='edit'>";
	echo $del . ". ";
	echo "<input type='checkbox' id='$del' name='$del' value='Delete' />";
	echo "[" . $edit['user'] . "] ";
	echo "<a href='index.php?page=Account&do=editnews&id=$del'>";
	echo $edit['subject'] . "</a>";
	echo "</div>";

}

 

As you can see, the checkbox is created inside a while statement, and there is no telling how many there will be. What I was trying to do is if the user clicks Submit, check for all the checkboxes that are checked then delete those ID's selected from my mysql table. The if statement doesn't echo anything and would be better if there was someway to code it so I don't have to specify this part " == 1)", but somehow code it so it knows which value, id/name is selected. Hope someone can help.

 

Thank You!  :)

Link to comment
Share on other sites

How about when the form is submitted looping through them again but testing to see if they are checked

//Submit Part
while($edit = mysql_fetch_array($nres)) {//loop through the same table
       	$del = $edit['id'];
       if($_POST['$del'] == 'checked'){
        //Delete Statement
        }
}

//Form part
while($edit = mysql_fetch_array($nres)) {
$del = $edit['id'];
echo "<input type='checkbox' id='$del' name='$del' value='checked' />";
}

Link to comment
Share on other sites

I didn't put the delete statement, but it also didn't echo anything and stopped the while statement after the if statement. So that stopped showing as well.

 

if($_POST['delete']) {
while($edit = mysql_fetch_array($nres))
{
	if($_POST['$del'] == 'checked') {
    	echo "Are you sure?";
	}
}
}

while($edit = mysql_fetch_array($nres)) {
	$del = $edit['id'];

	echo "<div id='edit'>";
	echo $del . ". ";
	echo "<input type='checkbox' id='$del' name='$del' value='checked' />";
	echo "[" . $edit['user'] . "] ";
	echo "<a href='index.php?page=Account&do=editnews&id=$del'>";
	echo $edit['subject'] . "</a>";
	echo "</div>";

}

Link to comment
Share on other sites

Only checked values are passes from the form. A better method is to create the checkboxes as an array. Then you just need to loop througt that one array element to find the selected checkboxes.

 

Rough example:

<?php

if(count($_POST['delete'])) {

   //Create one query to delete all the selected values
   $query = "DELETE FROM table WHERE id IN (" . implode(',', $_POST['delete']) . ")";
   $result = mysql_query($query) or die (mysql_error());

}

   while($edit = mysql_fetch_array($nres)) {
       $del = $edit['id'];

       echo "<div id='edit'>";
       echo $del . ". ";
       echo "<input type='checkbox' id='$del' name='delete[]' value='$del' />";
       echo "[" . $edit['user'] . "] ";
       echo "<a href='index.php?page=Account&do=editnews&id=$del'>";
       echo $edit['subject'] . "</a>";
       echo "</div>";

   }

?>

Link to comment
Share on other sites

I tried your way mjdamato, and I get this error:

 

Warning: implode() [function.implode]: Invalid arguments passed in /home/divnxn5/public_html/account/editnews.php on line 56

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

 

if(count($_POST['delete'])) {

$dels = "DELETE FROM news WHERE id IN (" . implode(',', $_POST['delete']) . ")";
$delres = mysql_query($dels) OR die(mysql_error());
}

while($edit = mysql_fetch_array($nres)) {
	$del = $edit['id'];

	echo "<div id='edit'>";
	echo $del . ". ";
	echo "<input type='checkbox' id='$del' name='delete[]' value='$del' style='border: 0' />";
	echo "[" . $edit['user'] . "] ";
	echo "<a href='index.php?page=Account&do=editnews&id=$del'>";
	echo $edit['subject'] . "</a>";
	echo "</div>";

}

 

Also, can you explain the query you made, as I've never seen the word "count, IN" and the way you formatted the code after IN. Would be much appreciated. Thank You!

Link to comment
Share on other sites

It works for me. I have included a slightly modified version of what you had with my code and it works fine. There must be a syntax error somewhere. Or, I uspect, your checkboxes don't have the same array name like I specified. That would explain the implode error.

 

Because I changed the checkboxes to be returned as an array I used if(count($_POST['delete'])) to determine if any checkboxes had been checked. You could probably also just use if($_POST['delete']).

 

Because the checked values are returned as an array I used the implode function to combine the id's comma separated. You can use the "IN" statement to find any records matchng one of the values. The resulting query should look something like this:

 

DELETE FROM news WHERE id IN (2,3,4)

 

My test script:

<?php

if(count($_POST['delete'])) {

$dels = "DELETE FROM news WHERE id IN (" . implode(',', $_POST['delete']) . ")";
echo "Query: $dels<br>";
//$delres = mysql_query($dels) OR die(mysql_error());
}

?>

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

<?php

//while($edit = mysql_fetch_array($nres)) {
for ($i=1; $i<6; $i++) {
    //$del = $edit['id'];
    $del = $i;
    echo "<div id='edit'>";
    echo $del . ". ";
    echo "<input type='checkbox' id='$del' name='delete[]' value='$del' style='border: 0' />";
    echo "[user$i] ";
    echo "<a href='index.php?page=Account&do=editnews&id=$del'>";
    echo "Subject$i</a>";
    echo "</div>";
}

?>

<button type="submit">Submit</button>
</form>

Link to comment
Share on other sites

You need to have something checked.  I'd suggest adding a check before the query:

 

This line already takes care of that

if(count($_POST['delete'])) {

 

This check goes a step further than just isset because it also validates that the value is an array.

Link to comment
Share on other sites

I'm still lost pretty lost here. I tried both of your ways, with the isset and the for loops. I still get the same error. I reverted back to the old code I had and I don't get the line where it says check your mysql syntax. All I get is this now:

 

Warning: implode() [function.implode]: Invalid arguments passed in /home/divnxn5/public_html/account/editnews.php on line 56.

 

I'll post the whole code.

 

<?php

$title = mysql_real_escape_string(stripslashes($_POST['title']));
$news = mysql_real_escape_string(stripslashes($_POST['idedit']));
$id = mysql_real_escape_string(stripslashes($_GET['id']));
$avatar = mysql_real_escape_string(stripslashes($_POST['avatar']));
$set = "1";

if($_POST['editnews'])
{
if(!empty($title) && !empty($news))
{
	mysql_query("UPDATE news SET subject='$title', avatar='$avatar', news='$news' WHERE id='$id'");
	$set = "2";
}
else if(empty($title) || empty($news))
{
	$set = "3";
}

echo	"<p><div class='create' style='width: 500px'>";

if($set == "2") {
   	echo "<p style='text-align: center'>The news with the ID of "$id" has been changed.</p>";
}
else if($set == "3") {
   	echo "<p style='text-align: center'>Please fill out the appropriate fields if you need to change anything.</p>";
}

echo	"</div></p>";

}

echo	"<form action='#' method='post'>
 <fieldset id='new'>
 <legend>You can edit news in here:</legend><br />";

if($_SESSION['id'] == 3 && $_SESSION['username']) {
$nsql = "SELECT id, user, subject FROM news WHERE user = '".$_SESSION['username']."'";
}
else if($_SESSION['id'] == 3 && $_COOKIE['user']) {
$nsql = "SELECT id, user, subject FROM news WHERE user = '".$_COOKIE['user']."'";
}

if($_SESSION['id'] == 5 && $_SESSION['username']) {
$nsql = "SELECT id, user, subject FROM news WHERE id > 0";
}
else if($_SESSION['id'] == 5 && $_COOKIE['user']) {
$nsql = "SELECT id, user, subject FROM news WHERE id > 0";
}

$nres = mysql_query($nsql) OR die(mysql_error());

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

mysql_query("DELETE FROM news WHERE id IN (" . implode(',', $_POST['delete']) . ")");
}

while($edit = mysql_fetch_array($nres)) {
	$del = $edit['id'];

	echo "<div id='edit'>";
	echo $del . ". ";
	echo "<input type='checkbox' id='$del' name='delete[]' value='$del' style='border: 0' />";
	echo "[" . $edit['user'] . "] ";
	echo "<a href='index.php?page=Account&do=editnews&id=$del'>";
	echo $edit['subject'] . "</a>";
	echo "</div>";

}

echo "<p><input type='submit' id='deleten' name='deleten' value='Delete Selected' tabindex='6' class='check' /></p>";

$esql = "SELECT news, subject, avatar FROM news WHERE id = '$id'";
$eres = mysql_query($esql) OR die(mysql_error());

echo	"<p style='color: #A2B5CD'>While editing, you must insert "<br />" for it show as a new line.</p>";

if($editid = mysql_fetch_array($eres)) {

	echo "<label for='title'>Title:</label>
	         <input type='text' id='title' name='title' tabindex='7' value='";
	echo $editid['subject'];
	echo "' class='check' style='width: 200px' /><br />";
	echo "<label for='avatar'>Avatar:</label>
	         <input type='text' id='avatar' name='avatar' tabindex='8' value='";
	echo $editid['avatar'];
	echo "' class='check' style='width: 188px' /><br /><br />";

	echo "<label for='idedit'>Edit News:</label><br />";
	echo "<textarea id='idedit' name='idedit' rows='15' cols='60' tabindex='9' class='check' >";
	echo $editid['news'];
	echo "</textarea><br /><br />";

	echo "<input type='submit' id='editnews' name='editnews' value='Edit News' tabindex='10' class='check' />
	         <input type='reset' value='Revert Back' tabindex='11' class='check' />";
}

echo	"</fieldset>
 </form>";

?>

Note this is an included script. session_start() is above the <html>. Hope this helps a bit more. Thank You!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.