0

I am trying to use jquery and ajax to remove the a row on the page and a record in the db but it's not working for me. The row on the page is removed successfully but the php file is not getting called. I just have an echo statement in the php to error check and that is not getting printed to the screen.

js file

function deleteLine(a,estimateNum,rowNum){
        var $rowcount = $("#desc_table tr").length;
        if($rowcount <= 2){
                alert("Cannot remove all the description lines.");
                return false;
        }
        else{
          if (confirm("Are you sure you want to delete?")) {
            $.ajax({
              type: "POST",
              url: "delete_desc_record.php",
               data: { estimate_num: estimateNum, row_num: rowNum },
              cache: false,
              success: function(){
                  a.closest('tr').remove();
                }
            });
          }
          else{
            return false;
          }
    }

delete_desc_record.php

<?php
include 'connect.php'; 
include 'set_variables.php';

$estimate_num = isset($_POST['estimate_num']) ? (int) $_post['estimate_num'] : 0;
$row_num = isset($_POST['row_num']) ? (int) $_post['row_num'] : 0;

echo 'in delete php file '. $estimate_num .' '. $row_num;

index.php

if(!empty($DESC)){
 foreach ($DESC as $i => $b) {
 ?>
 <tr>
 <td><input name="desc[]" type="text" value="<?php echo $DESC[$i]; ?>"></td> 
 <td> <input name="desc_hr[]" type="text" value="<?php echo $DESC_hr[$i]; ?>"</td>
 <td> <input name="desc_rt[]" type="text" value="<?php echo $DESC_rt[$i]; ?>" </td>
 <td><input name="desc_amt[]" type="text" value="<?php echo $DESC_amt[$i]; ?>"></td>
 <td><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteLine(this,<?php echo $estimate_num .','. $i; ?>)"></td>
  </tr>
  <?php
  }
}
1
  • Tip: for see the "output" you can check it on the "Network" tab on the Debugger Console. Or you can get the data on success callback and print it on console :) Commented Nov 9, 2017 at 21:13

2 Answers 2

3

You're php is expecting estimate_num and row_num but you are trying to give it

data: estimateNum, rowNum,

which is invalid syntax in any case. Change that line to:

data: { estimate_num: estimateNum, row_num: rowNum },
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Taplar...ok, I am using the php variable estimate_num and row_num in the index.php, but when I pass them to the js file, I thought I could call the variable a different name. So I tried your suggestion and it still is not echoing out the line from delete_desc_record.php. See anything else wrong by any chance?
You'd have to check the inputs in your script to make sure you are getting them. If you are, then it becomes a question of if you're sql is constructed properly, which I have no way of verifying. @jd0117
Yes I've verified the script is getting the inputs correctly (using alerts). I'm just doing an echo in my php page so it shouldn't matter if the SQL statement is correct just yet. I feel like there is something wrong with my ajax because it doesn't look like the php page is even getting called.
@jd0117 when I said "You'd have to check the inputs in your script" I was referring to your php script.
ok, thanks Taplar...I've taken everything out of my php script and just put an echo 'hi'; and even that is not printing.
0

Do you have any console errors in developer tools?

So you should read up on prepared statements to prevent Sql injection and your DELETE statement is incorrect. Remove the asterisk.

Update Just checking over your SQL query and cannot see where $cust_id is being set anywhere?

Maybe check your include files, they may be breaking the entire script. If they seem to be fine then just write an echo statement at the top of your file, refresh the page, do you get anything printed on screen? If yes then move the echo line down and repeat the steps of refreshing the page until you no longer get the echoed text.

If you have PHP errors on then you should see an error, make sure display_errors is set to 1 in your php.ini file.

5 Comments

no, there are no errors and it's dropping into the success function and removing the line on the page. Just not doing the echo on the delete_desc_record.php page.
Do you have php errors turned On? This may help you. error_reporting(E_ALL); ini_set('display_errors',1)
Thank you Ben, I've turned them on now but I'm not getting any errors thus far. That Delete statement silly mistake...but I will look into SQL injection prevention for sure.
@jd0117 No worries, here is a link from the PHP documentation about SQL Injection prevention. SQL Injection Prevention
Great, thanks Ben. I will take a look! Double checked and the display_errors are turned on in the ini file. It just seems like the php file is not getting called at all from the ajax statement. I should at least get a simple echo in the php file. I know the ajax is successful because the a.closest('tr').remove(); is completed. Can't seem to find the issue though. Very frustrating.

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.