0

How to select any element inside specific element tag by class. I need to retrieve attribute "name" value from "input" element that inside element "form". In example. there're 2 forms elements named _arrived and _class. I need to retrieve all attribute name value from form _class

   $source = '
    <form method="post" action"arrived.php" class="_arrived">
    <input type="hidden" name="type1" value="value1" autocomplete="off">
    <input type="hidden" name="type2" value="value2" autocomplete="off">
    <input type="hidden" name="type3" value="value3">
    <input type="hidden" name="type4" value="value4">
    </form>

    <form method="post" action="destiny.php" class="_class">
    <input type="hidden" name="type1" value="value1" autocomplete="off">
    <input type="hidden" name="type2" value="value2" autocomplete="off">
    <input type="hidden" name="type3" value="value3">
    <input type="hidden" name="type4" value="value4">
    <input type="hidden" name="type5" value="value5">
    <input type="hidden" name="type6" value="value6">
    <input type="hidden" name="type7" value="value8">
    <input type="hidden" name="type8" value="value9">
    </form>';


$dom = new DOMDocument();
$dom->loadHTML($source);

$xpath = new DOMXpath($dom);
$items = $xpath->query('//form[@class="_class"]');

 $form = $items->item(0);

 $element = $form->getElementsByTagName("input");

 foreach($element as $elemen){
     foreach($elemen as $value){
         echo $value->getAttribute('name').'<br>';
     }
 }
David Brossard
  • 13,584
  • 6
  • 55
  • 88
IOSBET
  • 61
  • 1
  • 1
  • 6
  • @AnantSingh---AlivetoDie no result just output white blank page. – IOSBET Jul 17 '19 at 04:57
  • `action"arrived.php"` is invalid, you should be getting `Warning: DOMDocument::loadHTML(): error parsing attribute name in Entity` – Amadan Jul 17 '19 at 04:59
  • Apart from that do:- `foreach($element as $elemen){ echo $elemen->getAttribute('name').'
    '; }` you will get names
    – Alive to die - Anant Jul 17 '19 at 05:02
  • @AnantSingh---AlivetoDie: I think OP wants all attributes, so another loop over `$elemen->attributes` is needed. – Amadan Jul 17 '19 at 05:03
  • 1
    @Amadan What is it means? that error? and how to solve that? – IOSBET Jul 17 '19 at 05:04
  • 2
    It means that your HTML is bad. `action"arrived.php"` should be `action="arrived.php"`. – Amadan Jul 17 '19 at 05:04
  • Furthermore, you could simplify by a couple of lines - there is no need for both XPath and `getElementsByTagName`, you can get what you want just with this XPath: `'//form[@class="_class"]/input'` – Amadan Jul 17 '19 at 05:06

4 Answers4

0

Why do you have loops on the elements? This should work:

...

$elements = $form->getElementsByTagName("input");

foreach($elements as $e){
  echo $e->getAttribute('name').'<br>';
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Sven Mich
  • 227
  • 2
  • 9
0

you need to have only single loop

foreach($element as $elemen){
    echo $elemen->getAttribute('name').'<br>';
 }

and you will get all name attributes.

Akhilesh
  • 927
  • 1
  • 6
  • 21
0

You can select the actual name attribute from the input elements directly rather than having to go through several calls to different methods and then just loop over them and output the text content.

Here the XPath expression first selects the form your after and then will pick out each input element and finally using @name will select the name attribute...

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($source);

$xpath = new DOMXpath($dom);
foreach($xpath->query('//form[@class="_class"]//input/@name') as $value){
    echo $value->textContent.'<br>';
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
-1

1.First you have to iterate over $elements one time

2.You have to use action="arrived.php" instead of action"arrived.php" to remove warning from your code which is:

Warning: DOMDocument::loadHTML(): error parsing attribute name in Entity, line: 2 in /in/tl53u on line 24

Do like below:

$dom = new DOMDocument();
$dom->loadHTML($source);

$xpath = new DOMXpath($dom);
$items = $xpath->query('//form[@class="_class"]');

 $form = $items->item(0);

 $element = $form->getElementsByTagName("input");

 foreach($element as $elemen){
    echo $elemen->getAttribute('name').'<br>';
 }

Output:-https://3v4l.org/Ma8bg

You can ignore warnings by using libxml_use_internal_errors(true);, but best is to solve them. Check reference below:-

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Problem is that the HTML isn't always under their control, it is more likely the result of code elsewhere and unfortunately resorting to `libxml_use_internal_errors(true);` is a common thing. – Nigel Ren Jul 17 '19 at 09:18
  • @NigelRen agreed. But at least OP can look to the code once to see if he found any issue at first glance or not? We cannot blame others when we are doing copy-paste business. It's our responsibility to solve all issues coming after copy-paste. – Alive to die - Anant Jul 17 '19 at 10:35