PHP 通过 JSON/Ajax 返回值
我目前正在开发 PHP CMS,并且陷入了进退两难的境地,即 CSS 和 AJAX。我试图解决的问题是如何让 AJAX/JSON 和 PHP 返回可以填充到表单中的值。
在我的其中一个页面上,我有一个带有选择列表的表单。当用户从列表中选择一个选项时,AJAX 脚本会请求 PHP 文件的响应,该文件将结果输出回浏览器。这工作正常。
然而,Jquery/CSS 不会在返回结果中设置特定元素的样式,例如复选框。
我想做的是向用户呈现一个表单,顶部有一个选择菜单,下面有空的输入字段。当他们从菜单中选择一个选项时,下面的表单字段将填充返回数据。表单本身由文本字段、文本区域和选择列表组成,我想让它们中的每一个都更新它们的值以匹配相应的返回数据。
首先……这可能吗?
其次...如果是的话,您能否为我指明程序语法流程的正确方向,或者我可以研究的示例脚本。
提前致谢。
Im working on a PHP CMS at the moment and am caught between a rock and hard place, those being CSS and AJAX. The issue that I'm trying to figure out is how to get AJAX/JSON and PHP to return values which can be populated into a form.
On one of my pages I have a form with a select list. When the user selects an option from the list the AJAX script requests a response from a PHP file which outputs results back to the browser. This is working fine.
However Jquery/CSS wont style specific elements such as checkboxes in the returned results.
What I would like to do instead is to present the user with a form with a select menu at the top and empty input fields beneath. When they select an option from the menu the form fields beneath are populated with the return data. The form itself is comprised of text fields, text-areas and select lists, and I would like to have each of them update their values to match the corresponding return data.
Firstly...is this possible?
Secondly...if its, could you point me in the right direction as to the pro-grammatical flow, or an example script that I can study.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,有可能,而且实施起来并不困难。假设您的 HTML 看起来像这样(这显然很难看;)):
您可以编写一个快速 jQuery ajax 请求,在
success
响应上填充表单数据,如下所示:在此处进行操作
注意,您的脚本将依赖于您正在将数据推送到(例如示例中输入文本、文本区域和选择之间的差异)。
Yep, possible and not horribly difficult to implement. Let's say your HTML looks something like this (this is obviously ugly ;)):
You could write up a quick jQuery ajax request that populates form data on
success
response, something like this:Fiddle here
Note that your script will be dependent on the element that you're pushing data into (like the differences between input text, textarea and select in the sample).
解决方案很简单:
pimpHTML
或其他函数)。.3。没那么难。只需创建一个函数
setHtml(obj, html)
(或其他东西),将响应 HTML 插入节点,然后重做pimpHTML
prep:
semi-inline:
显然还没有完成像这样,你必须自己改进和指定它。
Solutions is simple:
pimpHTML
or something)..3. is not so hard. Just create a function
setHtml(obj, html)
(or something) that inserts the response HTML into a node and then redoespimpHTML
prep:
semi-inline:
Obviously it's not done like this and you'll have to improve and specify it yourself.