iwato Posted April 24, 2017 Share Posted April 24, 2017 (edited) BACKGROUND: I am tempted to write something like the following foreach ($non-matches as $key => $val) { echo '<script> $("#' . $key . '").append("<p style=' . '"color:red;">Complete this item!</p>);</script>)'; } QUESTION ONE: Under the assumption that the echo statement is correct would you recommend this as proper programming procedure? If not, please explain why and provide an alternative, more appropriate strategy that would achieve the same objective suggested by the use of a jQuery object ? QUESTION TWO: If it is proper programming procedure, please correct the echo statement so that it will achieve the following: $("#...").append("<p style='color:red;'>Complete this item!</p>"); EDIT: In order to assist you in your response, the PHP variable $non-matches contains a list of values for the id attributes of form input control tags that have been carefully selected through a prior PHP procedure. An alternative approach that is currently under consideration is to create a JSON object, assign it to a Javascript variable, and iterate the resulting object with jQuery. Edited April 24, 2017 by iwato Link to comment Share on other sites More sharing options...
justsomeguy Posted April 24, 2017 Share Posted April 24, 2017 Instead of using PHP to output Javascript code that changes the appearance of an element, why not just print the element the way you want it to appear? Link to comment Share on other sites More sharing options...
iwato Posted April 24, 2017 Author Share Posted April 24, 2017 (edited) Because there are many elements, and I am never sure what will be contained in the PHP variable $non_matches. JSG: Please see my above edit. It may prove useful in your further response. Edited April 24, 2017 by iwato Link to comment Share on other sites More sharing options...
justsomeguy Posted April 24, 2017 Share Posted April 24, 2017 When you're printing each field you can check if the field ID exists in $non_matches, and style the element appropriately. I'm just suggesting that it is more efficient to print everything correct in the first place, versus printing everything then trying to run client-side code to show errors or whatever else. 1 Link to comment Share on other sites More sharing options...
iwato Posted April 24, 2017 Author Share Posted April 24, 2017 Got it. I understand. Write the HTML into the document manually, and then alter the element with PHP according to what appears in &non_matches. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now