Jump to content

Admin Interface With Checkboxes - How to connect it to a function?


astupiduser

Recommended Posts

Hello everybody,

 

I am working on an admin interface, where you have a list of database entries with checkboxes. In the end there are three drifferent buttons connected to three different functions. I need to get the id of the entry by clicking on the checkbox and then connect it somehow to the functions (functions are: delete, move to a database with different structure and send an e-mail). How do I pick up the id? And will irt work with more than one id? Here is my interface:

 

<?php

$result = mysql_query("SELECT * FROM db1 ORDER BY name1")or die ('Error: '.mysql_error ()); ?>

//This is the list with the checkboxes

<table><?php while($show = mysql_fetch_array($result)) {

$checkboxname=''.$show[id].'';?>

<tr><label> <td><input name="<?php echo''.$checkboxname.'' ?>" type="checkbox" /> </td>

<td><?php echo $show[name2]; ?> </td>

<td><?php echo $show[name1]; ?></td>

<td><?php echo $checkboxname; ?></td>

</label></tr>

<?php }

?></table>

//Those are the buttons I need to connet with the list

<form action="Copy_Test.php" method="post">

<input type= "submit" value="Move to db2"/>

</form>

<form action="Delete_Test.php" method="post">

<input type= "submit" value="Delete"/>

</form>

<p> </p>

<form action="Email.php" method="post">

<table width="200" border="1">

<tr>

<th><label>Date</label></th>

<th><input type="text" name="Date"/></td>

</tr>

<tr>

<td><label>Time</label></td>

<td><input type="text" name="Time"/></td>

</tr>

</table>

<br>

<input type="submit" value="E-mail" />

</form>

 

 

 

And here is the function "Delete" as an example:

 

 

$checkboxname=extract($_POST);

echo $checkboxname;//Has always the value 0 for some reason, although what I select has another id...

$myquery = "DELETE * FROM db1 WHERE id=''.$checkboxname.''";

$result=mysql_query($myquery);

mysql_close();

 

Well, this just doen't work... Can anybody help me?

 

Regards,

Veronika

Link to comment
Share on other sites

You can do it however you want, but I like to use radios instead since you are only going to be doing 1 action on a single row at a time. 

so you make each row have a 3 series radio


<input type="radio" name="action[".$row['id']."" value="delete" />
<input type="radio" name="action[".$row['id']."" value="email" />
<input type="radio" name="action[".$row['id']."" value="edit" />

and then on the second page you can then sort the array $_POST['action'] and then do all the deletes, edits, emails in groups via a loop that looks like

<?php
asort($_POST['action']);
foreach($_POST['action'] as $value){
if($value == "delete"){
#delete
}
}
foreach($_POST['action'] as $value){
if($value == "edit"){
#edit
}
}
foreach($_POST['action'] as $value){
if($value == "email"){
#email
}
}
?>

The same thing could be done in a single foreach using the if logic in a switch or if elseif case, however if you want to group by action this is easier.

Link to comment
Share on other sites

Hi, thanks for your reply! I tried to use that but it just doesn't work - asort($_POST['action']); - the 'action' is supposed to be an array but it is not...

 

Tried another thing - using the switch-function - but it doesn't work either... Here is the new code. What is wrong:

 

<?php

$result = mysql_query("SELECT * FROM db1 ORDER BY nname")or die ('Error: '.mysql_error ()); ?>

<form action="Deleting.php" method="post">

<table><?php while($row = mysql_fetch_array($result)) {

;?>

<tr><label> <td><input name="action[".$row[id]."] type="radio" value="1"/></td>

        <td><input name="action[".$row[id]."] type="radio" value="2"/></td>

    <td><?php echo $row[nname]; ?> </td>

                            <td><?php echo $row[vname]; ?> </td>

    </label></tr>

<?php }

?></table>

<input  type="submit" name="Submit" />

</form>

 

 

And this is the page with the functions:

 

echo $_POST['action'];// Doesn't give any value

 

switch($_POST['action']){

case '1':

$myquery = "DELETE * FROM db1 WHERE id=$row[id]";

$result=mysql_query($myquery);

echo 'Deleted '.$row[id];//Nothing happens of course, as the data is not posted for some reason...

break;

case '2':

echo '2!';

break;

default:

mysql_error();

 

 

But still, this is not what I want to do. I need 3 buttons - this is somehow user-friendlier and prevents the user from doing stupid stuff (if you have 3 columns with radio-buttons, you cannot delete a couple of users at the same time and you don't always see what you are doing at this moment...)

Link to comment
Share on other sites

Well, I have the following:

 

foreach($_POST['action'] as $key => $value){  //<-line 17

if($value == "delete"){

$myquery = "DELETE * FROM bewerbungen WHERE id=$key";

$result=mysql_query($myquery);

}else{

mysql_error();

}

}

 

And the following error:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Deleting.php on line 17

 

Link to comment
Share on other sites

$_POST['action'] is not an array. in fact, it's probably unset or empty. AFAIK, the action of a form is not passed because the script receiving the information IS the action. It would be like a form telling a script what the script's name is when the script already knows.

Link to comment
Share on other sites

Yes, I managed to solve the problem with the array and yes - the result is "array", which still doesn't help me. I continued working on my old idea - 3 buttons connected to 3 functions. This should be a trivial problem right? But I still cannot find a way of solving it...

 

<?php

$result = mysql_query("SELECT * FROM db1 ORDER BY nname")or die ('Error: '.mysql_error ()); ?>

<form action="Delete_Test.php" method="post">

//This is the list with the checkboxes

<table><?php while($show = mysql_fetch_array($result)) {

$checkboxname=''.$show[id].'';?>

<tr><label> <td><input name="$checkboxname" type="checkbox" /> </td>

<td><?php echo $show[nname]; ?> </td>

<td><?php echo $show[vname]; ?></td>

<td><?php echo $checkboxname; ?></td>

</label></tr>

<?php }

?></table>

<input type= "submit" value="delete"/>

</form>

 

 

and then the post page:

 

$key=$_POST['$checkboxname'];

echo $checkboxname;// nothing happens

$myquery = "DELETE * FROM db1 WHERE id=''.$checkboxname.''";

$result=mysql_query($myquery);

Link to comment
Share on other sites

the way i handle multiple buttons to the same form is to determine which was pressed. say i have 3 submit buttons named btnInsert, btnUpdate and btnDelete, each with a value "Insert", "Update" and "Delete":

 

       if ($_POST["btnInsert"] > "") {
	// INSERT A NEW RECORD

} else if ($_POST["btnUpdate"] > "") {
                // Update existing record

} else if ($_POST["btnDelete"] > "") {
                // Delete record

}

 

if your form can submit when someone hits return in a text field, set a default action for

 

if ($_SERVER['REQUEST_METHOD'] == "POST")

Link to comment
Share on other sites

Hey, thanks a lot for the reply! Think this is going to work very well. I still don't know how to tell my post-page what I have selected... Does anybody know a solution for that?

 

<?php

$key=$_POST['$checkboxname'];

if ($_POST["delete"] > "") {

echo $key; // Gives back "on", not very helpful

$myquery = "DELETE * FROM db1 WHERE id=$key";

$result=mysql_query($myquery);

 

} else if ($_POST["copy"] > "") {

                echo 'Hello 1!';

 

} else if ($_POST["email"] > "") {

                echo 'Hello 2!';

 

}

?>

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.