PHP within javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kublai khan

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

    }
    </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

  • lkrubner@geocities.com

    #2
    Re: PHP within javascript

    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["imagesFold er"];

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




    <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.");
    }
    }
    }
    </script>

    Comment

    • mark

      #3
      Re: PHP within javascript

      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

      Comment

      Working...