Hi Everyone!
I've got a php script I'm trying to run to update a SQL Server table from a form that has not been submitted yet. The behavior I'm expecting is, someone will update the forms table that was filled by a lookup and then there is a button that will update the table by running this php script. The form is set to refresh every 2 mins. Here is the code that I have in forms and the php script.
$("#updateButton").click(function(){ var dataArray = [ {uid: 1, status: "active"}, {uid: 3, status: "active"}, {uid: 5, status: "complete waiting processed"} ]; $.ajax({ url:"berkupdsql.php", type: "POST", data: {dataArray: dataArray}, success: function(response) { alert(response); //display response from PHP script }, error: function(xhr, status, error){ console.error(xhr.responseText); } }); });
<?php //Database connection parameters $serverName = "LF-Test\Laserfiche"; $database = "LF_Resources"; $uid = "phpuser"; $pass = "********"; $connectionOptions = [ "Database" => $database, "Uid" => $uid, "PWD" => $pass ]; //Establish the connection $conn = sqlsrv_connect($serverName, $connectionOptions); //Check connection if(!$conn){ die("Connection failed: ".sqlsrv_errors()); } //Retrieve data from AJAX request $dataArray = $_POST['dataArray']; //Loop through the array and update the table foreach ($dataArray as $data) { $uid = $data['uid']; $status = $data['status']; //SQL update query $sql = "UPDATE berkshiredailyappointments SET Status = ? WHERE UID = ?"; //Prepare and execute the query $stmt = sqlsrv_prepare($conn, $sql, array(&$status, &$uid)); if (sqlsrv_execute($stmt) === false) { die(print_r(sqlsrv_errors(),true))'' } sqlsrv_free_stmt($stmt); } //Close the connection sqlsrv_close($conn); echo "Data updated successfully."; ?>
When I click the button I get the following alert:
Any help would be greatly appreciated. Thanks!