开发者

Is it possible to fill in an Ajax form programatically using php?

开发者 https://www.devze.com 2022-12-16 19:19 出处:网络
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.

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


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 :

  • Begin by getting the lists of data (by using the form in a browser, for instance)
  • Know how those data have to be used (by observing the "normal" way the form works)
  • POSTing the correct values, with your curl request.


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.


If it is a simple html based form, it can be easily filled with cURL

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消