This topic is locked

Calling Stored Procedure from Ajax

9/12/2021 8:16:49 AM
PHPRunner General questions
L
Lance Spurgeon author

Not sure why I can't get the value to pass as paramater to my stored procedure

<button data-id="'.$row['id'].'" data-name="'.$row['template_number'].'" value="'.$row['boardname'].'" class="button temp-clone btn-clone btn btn-primary btn-icon" title="Clone '.$row['boardname'].' board only." data-original-title="Clone '.$row['boardname'].' only."><i class="typcn typcn-tabs-outline"></i></button>

Onload Event:
$("body").on("click",".btn-clone", function TemplateClone() {
var name = $(this).data('name');

$.ajax({
type: "POST",
url: "include/getData_TemplatesClone.php",
data: {name: name},
success: function(data){
swal({
position: 'top-end',
icon: 'success',
title: 'The template '+ name +' was cloned successfully',
showConfirmButton: false,
timer: 2500
})
}

});
});

Custom page called getData_TemplatesClone.php
<?php
include ("dbcommon.php");

$region = $_SESSION["region"];
$userid = $_SESSION["UserID"];

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

$sql = "CALL todoboards_templateClone('".$_POST['name']."', '".$userid."', '".$region."')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}

$conn->close();

}
?>

Any help appreciated :)

admin 9/12/2021

You need take some basic troubleshooting steps to figure out what doesn't work. You also need to provide more info if you need help, it is not clear right now what exactly doesn't work for you.

The first step is to figure out where it fails. Can be one of the following areas:

  1. Variable name is not being properly populated in Javascript before your AJAX call
  2. This variable is not being properly pass to PHP via AJAX call
  3. It properly passed to PHP but correct SQL is not being built or executed in your PHP file.

Narrow it down.