Jump to content

PHP and Javascript - Alternative Approaches


iwato
 Share

Recommended Posts

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 by iwato
Link to comment
Share on other sites

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 by iwato
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...