SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    birmingham, uk
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling Javascript function from PHP

    Hi All

    I have a problem. I am currently trying to call a javascript function from php. I am able to do this if i use a submit button, however I would like to call the function automatically but when I do so I get an error. Does anybody know why.



    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Patient Record</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT LANGUAGE="JavaScript " type="text/Javascript">
    function openindex(s)
    {
    // pxnow a js array
    var s = s.split(",");

    OpenWindow=window.open("", "newwin", "height=250,width=250," +
    "toolbar=no,scrollbars="+scroll+",menubar=no");
    OpenWindow.document.write("<TITLE>Title Goes Here</TITLE>");

    OpenWindow.document.write("<table size=\"100%\">")
    OpenWindow.document.write("<SELECT NAME=\"aSelect\" SIZE=\"10\" WIDTH=\"100%\" style=\"font-weight: bold; width: 235px;\">");
    for (i=0; i<s.length;i++)
    {
    OpenWindow.document.write("<OPTION>",s[i]);
    }
    OpenWindow.document.write("</select>");
    OpenWindow.document.write("</table>")
    OpenWindow.document.write( "<FORM>" +
    "<INPUT TYPE='BUTTON' VALUE='Close Window' onClick='window.close()'>" +
    "</FORM></BODY></HTML>"
    );
    OpenWindow.document.close();
    self.name="main";

    }
    </SCRIPT>

    </head>

    <body>
    <?Php

    $Host = "localhost";
    $User = "root";
    $Password = "";
    $db= "MedicalCenter";
    $TableName ="pd";
    //link the database
    $Link = mysql_connect($Host,$User,$Password) or die("<H1>Error</H1><P>Could Not Connect To Database </FORM></BODY></HTML>"); ;
    mysql_select_db($db)or die("<H1>Error</H1><P>Could not select database </FORM></BODY></HTML>");
    //create a text box for the surname
    echo "<form method=\"POST\" action=\"PatientRecord.php\">";
    echo "<P><B>Surname : </B>
    <INPUT TYPE=TEXT NAME=\"P\" Size=35 value =\"$P\" >";
    //submission value
    //echo "<input type=\"submit\" name=\"T\" value=\"...\"> ";
    if (chr(13))
    {
    print("hello");
    //search for all patients matchin the surname entered
    $sql = "SELECT * from pd where Surname LIKE '$P%'";
    print ("$sql");
    $res = mysql_query($sql) or die (" $result Invalid query3");
    //get the number of rows found
    $num_rows = mysql_num_rows($res);
    print" NO OF ROWS $num_rows";
    $i=0;
    //if no matching results are found
    if($num_rows ==0)
    {
    print("<P>There are no patients matchin the surname you entered");
    }
    //put the values retrieved from the database into an array
    if ($num_rows > 0)
    {
    while ($row = mysql_fetch_array($res)){
    $PatientArray[$i]=$row[3];
    $i++;
    $PatientArray[$i]=$row[9];
    $i++;
    }
    print_r ($PatientArray);
    $comma_separated = implode(",", $PatientArray);
    print($comma_separated);

    // ***************************************8
    //THIS DOES NOT ALLOW ME TO CALL THE FUNCTION
    //openindex('$comma_separated')";

    //THIS ALLOWS ME TO CALL THE FUNCTION
    echo"<FORM >
    <INPUT type=\"submit\" value=\"...\" onClick=\"openindex('$comma_separated')\">
    </FORM>";
    // *******************************************
    }
    }
    ?>

    </body>
    </html>


    Thanks


    Humaira

  2. #2
    Yugo full of anvils bronze trophy hillsy's Avatar
    Join Date
    May 2001
    Location
    :noitacoL
    Posts
    1,859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use an event handler to call Javascript. If you want it to run automatically when the page is loaded, put it into the onLoad event handler of the <body> tag.
    that's me!
    Now A Pom. And a Plone Nut
    Broccoli Martinez Airpark

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need an event handler, it's perfectly OK to call a function directly, as the page loads - just so long as you tell the browser that it's JavaScript.
    Code:
    <script type="text/javascript">
    
        openindex('$comma_separated')";
    
    </script>
    ::: certified wild guess :::


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •