You are viewing limited content. For full access, please sign in.

Question

Question

Run PHP Script From Forms

asked on May 15, 2024

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!

0 0

Replies

replied on May 17, 2024

I'm still having some issues with this. I tried doing a .cshtml script instead and I'm getting a similar "error" with the html code in the alert window.

0 0
replied on May 29, 2024

What are the contents of that pop-up? It looks like you're getting back a custom error page.

0 0
replied on May 29, 2024

This is the full error that shows up.

0 0
replied on July 26, 2024

Checking in on this one Miruna to see if you have any ideas on what's happening here. Thanks!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.