JS doesn't work with my PHP file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Betty_S
Forum Newbie
Posts: 20
Joined: Tue Dec 05, 2006 3:40 am

JS doesn't work with my PHP file

Post by Betty_S »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I wrote a simple js function:

Code: Select all

echo "<script type=\"text/javascript\">";
echo "function alertBtn()";
echo"{alert(document.getElementById(\"btn\").id)}";
echo "</script>";
It refuses to fire, did I miss some important point or should I just recheck my code?

Additionally, is there a special php command for conetcting the php file to a js file or should I just add "<script src=…></script>"?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Please post the HTML source which comes with the JS function.

If you want to print a dynamic JS file, you can use it in the src attribute in the script tag:

Code: Select all

<script src="http://www.example.com/js.php" type="text/javascript"></script>
But, if you want to communicate with PHP through JS, you need AJAX: http://developer.mozilla.org/en/docs/AJAX
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

I'm confused by what you asked. The snippet you showed merely sends that javascript code to the browser as part of the html. If it's not working, it's because you aren't calling it properly from somewhere else in the html. Where is the place that calls that function? That's the part that is failing.
Betty_S
Forum Newbie
Posts: 20
Joined: Tue Dec 05, 2006 3:40 am

thats the code

Post by Betty_S »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

echo "<script language=\"JavaScript\" type=\"text/javascript\">";
			echo "function filter(){alert(document.all(\"hdn\").value)}";
			echo "function filterx(){alert(document.getElementById(\"btn\"))";
			echo "</script>";
			echo "<input type=\"hidden\" id=\"hdn\" name=\"hdn\" value=".$id." />";
			echo "<input type=\"button\" id=\"btn\" name=\"btn\"  value=\"&#1505;&#1504;&#1503;\" onclick=\"filterx()\"/> ";

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Betty_S wrote:echo "<script language="JavaScript" type="text/javascript">";
echo "function filter(){alert(document.all("hdn").value)}";
echo "function filterx(){alert(document.getElementById("btn"))";
echo "</script>";
echo "<input type="hidden" id="hdn" name="hdn" value=".$id." />";
echo "<input type="button" id="btn" name="btn" value="סנן" onclick="filterx()"/> ";
After The Red Marked ) There Should Be A }
Post Reply