1

I have a form that allows a user to paste the code for an HTML form into a textarea. I then store the form they pasted in a string.

Before I echo that form back into my page template I want to change the value of the submit button.

Currently I am using jQuery to do this, but I would like to do it server side first.

Here is my jQuery Code:

    $("input[type='submit']").attr("value","my new value");

How would I search the string for that value using PHP and switch it out before the form is printed?

UPDATE: To clarify, I am trying to search a string for:

<input type="submit" value="old value" />

and replace it with

<input type="submit" value="new value" />

the string has a whole form inside it like this:

$string = '<form>
<input type="text" value="any">
<input type="submit" value="old value">
</form>';

1 Answer 1

1

You can do that with DOMDocument:

$dom = new DOMDocument;
$dom->loadHTML($form);
$xpath = new DOMXPath($dom);
$input = $xpath->query('//form/input[@type="submit"]')->item(0)->attributes;

foreach($input as $item){
    if($item->name == 'value'){
        $item->value = 'New Value';
    }
}

echo $dom->saveHTML();
Sign up to request clarification or add additional context in comments.

1 Comment

That works perfect! Thanks for the detailed description it was very easy to follow.

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.