3 Replies - 25549 Views - Last Post: 13 March 2008 - 01:52 AM Rate Topic: -----

#1 samn84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 21-February 08

displaying a variable value in javascript alert

Posted 13 March 2008 - 12:01 AM

i m not sure wether it is possible but i wnt a php variable to be printed in a javascript alert.izzi possible.

the code is as follows:
$err=$arrerr['message']

i want $err variable to be printed in the alert below:

<script language="Javascript" runat="server" type="text/javascript">
window.alert("Uniqueness Constraint Violated!");
</script>

Is This A Good Question/Topic? 0
  • +

Replies To: displaying a variable value in javascript alert

#2 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Re: displaying a variable value in javascript alert

Posted 13 March 2008 - 01:12 AM

I think you can't access php variables in javascript

you can store the value of that variable in a hidden HTNL field and then access that value in javascript.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: displaying a variable value in javascript alert

Posted 13 March 2008 - 01:48 AM

You cannot access PHP variables directly in Javascript, PHP is server side, Javascript is client side. You can, however, have PHP write your Javascript for you, here's an example:


echo "<script language=\"javascript\" type=\"text/javascript\">\n";
echo "  alert("'" .$err . "'">);\n";
echo "</script>";




Give something like that a try. Also, this is more of a PHP question so I'm moving this to the PHP forum :)
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5326
  • View blog
  • Posts: 27,243
  • Joined: 10-May 07

Re: displaying a variable value in javascript alert

Posted 13 March 2008 - 01:52 AM

View PostPsychoCoder, on 13 Mar, 2008 - 01:48 AM, said:

You cannot access PHP variables directly in Javascript, PHP is server side, Javascript is client side.

That's correct... just to add to the discussion:
By the time the code reaches your (the clients) web browser, PHP variables no longer exist, because they were processed by the PHP engine on the web server & are now part of the HTML code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1