Jump to content

Checkbox to delete or edit


tinkertron

Recommended Posts

I want to take this moment to thank alot of you for giving me the courage to dive into this head first on learning php/mysql. I'm getting very near to getting my first online database up and running. Your more then welcome to see what I have learn so far! Even test drive it yourself. http://tdcj.homeip.net/test.php ...

 

So here's my question: If I want to add check boxes next to the entries, would I have to make a special colunm in MySQL table, if I want to make it funcation to edit or delete that entry? I'm learning as I go... Thanks

Link to comment
Share on other sites

Nope you don't need anything in your database for a checkbox, just include it in your loop when you're pulling the info from the table.  For example:

 

$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{

// Your code for displaying the table rows here

?>

<input type="checkbox" name="delete[]" value="<?php echo $row['id']; ?>" />

<?php

}

 

Now see the checkbox name has [] after it, that's important.  It means that when the form is submitted, an array called "delete" will be available containing the unique IDs of the rows selected for deletion.  You can do the same with edit as well.

 

All it will be is a matter of then looping through that delete or edit array and deleting them with a MySQL query :)

 

Hope that makes sense.

Link to comment
Share on other sites

I think I follow but the code seem to be wacky to me,  so i'm going to post my code, and maybe you can show me:

 

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

 

Also where would the "link" or "button" appear to either delete or edit?

Link to comment
Share on other sites

In your main loop, which is:

 

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

 

is where you would put the checkbox, so change the above to this:

 

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><input type=\"checkbox\" name=\"delete[]\" value=\"" . $row['id'] . "\" /></td>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

 

Obviously you'll need to add a table header for the delete column.  You'll also need a <form> tag and a submit button :)

Link to comment
Share on other sites

You could handle the delete without a <form> and submit, by calling some javascript onclick of your checkbox, with the id in the js call, and then delete without a page reload. The record could then be marked as deleted, or with a little more work the row could be removed from the page.

 

If the row were simply marked as deleted then it would not be there the next time the page was loaded.

 

If you like, I can supply some example code to do this.

 

 

Link to comment
Share on other sites

Monkeh: You've changed the code twice, simlar to each other but I started seeing alot of backlashes "/" why the reason for this?

 

You went from this

<input type="checkbox" name="delete[]" value="<?php echo $row['id']; ?>" />

to this

echo "<td><input type=\"checkbox\" name=\"delete[]\" value=\"" . $row['id'] . "\" /></td>";

am I missing something here?

Link to comment
Share on other sites

There's absolutely no difference, just you posted your own code the second time so I showed you what your new code would look like.  I decided not to break your trend of echo-ing HTML using PHP so just changed it a little.  The backslashes allow you to escape quotation marks.

Link to comment
Share on other sites

" " " = php parsing error, as php had no way of knowing which " is the end quote.

" \" " = correct definition of a double quoted string having a double quote within the string.

it does take some time getting used to the \ character within strings (a bit harder to read in my opinion)

 

backslashes also used for many metacharacters \n \t \b for example

Link to comment
Share on other sites

jl5501

If you like, I can supply some example code to do this.
I would greatly appreciate this. and if you wouldn't mind can you also do this for edit? I would need the db to be as up to date as much as possible. I just notice that problem today, when I add a entry the db does not show the add information unless the user refresh the page  :'( it one thing after another. I did buy a php application from phpgrid.com but unable to get it to work. I have someone from the UK helping me. I made a new years resolution to quit smoking, but this project is not helping  :-\ , I need a simple but easy grid with the information as shown on the tabs, other then the database, the rest I can do, hopefully. As soon as this project is complete and I get the pat on my back, then I will quit smoking, and move on in my life.
Link to comment
Share on other sites

OK here are some code snippets of a system where I mark for deletion, and have a button on the page to delete marked records. As it sets a delete mark in the db, it can hold it until you run the delete or unset the delete mark

 

It can easily be changed to do the actual deletion, but for this particular customer, delete marking was what was required.

Bear in mind, I always use mysql_fetch_object to get database data so you will see me addressing fields in object notation.

In each table row, I have this at the end

 

<td class="body">

        <a href="ra_admin.php?id=<?php echo $ra->id?>">Edit</a>

</td>

<td class="body">

  <input type="checkbox" name="mdcb" onClick="markDelete(this,<?echo $ra->id?>)" <?php if($ra->markdelete==1) echo 'checked="checked"';?>>

</td>

 

ok and next you need the javascript function markDelete()

function markDelete(elm,id)

{

        var mark = elm.checked;

        var url="markfordel.php?id=" + id + "&delmark=" + mark;

//alert(url);

        http.open("GET",url, true);

        http.onreadystatechange=handleDelmark;

        http.send(null);

}

 

that makes an ajax call to markfordel.php which does this

 

$id = $_GET['id'];

$delmark = $_GET['delmark'];

if($delmark == 'true') $delmark = 1;

else $delmark = 0;

$qry = sprintf("update radata set mark_delete = %d where id=%d",$delmark,$id);

$res = mysql_query($qry);

 

You will notice that the javescript function does have a callback set, which will be executed when the php has finished, but I do not have anything currently in that function except for an alert to show me what the php did, which is commented out.

 

What you could have, is a div on the page which you place a nice message in

 

This is code from a live commercial system for a customer of mine, so I know it works

 

Link to comment
Share on other sites

jl5501 thanks for the posting. I will refer to this often as possible. Right now i'm running into a few bumps in the road, (if it's not one thing, it's another). I didn't know that I would be getting myself into this much trouble, but I am insisted on getting this project done, so that I can show it off to my employer and be done with it. I have invested to much time and money into this project that I wish it would be done by tomorrow, but it doesn't look that way. I wish I could hire a php/mysql css programmer and be done with it, but i'm limited on funds and so on. I bought this phpgrid from phpgrid.com and I though all my prays were answered, but it's still not  :'( Check out my current page that i'm working on http://tdcj.homeip.net/main.html (tell me what you think, also the grid on there is an example and not the correct content that I will be putting in.)

Link to comment
Share on other sites

That grid has a few nice features, but in my experience, changing the few things you need to, to get exactly what you want is exasperating.

 

But I am a freelance web coder and do things like that in a custom way for a living, so I am naturally biased :-)

 

Link to comment
Share on other sites

Hi

 

Yes I am available, but we had better discuss the details by PM or email or skype.

I would not want to be banned for advertising on the board.

 

I can be reached by email on john@javelindesign.com or skype as john.lodge3 or

yahoo as johnl5501

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.