can't call JavaScript function from php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evicailieva
    New Member
    • Aug 2007
    • 6

    can't call JavaScript function from php

    A have a php scrip where I call a JavaScript function. I don't know why, but it doesn't work. At the beginning, when I was writing the script it was working but now it's not. I don't know wхat to do. Please help me. Here is the php code:
    [code=php]
    function print_rezBilet( $rezBilet,$posR ezBilet1){
    $step=$_SESSION['cekor'];
    $max=$_SESSION['max'];
    $curr= $_SESSION['currentRezBile t']+$step-1 ;


    if($max<$curr)
    $curr=$max;

    //vo ovie dve promenlivi se zacuvuva brojot na prikazanata strana i
    //vkupniot broj na strani
    $currPage=ceil( $curr / $_SESSION['cekor']);
    $maxPage=ceil($ max / $_SESSION['cekor']);
    if (!$rezBilet) {
    echo error_message(' Íå ñå âíåñåíè ðåçåðâàöèè íà áèëåòè');
    echo <<<END
    <tr><form method="post" action="$posRez Bilet1" ><td align="center">
    <center>
    <input type="submit" value="Äîäàäè íîâ ðåçåðâàöè¼à" name="newRezBil et" class="button">
    </center>
    </td>
    </form>
    </tr>
    END;
    }
    else {
    echo <<<END
    <center><tabl e cellpadding='2' cellspacing='1' bordercolor='#9 93300'>
    <tr>
    <th scope='col'>Èçì åíè</th>
    <th scope='col'>Áðè øè</th>
    <th scope='col'>Òóð èñòID<a href="?orderCol umnRezBilet=tur ist_id&orderRez Bilet=desc"/><img src="../images/dole.gif" width="10" height="10" border="0"/></a><a href="?orderCol umnRezBilet=tur ist_id&orderRez Bilet=asc"/><img src="../images/gore.gif" width="10" height="10" border="0"/></a></th>
    <th scope='col'>Áèë åòId<a href="?orderCol umnRezBilet=b_i d&orderRezBilet =desc"/><img src="../images/dole.gif" width="10" height="10" border="0"/></a><a href="?orderCol umnRezBilet=b_i d&orderRezBilet =asc"/><img src="../images/gore.gif" width="10" height="10" border="0"/></a></th>
    <th scope='col'>Äàò à íà ðåçåðâèðàœå <a href="?orderCol umnRezBilet=rez _data&orderRezB ilet=desc"/><img src="../images/dole.gif" width="10" height="10" border="0"/></a><a href="?orderCol umnRezBilet=rez _data&orderRezB ilet=asc"/><img src="../images/gore.gif" width="10" height="10" border="0"/></a></th>
    <th scope='col'>Êîë è÷èíà<a href="?orderCol umnRezBilet=kol icina&orderRezB ilet=desc"/><img src="../images/dole.gif" width="10" height="10" border="0"/></a><a href="?orderCol umnRezBilet=kol icina&orderRezB ilet=asc"/><img src="../images/gore.gif" width="10" height="10" border="0"/></a></th>
    </tr>

    END;
    $poraka="Dali sakas";
    foreach($rezBil et as $rezB)
    {
    echo '<tr>';
    echo '<td align="center"> <a href="?modifyRe zBilet='.
    htmlentities($r ezB['TURIST_ID'])." ".htmlentities( $rezB['B_ID']).'" /><img src="../images/edit.png" width="24" height="24" border="0" /></a></td>';
    echo '<td align="center"> <a href="?deleteRe zBilet='.
    htmlentities($r ezB['TURIST_ID'])." ".htmlentities( $rezB['B_ID']).'" /><img src="../images/delete2.png" width="24" height="24" border="0" /></a></td>';
    echo '<td>'.$rezB['TURIST_ID'].'<a href="?showTur= '.
    htmlentities($r ezB['TURIST_ID']).'" onclick="showLi stTurist();retu rn false;" /><img align="right" src="../images/details.png" width="14" height="14" border="0" /></a></td>';
    echo '<td>'.$rezB['B_ID'].'<a href="?showBil= '.
    htmlentities($r ezB['B_ID']).'" onclick="showLi stBilet(); return false;" /><img align="right" src="../images/details.png" width="14" height="14" border="0" /></a></td>';
    echo '<td>'.$rezB['REZ_DATA'].'</td>';
    echo '<td>'.$rezB['KOLICINA'].'</td>';
    echo '</tr>';
    }
    echo <<<END
    <tr><form method="post" action="$posRez Bilet1"><td align="center">
    <a href="?newRezBi let2=vnesi" /><img src="../images/user1_add.png" width="36" height="36" border="0" /></a></td><td colspan="1" align="center">
    <input type="submit" value="Äîäàäè íîâà ðåçåðâàöè¼à" name="newRezBil et" class="button">
    </td>

    <td colspan="2"><di v align="center">
    <input type="submit" value="< Ïðåòõîäíè $step" name="previousR ezBilet" class="button">
    <input type="submit" value="Ñëåäíè $step >" name="nextRezBi let" class="button">
    </div>
    </td>
    <td>
    <select name="cekor" style="backgrou nd:#F1D798" onchange="MM_ju mpMenu('parent' ,this,0)">
    <option value="" style="backgrou nd:#F1D798">ïðè êàæè</option>
    <option value="?cekor=5 " style="backgrou nd:#F1D798">5</option>
    <option value="?cekor=1 0" style="backgrou nd:#F1D798">10</option>
    <option value="?cekor=1 5" style="backgrou nd:#F1D798">15</option>
    <option value="?cekor=2 0" style="backgrou nd:#F1D798">20</option>
    <option value="?cekor=2 5" style="backgrou nd:#F1D798">25</option>
    <option value="?cekor=3 0" style="backgrou nd:#F1D798">30</option>
    </select>
    </td>
    <td align="center"> $currPage/$maxPage Ñòðàíà</td>
    </tr>
    </form>
    </table></center>
    END;
    }
    }
    [/code]



    and here is the part where the JavaScript functions are:

    [code=javascript]
    <script type="text/JavaScript">
    <!--
    function MM_jumpMenu(tar g,selObj,restor e){ //v3.0
    eval(targ+".loc ation='"+selObj .options[selObj.selected Index].value+"'");
    if (restore) selObj.selected Index=0;
    }
    function MM_popupMsg(msg ) { //v1.0
    confirm(msg);
    }
    //-->
    </script>
    <!-- TinyMCE -->
    <script language="javas cript" type="text/javascript" src="../jscripts/tiny_mce/tiny_mce.js"></script>
    <script language="javas cript" type="text/javascript">
    tinyMCE.init({
    mode : "textareas" ,
    theme : "simple"
    });

    function fileBrowserCall Back(field_name , url, type, win) {
    // This is where you insert your custom filebrowser logic
    alert("Filebrow ser callback: field_name: " + field_name + ", url: " + url + ", type: " + type);

    // Insert new URL, this would normaly be done in a popup
    win.document.fo rms[0].elements[field_name].value = "someurl.ht m";
    }

    function showListTurist( )
    {
    <?php
    $_SESSION['showTur']=$_GET['showTur'];
    ?>
    sList = window.open("tu rist_prozorecB. php", "turist", "width=800,heig ht=200, scrollbars=yes" );
    }

    function showListBilet()
    {
    <?php
    $_SESSION['showBil']=$_GET['showBil'];
    echo "neso";
    ?>
    sList = window.open("bi let_prozorecB.p hp", "bilet", "width=800,heig ht=100, scrollbars=1");
    }

    function showListTurInse rt()
    {
    sList = window.open("tu rist_prozorecLi st.php", "turisti", "width=800,heig ht=700, scrollbars=1");
    }
    function showListBilInse rt()
    {
    sList = window.open("bi let_prozorecLis t.php", "bileti", "width=800,heig ht=700, scrollbars=1");
    }



    </script>
    <!-- /TinyMCE -->
    [/code]

    I call the functions on other places too, but it doesn't work there neither :
    [code=html]
    <tr>
    <td colspan=3 align="right">
    Òóðèñò:
    <input type="text" name="turistid" size="29" value=$turistid >
    <input type="button" value="èçëèñò༠" onClick="showLi stTurInsert()" class="button">
    </td>
    </tr>
    [/code]

    PLEASE HELP ME
    Last edited by Atli; Aug 22 '07, 12:54 AM. Reason: Added code tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Please post your code inside [code] tags! It's impossible to read it without them.

    What have you changed since the code worked?
    Are you getting any PHP or JavaScript errors?
    What is your code supposed to be doing? How is it not working?

    Comment

    • jx2
      New Member
      • Feb 2007
      • 228

      #3
      Code:
       
      <a  href="?showTur='.htmlentities($rezB['TURIST_ID']).'
      i'm not sure but i think it won't work
      shouldnt that be urlencode() as it is part of a url?

      regards
      jx2

      Comment

      • evicailieva
        New Member
        • Aug 2007
        • 6

        #4
        cant

        here is the code in code tags
        [PHP]$poraka="Dali sakas";
        foreach($rezBil et as $rezB)
        {
        echo '<tr>';
        echo '<td align="center"> <a href="?modifyRe zBilet='.
        htmlentities($r ezB['TURIST_ID'])." ".htmlentities( $rezB['B_ID']).'" /><img src="../images/edit.png" width="24" height="24" border="0" /></a></td>';
        echo '<td align="center"> <a href="?deleteRe zBilet='.
        htmlentities($r ezB['TURIST_ID'])." ".htmlentities( $rezB['B_ID']).'" /><img src="../images/delete2.png" width="24" height="24" border="0" /></a></td>';
        echo '<td>'.$rezB['TURIST_ID'].'<a href="?showTur= '.
        htmlentities($r ezB['TURIST_ID']).'" onclick="showLi stTurist();retu rn false;" /><img align="right" src="../images/details.png" width="14" height="14" border="0" /></a></td>';
        echo '<td>'.$rezB['B_ID'].'<a href="?showBil= '.
        htmlentities($r ezB['B_ID']).'" onclick="showLi stBilet(); return false;" /><img align="right" src="../images/details.png" width="14" height="14" border="0" /></a></td>';
        echo '<td>'.$rezB['REZ_DATA'].'</td>';
        echo '<td>'.$rezB['KOLICINA'].'</td>';
        echo '</tr>';
        }
        echo <<<END
        <tr><form method="post" action="$posRez Bilet1"><td align="center">
        <a href="?newRezBi let2=vnesi" /><img src="../images/user1_add.png" width="36" height="36" border="0" /></a></td><td colspan="1" align="center">
        <input type="submit" value="Äîäàäè íîâà ðåçåðâàöè¼à" name="newRezBil et" class="button">
        </td>

        <td colspan="2"><di v align="center">
        <input type="submit" value="< Ïðåòõîäíè $step" name="previousR ezBilet" class="button">
        <input type="submit" value="Ñëåäíè $step >" name="nextRezBi let" class="button">
        </div>
        </td>
        <td>
        <select name="cekor" style="backgrou nd:#F1D798" onchange="MM_ju mpMenu('parent' ,this,0)">
        <option value="" style="backgrou nd:#F1D798">ïðè êàæè</option>
        <option value="?cekor=5 " style="backgrou nd:#F1D798">5</option>
        <option value="?cekor=1 0" style="backgrou nd:#F1D798">10</option>
        <option value="?cekor=1 5" style="backgrou nd:#F1D798">15</option>
        <option value="?cekor=2 0" style="backgrou nd:#F1D798">20</option>
        <option value="?cekor=2 5" style="backgrou nd:#F1D798">25</option>
        <option value="?cekor=3 0" style="backgrou nd:#F1D798">30</option>
        </select>
        </td>
        <td align="center"> $currPage/$maxPage Ñòðàíà</td>
        </tr>
        </form>
        </table></center>
        END;
        }
        } [/PHP]

        this is only the part where I call the functions, and here are the JavaScript functions

        [CODE=javascript]function showListTurist( )
        {
        <?php
        $_SESSION['showTur']=$_GET['showTur'];
        ?>
        sList = window.open("tu rist_prozorecB. php", "turist", "width=800,heig ht=200, scrollbars=yes" );
        }

        function showListBilet()
        {
        <?php
        $_SESSION['showBil']=$_GET['showBil'];
        echo "neso";
        ?>
        sList = window.open("bi let_prozorecB.p hp", "bilet", "width=800,heig ht=100, scrollbars=1");
        }[/CODE]

        The code prints the values stored the array $rezBilet in a table and I want when you press on the picture 'details' to open new window. That's why I call the JavaScript function. But I also want to send the variable $rezB['TURIST_ID'] to the new window that opens, so thats why I use href=?showTur.. ., and then in the JavaScript function I put it in session.
        I don't get any kind of errors, it does nothing, and the biggest problem is, that I that I haven't changed much. I only added some things but on the other part of the script that has not much connection whit this (I think). It just stop working.
        Just adds the value from $rezB['TURIST_ID'] to showTur, but it doesn't call the JavaScript functions.
        Sorry that I didn't write correctly the previous post, but I'm new at this.

        Comment

        • jx2
          New Member
          • Feb 2007
          • 228

          #5
          you should use [ php] [ /php] tags for your code

          what do you mean do not call javascript? php cant call javascript anyway...

          regards jx2

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, evicailieva. Welcome to TSDN!

            You're having trouble because (as JX2 mentioned) PHP can't call a JavaScript function.

            PHP is server-side, whereas JavaScript is client-side. Since the code is running on separate machines, there's no way for them to directly communicate.

            Now, you can have the PHP output JavaScript that you'd like the browser to run while it's loading the page, and you can use AJAX to send a new HTTP request to the server to run additional scripts after the main page has loaded, but those are your best options.

            Comment

            • evicailieva
              New Member
              • Aug 2007
              • 6

              #7
              I mean it does not execute the code in the function showListTurist( ), it doesn't open new window.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, Evicailieva.

                You mean that when you click on the links in the table cells, it's not opening the new window, correct?

                Are you getting an error, or is it just not doing anything?

                Comment

                • evicailieva
                  New Member
                  • Aug 2007
                  • 6

                  #9
                  Yes that's exactly what I mean. It does not show errors. It does nothing. I really don't know what's wrong! I also have this code

                  [PHP]echo <<<END
                  <tr>
                  <td colspan=3 align="right">
                  Òóðèñò:
                  <input type="text" name="turistid" size="34" value="òóðèñò">
                  <input type="button" value="èçëèñò༠" onClick="showLi stTurInsert()" class="button">
                  </td>
                  </tr>
                  END;[/PHP]

                  and also dosen't open new window. It also does nothing.

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    Originally posted by evicailieva
                    ...
                    The code prints the values stored the array $rezBilet in a table and I want when you press on the picture 'details' to open new window. That's why I call the JavaScript function. But I also want to send the variable $rezB['TURIST_ID'] to the new window that opens, so thats why I use href=?showTur.. ., and then in the JavaScript function I put it in session
                    ....
                    There is one more thing I think may be causing a problem.

                    You call a JavaScript function through the onclick event in a <a> tag, but it the same tag you set the href parameter, trying to pass the url in the href parameter to the window, opened by you JavaScript function.
                    This will not work. What actually happens is, when your user clicks the <a> link, the JavaScript function is executed, and then the client is redirected to the URL in the href parameter

                    For example:
                    [code=javascript]
                    <script type="text/javascript">
                    function alertme()
                    {
                    alert("You have been alerted!");
                    }
                    </script>

                    <a href="?var=val" onclick="alertm e();">Linkage</a>
                    [/code]
                    This will put up an alert box and then it will redirect you to '?var=val'.

                    You would need to do something like this:
                    [code=javascript]
                    <script type="text/javascript">
                    function openWindow(getP arams)
                    {
                    window.open("my page.php?"+ getParams);
                    }
                    </script>

                    <a href="#" onclick="openWi ndow('var=val') ;">Linkage</a>
                    [/code]
                    Which will open up another window showing the page 'mypage.php?var =val'.

                    Comment

                    • evicailieva
                      New Member
                      • Aug 2007
                      • 6

                      #11
                      Thank you very much. That works.

                      Comment

                      Working...