1

I am new to PHP. So i first apologize if i am using dumb strategy. I am working on PHP site. my page queries data from MySql and shows in html table along with me checkboxes. User can Delete any data using check boxes and button. this all is done using javascript. Here is my code to delete data from html table

<script type="text/javascript">
function deleteRow() {
        try {
            var table = document.getElementById("Categories");
        var rowCount = table.rows.length;



        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
                table.deleteRow(i);
                rowCount--;
                i--;
            }


        }
        }catch(e) {
            alert(e);
        }
    }

</script>

on clicking Save button MySql update query should run to save all changes made to page. I know this can be done by PHP. but problem here is how i can get data from html in php to run the query?? if any one can point me to right direction, i will be thankful to you.

Thanks.

6
  • What about using DOM ? take a look at this tutorial about HTML DOM : w3schools.com/htmldom/default.asp Commented Jul 6, 2012 at 11:19
  • Do you mean you want to send the current DOM - i.e. the current live state of the HTML page - as a string to your PHP script? Commented Jul 6, 2012 at 11:21
  • @utkanos yes i am trying to do so with. want to send current live state html of my page. Commented Jul 6, 2012 at 11:23
  • @pheromix thanks for the article. let me read that :) Commented Jul 6, 2012 at 11:24
  • OK - but the title of your question said you wanted to send it to a PHP array. What did you mean here? Sending the page to PHP is simple enough but not sure what you mean with the array part. Commented Jul 6, 2012 at 11:24

1 Answer 1

2

You can make a form where you'll have checkboxes like: <input type="checkbox" name="m_Name[]" value="id_of_element">. When you submit this form on server-side you can do:

foreach ($_POST['m_Name'] as $id){
    $query = 'DELETE FROM table WHERE id='.$id;
    //execute query with mysqli_*, PDO or mysql_*; NOTE: if you are not doing educational project don't use last one.
}

But better approach will be:

$query = 'DELETE FROM table WHERE id IN (';
$in = '';

foreach ($_POST['m_Name'] as $id)
    $in .= $id.',';

$in = substr($in, 0, strlen($in)-1).')'; // removes extra "," at the end.
$query .= $in;
//run the query.

The last one wil execute only one query to delete notes so it's better for performance. But the way I've created query seems ugly to me but I am very tired to thing about improvements. :)

Sign up to request clarification or add additional context in comments.

5 Comments

Probably want to sanitize your id inputs ... Using intval should help.
@BrianAdkins in $_POST values are treated as strings and I concatenate them with query string. Why do I need to use intval? You would be right if I were using prepared statements and binding these values as integers, but for this purposes it would be extra, IMO.
Your sample code is currently vulnerable to SQL injection... Example, someone submits a POST with an id set to a single quote instead of a number.
@BrianAdkins I know that it is, but for educational purposes easy to use. There're lots of tutorials about PDO, mysqli_, he can find, by the way. :)
of course... Just trying to make sure a self-described php newbie gets at least a hint that the inputs should be sanitized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.