473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 23 '05 #1
2 1073
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 23 '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 23 '05 #3

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

Similar topics

6
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...

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.