是否可以使用 php 以编程方式填写 Ajax 表单?
我正在使用 php 来填写表格。现在,表单的许多字段都使用了 ajax。
例如
选择[国家] (ajax 将显示填充该国家/地区州的下拉菜单)
选择 [州] (ajax将显示填充有城市的下拉菜单)
选择[城市] (ajax将启用提交按钮)
如果它是一个简单的基于html的表单,则可以轻松地用cURL填充。但是如果表单使用 ajax 填充下拉字段怎么办?
谢谢
I am using php to fill up a form. Now, it so happens that form is using ajax for many of its fields.
e.g.
select [country]
(ajax will show drop-down filled with states for that country)
select [states]
(ajax will show drop-down filled with cities)
select [city]
(ajax will enable a submit button)
If it is a simple html based form, it can be easily filled with cURL. But what if the form is using ajax to populate the drop-down fields.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用curl“填充”表单时,您实际上是在发布本来要在表单中键入/输入的数据。
不需要发出 Ajax 请求,只要您知道必须使用哪些数据即可。
因此,解决方案是:
最后,您的curl请求应该与提交表单时浏览器发出的请求相同——独立于之前发送的Ajax请求(这些请求仅对获取数据)
嗯,除非 Ajax 请求实际上在服务器上“写入”某些内容,但这种情况很少见。
When you are "populating" the form with curl, you are actually POSTing the data that would have been typed/entered into the form.
There is no need for the Ajax requests to be made, as long as you know what data you have to use.
So, the solution would be to :
In the end, your curl request should be the same than the one made by the browser when the form is submitted -- independantly of the Ajax requests that are sent before (those are only useful for getting data)
Well, that is unless the Ajax requests are actually "writting" something on the server -- but that's pretty rare for this kind of situation.
我不清楚你在这里的意思。据我所知,cURL是一个发出HTTP请求的工具。它不能“填写表格”(与 WWW::Mechanize 等不同)。我在这件事上有错吗?
我认为您的意思是:“如果它是一个简单的基于 HTML 的表单,我可以使用 cURL 轻松构建 HTTP 请求,该请求提交与使用浏览器相同的查询字符串或 POST 数据。”我将继续这个假设。
Ajax 的使用(就其本身而言)不会阻止您手动构建表单数据并正常提交它。它只会让计算出您需要提交哪些数据变得更加困难。
如果您没有以正确的顺序请求所有数据位,则远程系统可能会崩溃(例如,如果您在最后提交完整的数据而没有请求城市列表,则远程系统可能会崩溃)一个国家)。强调“可能”,这不是实施该系统的明智方式。
无论如何,您可能还想使用 cURL 发出多个请求,以便可以获取城市列表(以及可能与它们关联的任何 ID)并以编程方式访问它们。
I'm not clear what you mean here. As far as I know, cURL is a tool for making HTTP requests. It can't "fill forms" (unlike, for example, WWW::Mechanize). Am I wrong about this?
I think you mean: "If it is a simple HTML based form, I can easily construct an HTTP request using cURL that submits the same query string or POST data as using a browser would." I'm going to proceed on that assumption.
The use of Ajax (in of itself) doesn't stop you constructing a the form data manually and submitting it as normal. It just makes it a little more difficult to work out what data you need to submit.
The remote system might be implemented in such a way that it falls over if you don't request all the bits of data in the right sequence (e.g. it will barf if you submit the complete data at the end without requesting the list of cities for a country). Emphasis on 'might', this wouldn't be a sane way to implement the system.
You might also want to make multiple requests with cURL anyway so that you can fetch the list of cities (and any ids that might be associated with them) and access them programatically.