2

I have an HTML string inside a PHP variable. The string contains several input fields. How can I get the value of an input field by its ID?

$text = '<html><head></head><body><form action="" method="post"><label>Test</label><input type="text" name="test1" id="test1" value="Value Test" /><label>Test 2</label><input type="text" name="test2" id="test2" value="Value Test2" /></form></body></html>';
3
  • You want the value attribute of a specific input while it's in that string? Which specific input? Commented Jan 22, 2014 at 19:45
  • I'm not submitting the form, I got him in a need of a php variable and pick up some things, like, think that has a hidden field with a code for the form work and I need to get that code, which came from a site that already has this form, to put it on my page, example: "<input type='hidden' id='formID' value='12345' />" Commented Jan 22, 2014 at 19:49
  • I need some codes that are within "hidden input" so that I can download a form that is ready, I picked up within an iframe to put in another form that stylized my way, but I need to do this with pure PHP without using any plugin installed on my site Commented Jan 22, 2014 at 19:58

4 Answers 4

2

From what I can tell, you have an HTML string and you want to parse it as HTML, then find an element by its ID.

If you don't want to learn the somewhat initially confusing syntax for XML manipulation or write a probably convoluted regular expression, you could use a tool like phpQuery.

An example would look like:

$html = phpQuery::newDocument($text);
$result = pq($html)->find("#id");
Sign up to request clarification or add additional context in comments.

Comments

2

One possibility is to use PHP Simple HTML DOM.

Here's an example for finding an input element by ID and printing its value:

<?php

$text = '<html><head></head><body><form action="" method="post"><label>Test</label><input type="text" name="test1" id="test1" value="Value Test" /><label>Test 2</label><input type="text" name="test2" id="test2" value="Value Test2" /></form></body></html>';

// Create DOM from URL or file
$html = str_get_html($text);

foreach($html->find('input') as $element) {
    if($element->id == "test1") {
        echo "Element: #".$element->id." has value: ".$element->value;
    }
}

?>

1 Comment

phpQuery is prettier ;)
1

$_POST['test1'] $_POST['test2']

Of course you will need to use these inside of your php code, depending on what you are doing. My example will post the results to your action page.

<?php echo("Your name is ".$_POST['test1']."<BR>Your Age is ".$_POST['test2']); ?>

2 Comments

it does not solve, I'm not submitting the form, I got him in a need of a php variable and pick up some things, like, think that has a hidden field with a code for the form work and I need to get that code, which came from a site that already has this form, to put it on my page, example: <input type='hidden' id='formID' value='12345' />"
Sorry my adhd makes me skim quickly through things, and I often miss them Just assign your hidden value to a variable IE varHidden = $_POST['formID']; This will allow you to fetch the variable for what ever portion of the code you wish.
1

While regular expressions are not the preferred method for parsing HTML, this is my solution:

<?php
$text = '<html><head></head><body><form action="" method="post"><label>Test</label><input type="text" name="test1" id="test1" value="Value Test" /><label>Test 2</label><input type="text" name="test2" id="test2" value="Value Test2" /></form></body></html>';

$id = 'test1';
preg_match_all("/<input type=\"text\"(.*)id=\"$id\" value=\"(.*?)\"(.*)>/",$text,$matches);

$value = '';
if(isset($matches[2][0])){
    $value = $matches[2][0];
}

echo 'Value: '.$value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.