简体   繁体   中英

How do I write Query code while I use Jquery in PHP

I am new to Php and mysql. my project is like bio "Dictionary". I want to find a WORD and I will get it's definition and couple more columns which has related information regarding the WORD im looking for. I want to get dictionary data in nice table.

I have wrote the query to retrieve data related to my word from my mysql database. I will get data from 4 different tables.

I have wrote Index.php and data.php code which is posted below. Problems>>>>>>

1) when I am running inded.php file I m getting error

function get() { $.post ('data.php', { word: form.name.value }, function (output) { $('#defination') .html(output).show() ; }); } "

2) I am having difficult in writing code for data.php file, please give me suggestion. I m retrieving data from multiple tables so its very confusing.

PLEASE help me out, I m really struggling and I would appreciate your help.

Thank you

index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"

<html lang="en">

<head>
    <title></title>

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript"></script>   
             function get() {
                  $.post ('data.php', { word: form.name.value },
                          function (output) {

                        $('#defination') .html(output).show() ;

                    });
             }
</head>

<body>

<p>
 <form name ="form">
    <input type="search" name="name" size = "30" placeholder="Please enter the Word">
    <input type="button" value="Get" onClick="get();">

 </form>

 <div id="defination"> </div>
</p> 


</body>
</html>

data.php

    <table border="2" cellpadding="5" cellspacing="1" bordercolor="#000000">
    <tr>
    <!--<td width="120"><b>Normalized Word</b></td>
    <td width="120"><b>Normalized String</b></td>-->
    <td width="120"><b>Word</b></td>
    <td width="120"><b>Defination</b></td>
    <td width="120"><b>Type of Sementic</b></td>
    <td width="120"><b>Source</b></td>

    </tr>
    <tr>
    <td>

    <?php
    $username = "root";
    $password = "";
    $hostname = "127.0.0.1"; 

    //connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password) 
     or die("Unable to connect to MySQL");


    //select a database to work with
    $selected = mysql_select_db("umls",$dbhandle) 
      or die("Could not select examples");


      // $_Post

      $name = mysql_real_escape_string($_POST['word']);

    //Name  is NULL
    if ($word==NULL)
        echo "Please enter a Word" ;
    else {
    // Do Query

    $result = mysql_query("SELECT DISTINCT * FROM nString WHERE Normalized_String= '$word' GROUP by  string, defination, Source");
    $result_num_rows = mysql_num_rows($result);
    //fetch tha data from the database 



        while ($row = @mysql_fetch_array($result))
        {
        $variable1=$row["Normalized_Word"];
        $variable2=$row["Normalized_String"];
        $variable3=$row["String"];
            $variable4=$row["Defination"];
        $variable5=$row["Semantic_type"];
        $variable6=$row["source"];

        //table layout for results

        print ("<tr>");
        //print ("<td>$variable1</td>");
        //print ("<td>$variable2</td>");
        print ("<td>$variable3</td>");
            print ("<td>$variable4</td>");
        print ("<td>$variable5</td>");
        print ("<td>$variable6</td>");

        print ("</tr>");
        }
        }   


       // while ($row = mysql_fetch_array($result)) {
     //   echo $row{'CUI'}.$row{'SUI'}.$row{'AUI'}."<br>";

    //close the connection
    mysql_close($dbhandle);
    ?>

    </table>

you are getting the first error since the javascript code should be inside the <script> tag and not outside it..

try this..

<script type="text/javascript">  //notice the </script> tag is shifted to the end of javascript code

         function get() {
              $.post ('data.php', { word: form.name.value },
                      function (output) {

                    $('#defination') .html(output).show() ;

                });
         }

</script>    

and other few errors in data.php

 $name = mysql_real_escape_string($_POST['word']);


if ($name==NULL)  //<----here $word = $name 
    echo "Please enter a Word </td></tr>"; //close the tr and td
else {

and you can end and start the php tag again

  <?php
  $username = "root";
  $password = "";
  $hostname = "127.0.0.1"; 
  .....  
  .....
    $variable5=$row["Semantic_type"];
    $variable6=$row["source"];

    ?>

    <tr>

      <td><?php echo $variable3; ?></td>
      <td><?php echo $variable4; ?></td>
      <td><?php echo $variable5; ?></td>
      <td><?php echo $variable6; ?></td>

    </tr>

NOTE: mysql is deprecated use mysqli PDO ... learn OOP create class instead of procedure based and look for some php framework... CODEIGNITER , ZEND , YII and so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM