jay20aiii Posted November 26, 2013 Share Posted November 26, 2013 Hi i'm new to PHP and the forum. I am working on a email script. In it I am using the following to highlight any Input values the user forgot to fill in. $html = file_get_contents("page.html"); $document = new DOMDocument(); @$document->loadHTML($html); if (empty($_POST['txtEmail'])) { $document->getElementById('email')->setAttribute('style', "color: #ff0000"); } else { $document->getElementById('txtEmail')->setAttribute('value', $_POST['txtEmail']); } print_r($document->saveHTML()); I have two questions... 1). I read print_r should be used primarily for debugging. Should I save the html changes to file and then navigate to that file.. Something like: $document->saveHTMLFile('somepage.html'); header('Location: http://www.domain.com/somepage.html'); Or is there in fact a better, or more suitable, way using only PHP? 2). In my snippet I can update the color of a Label element if a value is missing, or populate the previous Post value if it was present (but a different required value was missing). But how would I go about selecting the option of a Select element? e.g. if my html was: <label id="title"><span class="fieldname">Title:</span> <select id="txtTitle" name="txtTitle"> <option value="">Select</option> <option value="DR">Dr </option> <option value="MR">Mr</option> <option value="MRS">Mrs</option> <option value="MS">Ms</option> <option value="MISS">Miss</option> <option value="OTHER">Other</option> </select> </label> and the post data of txtTitle was "Mr", how would I select Mr in the DomDocument e.g. how would I ammend this line: <option value="MR">Mr</option> to this: <option value="MR" selected="selected">Mr</option> Hope I was clear.ThanksJay Quote Link to comment https://forums.phpfreaks.com/topic/284275-2-domdocument-questions/ Share on other sites More sharing options...
requinix Posted November 26, 2013 Share Posted November 26, 2013 1. print_r() is only for debugging. Not "primarily". Only. It's a string. Just output the string with an echo or print. 2. Find the appropriate (a simple loop over the select node's getElementsByTagName("option") with a check of their values) and if you find one set its "selected" attribute. Quote Link to comment https://forums.phpfreaks.com/topic/284275-2-domdocument-questions/#findComment-1460101 Share on other sites More sharing options...
jay20aiii Posted November 26, 2013 Author Share Posted November 26, 2013 (edited) 1). Thanks 2). Can you tell me where I am going wrong on this loop: $element = $document->getElementById('txtTitle'); $options = $element->getElementsByTagName("option"); foreach($options as $option) { if ($option->textContent = $_POST['txtTitle']) { $option->setAttribute("selected", "selected"); } } The resulting html has an empty "selected" attribute on every Option tag. Edited November 26, 2013 by jay20aiii Quote Link to comment https://forums.phpfreaks.com/topic/284275-2-domdocument-questions/#findComment-1460102 Share on other sites More sharing options...
Solution requinix Posted November 26, 2013 Solution Share Posted November 26, 2013 if ($option->textContent = $_POST['txtTitle']) {Count your equals signs. But you actually use the "value" attribute instead of the textContent since that's where the txtTitle value actually comes from. Quote Link to comment https://forums.phpfreaks.com/topic/284275-2-domdocument-questions/#findComment-1460103 Share on other sites More sharing options...
jay20aiii Posted November 26, 2013 Author Share Posted November 26, 2013 (edited) Cool, working as it should be now thanks. Good point about the textContent. Edited November 26, 2013 by jay20aiii Quote Link to comment https://forums.phpfreaks.com/topic/284275-2-domdocument-questions/#findComment-1460107 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.