By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,745 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

PHP within javascript

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.