This question already has an answer here:
Hi i'm looking for a bit of help. I am using Signature Pad by Thomas J Bradley. What I'm trying to do is store the output of the signature into a database then call it when needed.
Step 1
Store output information into a database. (complete)
Save_sign.php
<?php
include 'info.php';
$con=mysqli_connect($host,$username,$password,$db_name);
$sign_data = $_REQUEST['output'];
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO sign (data)
VALUES
('$sign_data')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Note: the information stored within the database is an array.
example: [{"lx":80,"ly":41,"mx":80,"my":40},{"lx":80,"ly":40,"mx":80,"my":41}...]
Step 2
Calling the data from the database and passing it to my ajax command (btnGet). The problem I'm having is that the data within the field, when retrieved turns into an array, which i need to pass to my regenerate function:
$('.sigReturn').signaturePad(ReadOnly).regenerate(data);
PHP does not allow me to return the array, heres what im currently using:
return_sign.php
<?php
include 'info.php';
$con=mysqli_connect($host,$username,$password,$db_name);
$sign_location = $_REQUEST['value'];
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT data FROM sign WHERE SignID='$sign_location'");
while($row = mysqli_fetch_array($result))
{
echo stripslashes(implode("", $row)); //i know that the implode turns it to string, was the only way i could get the data to my ajax command.
}
mysqli_close($con);
?>
So how would I pass the data to my ajax command, avoiding the array to string conversion error from php?
Thanks
</div>
Take your array and encode it to json with
echo json_encode($array);
then modify your ajax to process it, add the dataType field
dataType: "json"
Now your data
variable from the success function is a javascript object you can use.
You could return it as JSON and let jQuery parse it to JavaScript object in the browser code. So instead
echo stripslashes
You would encode it
echo json_encode($array)