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
$_POST['vendor']
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);
You need to add
type="button"
to your button, otherwise the page reloads and request will not be sent because it defaults to “submit”.Additionally, you are referencing the
div
not theform
in your AJAX request. You don’t need themethod
oraction
attributes, AJAX handles that for you. Send the entire form instead of just theinput
.