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

6
  • 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. Commented Mar 28, 2014 at 7:26
  • I not used JSON, could you elaborate? Commented Mar 28, 2014 at 7:27
  • you should echo the json_encode of the array and parse it in your js Commented Mar 28, 2014 at 7:27
  • 1
    @Aero204: Search for "send php to javascript site:stackoverflow.com" on A Famous Web Search Engine. Commented Mar 28, 2014 at 7: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 en.wikipedia.org/wiki/SQL_injection#Technical_implementations Commented Mar 28, 2014 at 7:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

If you set your php header the right way, you needn't worry about jQuery trying to evaluate it as JSON.
Will i not need to decode the json?
@Aero204 jQuery does that for you
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.
My object is only 1 length with this method, i should have a length of 42.
|
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)

3 Comments

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.
I thinking its something to do with my select statement, the data needs to be split... i dont think thats happening.
Of course you need to format it in php first to suitable structure and the echo the result to the browser.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.