Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,937 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,849 people online right now. Registration is fast and FREE... Join Now!




displaying a variable value in javascript alert

 
Reply to this topicStart new topic

displaying a variable value in javascript alert

samn84
12 Mar, 2008 - 11:01 PM
Post #1

New D.I.C Head
*

Joined: 21 Feb, 2008
Posts: 41

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>

User is offlineProfile CardPM
+Quote Post

bhandari
RE: Displaying A Variable Value In Javascript Alert
13 Mar, 2008 - 12:12 AM
Post #2

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Displaying A Variable Value In Javascript Alert
13 Mar, 2008 - 12:48 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,998



Thanked: 126 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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:


php

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 smile.gif
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Displaying A Variable Value In Javascript Alert
13 Mar, 2008 - 12:52 AM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,495



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(PsychoCoder @ 13 Mar, 2008 - 01:48 AM) *

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.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:48PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month