douche3791 2010-11-22 12:07
浏览 38
已采纳

获取php文件值

We have 5 inputs (type="text") on html page /edit_person.php:

<input id="name" name="name" value="" />
<input id="surname" name="surname" value="" />
<input id="age" name="age" value="" />
<input id="begin" name="begin" value="" />
<input id="end" name="end" value="" />

And a php file /scripts/db.php

If we open in browser /scripts/db.php?get=1 it will give something like (an array with values):

$data = Array(
[name]=>Mark,
[surname]=>Twain,
[age]=>74,
[begin]=>November 30, 1835,
[end]=>April 21, 1910
)

How can we request /scripts/db.php?get=1 by ajax on /edit_person.php and insert received data to the inputs?

jQuery last version is used and PHP 5.2.

If input currently has some value, it should be replaced.

Output format of db.php can be changed, you can request options which would be better.

Thanks.

  • 写回答

1条回答 默认 最新

  • doufen3786 2010-11-22 12:09
    关注

    db.php seems to be outputting the array in some custom, PHP-like format. I wouldn't do that. Use json_encode() instead: jQuery's Ajax can handle JSON data natively.

    • Docs on jQuery.getJSON() with examples

    • If your JSON data is in a variable named data, the insertion into each input will be a simple $("#name").val(data.name);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题