简体   繁体   中英

Select an option from dropdown box using DOMCrawler in PHP

I am using DOMCrawler in PHP. I have the HTML below. I need to be able to select the option "Text1", and submit the form. I have the following code but I can't seem to make it work... What am I doing wrong?

use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://myURL');
$form = $crawler->selectButton('Text1')->form();   
$crawler2 = $client->submit($form);

This is the HTML:

<form action="something.php" name="frmOpcion" id="frmOpcion" method="post" enctype="multipart/form-data">

<select name="cmbOpcion" id="cmbOpcion" class="textoCmb">
<option value="a">Text1</option>
<option selected="selected" value="b">Text2</option>
</select>

<input type="image" name="imgOpcion" id="imgOpcion" alt="Send" title="Send" src="goTo.gif">

</form>

The documentation gives this example:

// Select an option or a radio
$form['country']->select('France');

To adapt the example to your situation, first select the form. Note that selectButton() is for buttons and inputs, not select controls:

$form = $crawler->selectButton('imgOpcion');

Next, set the value of the select:

$form->select('Text1');

Finally, submit the form:

$client->submit($form)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM