skip to Main Content

Trying to update a delete flag in SQL Server through jQuery Ajax post method. I am not sure if the issue is with my PHP or the jQuery code. When I press the delete button nothing happens but the web dev tools do not indicate anything is wrong with the syntax.

Here is the HTML:

    <div class='dialog' id='editForm' title='Edit' >
        <form method='POST' id ='editFo' name = 'editFo' action='post' enctype='multipart/form-data'>

        </form>
    </div>`

Here is the html for the button:

    <button id="deleteFormbtn" >Delete</button>

Here is the jQuery code:

    $("#deleteFormbtn").click(function(){
       $.post ( "deleteRow.php", $("#editForm: input").serializeArray(), function(data){
            alert(data);
        });
    });`

Here is the PHP code:

`

require_once ('sql/connectionstring/connectionstring.php');
$conn = SQLServerConnection();  


if(isset($_POST['vendor'])){

    $loc_sql = "SELECT TOP 1 loc_id FROM <table> WHERE loc_name = ?";
    $parms = array($_GET['loc']);
    $loc = sqlsrv_query($conn, $loc_sql, $parms) or die (print_r ( sqlsrv_errors(), true));

    while ($q = sqlsrv_fetch_array($loc)){
        $loc_id = $q["loc_id"];
    }
    $username = $_POST['username'];
    $password = $_POST['password'];
    $comments = $_POST['comments'];
    $vendor_website = $_POST['website'];
    $vendor = $_POST['vendor'];


    $query = "UPDATE <table> SET <column> = '0' WHERE <column> = ?";

    $parms = $username;

    $result = sqlsrv_query($conn, $query, $parms) or die (print_r ( sqlsrv_errors(), true));

    sqlsrv_close($conn);

}
header('Location: <location>);

?>`

2

Answers


    • You need to pass the ‘vendor’ with the AJAX post request so PHP can recognize it when trying to access $_POST['vendor']
    • You also need to handle when “if(isset($_POST...) { .... }” isn’t fulfilled so it gets back with an error.
    • Focus more on the PHP part to send you a reply in JSON.
      Something similar to:

      $data = /** whatever you’re serializing **/;

      header(‘Content-Type: application/json’);

      echo json_encode($data);

    Login or Signup to reply.
  1. You need to add type="button" to your button, otherwise the page reloads and request will not be sent because it defaults to “submit”.

    <button type="button" id="deleteFormbtn" >Delete</button>
    

    Additionally, you are referencing the div not the form in your AJAX request. You don’t need the method or action attributes, AJAX handles that for you. Send the entire form instead of just the input.

    <div class='dialog' id='editForm' title='Edit' >
      <form id ='editFo' name = 'editFo' enctype='multipart/form-data'>
    
      </form>
    </div>
    
    $("#deleteFormbtn").click(function(){
      $.post ("deleteRow.php", $("#editFo").serialize(), function(data){
        alert(data);
      });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search