0

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)

http://jsfiddle.net/54L7t/3/

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

user1844933
  • 3,296
  • 2
  • 25
  • 42
Aero204
  • 99
  • 10
  • 1
    Since when does PHP not know how to communicate (or rather 'translate' )? Use json_encode() and json_decode() if you're going to communicate with your frontend using JSON. Beware of some UTF-8 translation issues, but with most requests you won't have a problem. – Raphioly-San Mar 28 '14 at 07:26
  • I not used JSON, could you elaborate? – Aero204 Mar 28 '14 at 07:27
  • you should echo the json_encode of the array and parse it in your js – rakeshjain Mar 28 '14 at 07:27
  • 1
    @Aero204: Search for "send php to javascript site:stackoverflow.com" on A Famous Web Search Engine. – T.J. Crowder Mar 28 '14 at 07:28
  • 1
    A totally different subject - you should be aware that you are open to SQL injection. The value $sign_location is taken directly from a user input. Make sure to fix this as well http://en.wikipedia.org/wiki/SQL_injection#Technical_implementations – Ronnie Jespersen Mar 28 '14 at 07:30
  • @T.J.Crowder not fully the same. It does describe how you can use PHP data in JavaScript, but not how to communicate. He needs to communicate. – Raphioly-San Mar 28 '14 at 07:33

2 Answers2

3

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.

slash197
  • 9,028
  • 6
  • 41
  • 70
  • If you set your php header the right way, you needn't worry about jQuery trying to evaluate it as JSON. – Raphioly-San Mar 28 '14 at 07:29
  • Will i not need to decode the json? – Aero204 Mar 28 '14 at 07:30
  • @Aero204 jQuery does that for you – slash197 Mar 28 '14 at 07:33
  • at the moment, it sort of works, but its not what i'm aiming to achieve: data: object 0: "[{"lx":80,"ly":41,"mx":80,"my":40},{"lx":80,"ly":40,"mx":80,"my":41}...]" Still doesn't work... when i setup a var and put the array within the far it works fine. – Aero204 Mar 28 '14 at 07:36
  • My object is only 1 length with this method, i should have a length of 42. – Aero204 Mar 28 '14 at 07:49
  • Then your array is not properly formatted in php. Most likely `data[0]` contains the array you are looking for. – slash197 Mar 28 '14 at 11:29
0

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)
Zharktas
  • 104
  • 1
  • 1
  • 7
  • Ive just tried this, i seem to get the following: 'data: object 0: "[{"lx":80,"ly":41,"mx":80,"my":40},{"lx":80,"ly":40,"mx":80,"my":41}...]"' i need the data to be put in separately. it still doesn't see this data as an array. – Aero204 Mar 28 '14 at 07:52
  • I thinking its something to do with my select statement, the data needs to be split... i dont think thats happening. – Aero204 Mar 28 '14 at 07:58
  • Of course you need to format it in php first to suitable structure and the echo the result to the browser. – Zharktas Mar 28 '14 at 08:44