souciance Posted June 30, 2009 Share Posted June 30, 2009 Hello, I have a php function which prints out some html form code. I am having trouble getting the javascript to work when it is being echoed in the function. This is part of the code where the javascript is called. echo'<table class="currencyTransferTable" cellspacing="10" cellpadding="10"> <tr> <td> <label for="amount" class="label">Amount: </label> </td> <td> <input id="amount" name="amount" value="' .$amount; echo '" class="input" type="text" /><img src="images/asterisk.gif" height="7" class="img" alt="required" onkeypress="javascript:return numbersonly(event)" /><br>'; echo '</td></tr>'; global $error; The problem is, not matter what javascript I write it fails to register. Even a simple alert() won't work. Are there special things to remember when printing out javascript, especially inside a php function? Thanks, Moeed. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted June 30, 2009 Share Posted June 30, 2009 Well from what I remember, you can't write javascript inside PHP Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 1, 2009 Share Posted July 1, 2009 Well from what I remember, you can't write javascript inside PHP Of course you can! You can write anything that would be a complete page to be received by the browser: HTML, CSS, JavaScript, etc. Have you checked the rendered HTML code to see if the JavaScript is generated as expected? There might be a quote mark out of place. The manner in which you are echoing the code makes it difficult to follow. I do notice that you have a parameter in the onkeypress() trigger - event. has that variable been declared? Also, are you getting JavaScript errors? here is a rewrite of your code that's a little easier to follow for me: echo "<table class=\"currencyTransferTable\" cellspacing=\"10\" cellpadding=\"10\">\n"; echo " <tr>\n"; echo " <td><label for=\"amount\" class=\"label\">Amount: </label></td>\n"; echo " <td> echo " <input id=\"amount\" name=\"amount\" value=\"$amount\" class=\"input\" type=\"text\" />\n" echo " <img src=\"images/asterisk.gif\" height=\"7\" class=\"img\" alt=\"required\" onkeypress=\"javascript:return numbersonly(event)\" /><br>\n"; echo " </td>\n"; echo " </tr>\n"; global $error; Quote Link to comment 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.