在PHP中使用Jquery时如何编写查询代码

时间:2013-07-23 20:44:54

标签: php jquery reference

我是Php和mysql的新手。我的项目就像生物“词典”。我想找到一个WORD,我会得到它的定义,并且会有更多列,其中包含有关我正在寻找的WORD的相关信息。我想在漂亮的表格中获取字典数据。

我已经编写了查询来从我的mysql数据库中检索与我的单词相关的数据。我将从4个不同的表格中获取数据。

我写了下面发布的Index.php和data.php代码。问题与GT;>>>>>

1)当我运行inded.php文件时出现错误

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

2)我在编写data.php文件的代码时遇到了困难,请给我一些建议。我从多个表中检索数据,因此非常混乱。

请帮助我,我真的很挣扎,我很感激你的帮助。

谢谢

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

1 个答案:

答案 0 :(得分:3)

您收到第一个错误,因为javascript代码应位于<script>标记内,而不在其外..

试试这个..

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

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

你可以再次结束并启动php标签

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

注意:不推荐使用mysql使用mysqli PDO ...学习OOP创建类而不是基于程序并寻找一些php框架... CODEIGNITER,ZEND,YII等等