473,493 Members | 4,319 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

PHP within javascript

I want to set a conditional statement that checks to see if a session
variable has populated. Here's the statement which does not work.

<?php $testval = $_SESSION['TestStr']; ?>
(script type = "text/javascript">
if ((<?php $testval == ""; ?>) &&
(document.cookie.indexOf('Name=User')==-1) {

--- do this ---

}
</script>

This does not work.
However,

(script type = "text/javascript"> alert('(<? echo $testval; ?>)');
</script>
does print the value of $testval.

What am doing wrong?
TIA

KK

Jul 17 '05 #1
2 7520
It's easy enough to get PHP values into Javascript. Create a variable
in PHP and then break into an echo statement to get that variable into
your PHP, just as you would get PHP variables into any HTML. Remember
you have to use double quotes to get PHP variables to interpret as
such.

This is how I'm doing it and it works fine:

<?php

$config = getConfig();
$path = $config["imagesFolder"];

echo "
<script type=\"text/javascript\">
pathToImage = '$path';
</script>
";
?>


<script type="text/javascript">

function insertImageFromSelect (myField) {
var imageName = myField.value;
if (imageName != '') {
pathToImage += imageName;
address = '<img src=\"' + pathToImage + '\">';

if (document.selection && document.selection.createRange) {
var range = document.selection.createRange();
if (range.parentElement() == element) range.text = range.text
+ address;
} else if ('number' == typeof myField.selectionStart) {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
if (startPos != 0 || endPos != 0) {
var mySelection =
myField.value.substring(myField.selectionStart, myField.selectionEnd);
mySelection += address
myField.value = myField.value.substring(0, startPos) +
mySelection + myField.value.substring(endPos, myField.value.length);
} else {
myField.value += address;
alert("You haven't selected any text to change so we put the
image at the end of your text.");
}
} else {
myField.value += address;
alert("You haven't selected any text to change so we put the
image at the end of your text.");
}
}
}
</script>

Jul 17 '05 #2
Hello Kublai,

You are close, but you need to print the value of testval so the
javascript can read it. What you have tests the value of testval in the
PHP code, but prints nothing.

View the source of your javascript on the page to see if your PHP code
is outputting what you expect.

Try this:

<?php $testval = $_SESSION['TestStr']; ?>
(script type = "text/javascript">
if (("<?php echo($testval) ?>" == "") &&
(document.cookie.indexOf('Name=User') == -1)) {
--- do this ---

}

Mark Aardsma

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
12943
by: Mike Daniel | last post by:
I am attempting to use document.write(pageVar) that displays a new html page within a pop-up window and the popup is failing. Also note that pageVar is a complete HTML page containing other java...
7
2290
by: David Hayes | last post by:
I tried finding an answer on http://www.quirksmode.org/ without success. I am attempting a complicated Frames structure. I have made it work in IE, but not Netscape. I begin with three...
4
6298
by: Martin Feuersteiner | last post by:
Dear Group I'm using VB to write an aspnet application. I would like to call a javascript function from within a VB Sub or VB Function, is it possible? My code is something like this: VB...
17
2745
by: CES | last post by:
All, I was wondering if their is a way of loading an external script fill from within a script?? <script language="javascript" type="text/javascript"> function test(var){ <script...
13
6658
by: ukrbend | last post by:
I'm new to Javascript and to html and am trying to make the following code snippet work but it doesn't. It refuses to call the getPage() function and I always get a 404 error. I know the code is...
2
2317
by: Jon | last post by:
All, I'm currently working with a PHP-based CMS application, and am begining to put the finishing touches on it via Javascript validation. Currently, I'm using fairly standard methods of...
3
2940
by: Water Cooler v2 | last post by:
Questions: 1. Can there be more than a single script block in a given HEAD tag? 2. Can there be more than a single script block in a given BODY tag? To test, I tried the following code. None...
9
41691
by: misdst23 | last post by:
Hi, I know I can call a static java method within javascript by using the <% ... %tags. But how can pass a javascript variable ? function thefunction() { var = javascriptvariable ;
3
2075
by: u0107 | last post by:
Hello, I have a multi-frame page. The frames are named Frame_1, Frame_2 and Frame_3. Frame_1 has a drop down box. When a value is selected in this drop down box, Frame_2 is updated using an...
9
1127
by: Morlaath | last post by:
Is there a way to embed php within javascript code? I know that php is server side and js is client side. For instance when working with jsp, you can imbed jsp scriptlets within the js code. e.g...
0
7118
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.