PHP within javascript

K

kublai khan

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
 
L

lkrubner

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>
 
M

mark

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top