0

I am trying to send data from .js file to .php but it is not working.

I tried to search and find solution but anything didn't work for me. I guess there is issue in URL path but I am not sure. I am using win10, chrome, Netbeans and XAMPP.

ed.js :

function delete_row(id) {

    $.ajax({
        url: "modify.php",
        type: "POST",
        data: {delete_row: "deleted_row", row_id: id},
        success: function (data) {
            console.log(data);
        }
    });
}

modify.php :

<?php
$host = "localhost";
$user = "root";
$pass = "";
$databasename = "exDB";

$con = mysqli_connect($host, $user, $pass, "exDB");
echo "<script>alert('alert');</script>";

 if (isset(filter_input(INPUT_POST, 'delete_row'))) {
  $row_no = filter_input(INPUT_POST, 'row_id');
    mysqli_query($con, "delete from exDB where id='$row_no");
    echo "success";
    exit();
 }
?>

I even put alert in .php file but I am not getting it.

7
  • Remove the round brackets from the data Commented Oct 18, 2016 at 8:02
  • Have you checked what the request is actually doing in the console? It should give you an error code at the very least Commented Oct 18, 2016 at 8:02
  • do you know how the ajax mechanism works? Commented Oct 18, 2016 at 8:03
  • what is in your console? there some 500 error or 400 error for ajax url? Commented Oct 18, 2016 at 8:07
  • @KinjalMistry I did but still not working. Commented Oct 18, 2016 at 8:20

1 Answer 1

3

Just a simple typo

In your javascript you called this parameter deleted_row and in your PHP you test a variable called delete_row. So amend one so they match

if (isset(filter_input(INPUT_POST, 'deleted_row'))) {

When developing, specially if yo are developing on a LIVE Server where Error Reporting will of course be turned off, you should add these lines to sctipys that you are developing/testing

<?php 
   ini_set('display_errors', 1); 
   ini_set('log_errors',1); 
   error_reporting(E_ALL);

And if you are using the MYSQLI_ API also

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Errors then become much easier to identify and see

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

Comments

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.