dsy48837 2014-03-28 07:23
浏览 31
已采纳

如何将PHP数组转换为Javascript [重复]

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)

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

</div>
  • 写回答

2条回答 默认 最新

  • douhan9748 2014-03-28 07:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题