2

I have a html file loaded as a string in php, and I need to get the values of the input elements in the HTML string. Can someone help me build a function which takes the name of the input element and returns its value?

This is an example of the function I would like to do:

function getVal($name){

    $htmlStr = "<form action = \"action.php\"><input type=\"hidden\" name=\"command\" value=\"123456\">
                <input type=\"hidden\" name=\"quantity\" value=\"1\">
                <input type=\"hidden\" name=\"user_mode\" value=\"1\">
                <input type=\"hidden\" name=\"stock\" value=\"-1255303070\">
                <input type=\"hidden\" name=\"id\" value="429762082">
                <input type=\"hidden\" name=\"pidm\" value=\"2\"></form>";

            // I'd like to get the value of $name here probably using preg_match     

            return $value; //returns 123456

        }

$val = getVal("command"); //val should be 123456.

any idas? Thank you

7
  • 4
    This would be easier if you just posted the form to your script. Commented Nov 27, 2012 at 19:00
  • 2
    Use DOMDocument to parse the HTML string and DOMXPath to query for the value you want. There are zillions of examples here in SO (both for HTML and XML, it's the same most of the time). Commented Nov 27, 2012 at 19:00
  • instead, use ajax to run a php file in javascript and pass the value of the input field you want. Commented Nov 27, 2012 at 19:00
  • stackoverflow.com/questions/1280767/… Commented Nov 27, 2012 at 19:01
  • What exactly are you trying to do? Like @PatrickJamesMcDougle said if you post the form to your page you can access the post variables and manipulate them anyway you want. Commented Nov 27, 2012 at 19:02

2 Answers 2

8

Here is an example with DOM:

$html = "<form action = \"action.php\">
  <input type=\"hidden\" name=\"command\" value=\"123456\">
  <input type=\"hidden\" name=\"quantity\" value=\"1\">
  <input type=\"hidden\" name=\"user_mode\" value=\"1\">
  <input type=\"hidden\" name=\"stock\" value=\"-1255303070\">
  <input type=\"hidden\" name=\"id\" value=\"429762082\">
  <input type=\"hidden\" name=\"pidm\" value=\"2\">
</form>";

$document = new DOMDocument();
$document->loadHTML($html);

$inputs = $document->getElementsByTagName("input");

foreach ($inputs as $input) {
  if ($input->getAttribute("name") == "id") {
    $value = $input->getAttribute("value");
  }
}

echo $value;
Sign up to request clarification or add additional context in comments.

Comments

1

Load the HTML in a DOMDocument, then a simple XPath query would to the trick:

$xpath = new DOMXPath($domDocument);
$items = $xpath->query('//*[@name="command"]/@value');
if ($items->length)
    $value = $items->item(0)->value;

1 Comment

$value = $xpath->evaluate('string(...)') can be more convenient if you don't really care about the difference between "no such element" and "empty value".

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.