Jump to content

Javascript inside php function


souciance

Recommended Posts

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.

Link to comment
Share on other sites

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;		

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.