473,573 Members | 3,126 Online
Bytes | Software Development & Data Engineering Community
+ 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.cooki e.indexOf('Name =User')==-1) {

--- do this ---


This does not work.

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

What am doing wrong?


Jul 17 '05 #1
2 7532
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

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


$config = getConfig();
$path = $config["imagesFold er"];

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

<script type="text/javascript">

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

if (document.selec tion && document.select ion.createRange ) {
var range = document.select ion.createRange ();
if (range.parentEl ement() == element) range.text = range.text
+ address;
} else if ('number' == typeof myField.selecti onStart) {
var startPos = myField.selecti onStart;
var endPos = myField.selecti onEnd;
if (startPos != 0 || endPos != 0) {
var mySelection =
myField.value.s ubstring(myFiel d.selectionStar t, myField.selecti onEnd);
mySelection += address
myField.value = myField.value.s ubstring(0, startPos) +
mySelection + myField.value.s ubstring(endPos , myField.value.l ength);
} 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.");

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.cooki e.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

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 scripts. Being a javascript newbie and after significant testing, I suspect that the document.write fails after finding a </script> within pageVar....
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 frames, where the two lower ones are within a Frameset within the master Frameset: 1111111111111111111
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 Code: Public Sub VBProcedure() .. ..
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 language="javascript" src="../scripts/base.js" type="text/javascript" /> } </script> Obviously this would cause n error but this would give you an idea of what...
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 getting as far as the iframe section as other parts of the html code work correctly. But it is not executing any code within the getPage() function...
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 validation of each form field, the algorithm is much like: if(document.formName.field.value == ""){ //throw an error and return false }
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 of the script gets executed. Can someone please give me a direction as to what I may be missing? Thanks.
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 ;
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 asp file (as I have to read from a back-end access database). In turn in the asp file, I have a Body onload tag to execute a
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 <script language="javascript"> function foo() { var bar = <%=(java variable)%>
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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 we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.