0

So I have a table that pulls data from a DB and I'm trying to add a Remove/Delete button but it's not working and I can't really work out why.

JS Code

$(document).on('click', '.removeIteam', function (event) {
    var $iteamID = $(this).find('span').text();
     $.ajax({
        type: "POST",
        url: "removeIteam.php",
        data: { BoxID: $iteamID }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });      
});

PHP Code

<?php
    require_once('connent.php');
    function abc($BoxID){
        $sql = 'DELETE FROM client_info WHERE BoxID=$BoxID';
        $retval = mysql_query( $sql, $conn );
    }
 ?>
Ryan J
  • 51
  • 7

3 Answers3

1

You have to execute the PHP function. Furthermore I edited your code to be more secure and less vulnerable to SQL injections. However an even better option is to use MysqlI or PDO, like comments have already pointed out. I didn't want to make these modifications since I do not know the value of $conn.

JS Code

$(document).on('click', '.removeIteam', function (event) {
    var $iteamID = $(this).find('span').text();
     $.ajax({
        type: "POST",
        url: "removeIteam.php",
        data: { BoxID: $iteamID }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });      
});

PHP Code

<?php
    require_once('connent.php');
    $box_id = isset($_POST['BoxID']) ? mysql_real_escape_string($_POST['BoxID']) : null;
    if($box_id == null) die("Missing BoxID.");

    $sql = "DELETE FROM client_info WHERE BoxID='".$box_id."'");
    $retval = mysql_query( $sql, $conn );
 ?>
Pieter De Clercq
  • 1,951
  • 1
  • 17
  • 29
  • I have tried with your edit's and I'm still getting an error `500 (Internal Server Error)` Any idea why? – Ryan J May 29 '17 at 11:36
  • @RyanJ try putting `error_reporting(-1); ini_set('display_errors', 'on');` somewhere on top of your page and check the error logs. If you are using PHP7 then you cannot use `mysql_` functions since they were removed. – Pieter De Clercq May 29 '17 at 11:48
  • @RyanJ have you checked the your PHP error logs? The location of the log file is different depending on how you installed php. For example, if you are using XAMPP or WAMP on Windows, the default location is `C:\Windows\Temp` –  May 29 '17 at 11:51
  • @thepieterdc When I added the code to show the errors nothing is happening other then the `500 (Internal Server Error)` showing in the console and it's the same when I use `mysqli_` – Ryan J May 29 '17 at 12:13
-2

make sure you stored the posted value in $boxID And $sql = 'DELETE FROM client_info WHERE BoxID=$BoxID'; $retval = mysqli_query( $conn, $sql );

Joseph
  • 11
  • 8
  • Your sentence explaining what to do seems alright but the code you posted doesn't do what you suggested. –  May 29 '17 at 11:29
  • btw, i just wanna say that dont use deprecated syntax. thats all – Joseph May 29 '17 at 11:31
  • How do I do that? And is that not what `data: { BoxID: $iteamID }` is doing ? – Ryan J May 29 '17 at 11:32
  • 1
    remove $ sign. its a php syntax. var {variable name} is enough in jscript – Joseph May 29 '17 at 11:33
  • @joseph also, don't just use the `mysqli_` function without explaining that you are doing that. (Even if it is a good call) –  May 29 '17 at 11:34
  • There is no need to remove the `$` from the Javascript variable name. –  May 29 '17 at 11:46
-3

Please remove the $ (dollar sign) before the var iteamID.

$(document).on('click', '.removeIteam', function (event) {
    var iteamID = $(this).find('span').text(); //instead of var $iteamID = $(this).find('span').text();
     $.ajax({
        type: "POST",
        url: "removeIteam.php",
        data: { BoxID: iteamID } //instead of data: { BoxID: $iteamID }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });      
});
Josiah
  • 118
  • 10
  • 1
    Why? The `$` is perfectly valid. –  May 29 '17 at 11:26
  • It is not. Please try it in your code editor. This is javascript (jquery) not php – Josiah May 29 '17 at 11:28
  • 1
    [Prepare to learn something new](https://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names) –  May 29 '17 at 11:33
  • See how its used: https://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign – Josiah May 29 '17 at 11:34