如何使用PHP

时间:2015-05-08 00:57:36

标签: javascript php html mysql ajax

我有三个文件,index.html,database.php和function.js。在我的database.php中,我创建了一个带有删除按钮的表单,以便在单击时执行delete sql查询。我的主要目的是显示一个显示记录的表和每个删除按钮,这样每当我单击删除按钮时,它就会执行SQL查询并从数据库中删除该特定行。

在我将ajax添加到javascript之前,它工作正常。现在,当单击“删除”按钮时,整个页面只会刷新。

  1. 如何在删除按钮上执行删除查询,使用我想在我的php文件中调用的javascript函数,而不创建/使用新文件?
  2. 我使用vi编辑器进行编码,所以除了IE的开发人员工具之外我没有任何调试方法。我的javascript文件似乎不起作用,因为在HTML文件中,表单在

    返回null
          onsubmit="return checkFields()";
    

    从我收到的错误中说明,但可能只是因为我的javascript文件中存在错误。

    P.S。我是PHP,javascript和ajax的新手,所以如果我犯了任何粗心或明显的错误,请原谅我。我也不知道任何jQuery或JSON。在最简单的解释中提供任何形式的帮助将不胜感激。

    这是index.html文件:

    <html>
     <head>
        <link rel="stylesheet" type="text/css" href="style.css"/>
        <script src="function.js" type="text/javascript"></script>
     </head>
    
     <body>
        <form name="infoForm" method="post" onsubmit="return checkFields()"  action="">
                <table>
                <tr>
                        <td>Name:</td>
                        <td><input type="text" name="name" id="name" maxlength="40"></td>
                </tr>
                <tr>
                        <td>Address:</td>
                        <td><textarea maxlength="45" name="address"id="address" ></textarea></td>
                </tr>
                <tr>
                        <td>Phone:</td>
                        <td><input type="text" name="phone" id="phone"  maxlength="20"><br></td>
                </tr>
                <tr>
                        <td>Gender:</td>
                        <td><input checked type="radio" name="gender" id="male" value="Male">Male
                        <input type="radio" name="gender" id="female" value="Female">Female</td>
                </tr>
                <tr>
                        <td>
                                Nationality:
                        </td>
                        <td>
                                <select name="nation">
                                  <option value="Singapore">Singapore</option>
                                  <option value="Malaysia">Malaysia</option>
                                  <option value="Thailand">Thailand</option>
                                  <option value="Indoensia">Indonesia</option>
                                  <option value="Philippines">Philippines</option>
                                </select>
                        </td>
                </tr>
                <tr>
                        <td></td>
                        <td>
                                <br><input type="reset" value="Cancel">
                                <input type="submit" name="result" value="Submit"/>
                        </td>
                </tr>
                </table>
        </form>
    
        <div id="divTable"></div>
     </body>
    </html>
    

    这是database.php文件:

    <?php
    
        // Define database parameters //
        DEFINE ('DB_USER' ,'iqwe');
        DEFINE ('DB_PASSWORD', 'inqwe123');
        DEFINE ('DB_HOST', 'localhost');
        DEFINE ('DB_NAME', 'hqwdqq');
    
        $table_info = "info";
    
        // Connect to database
        $conn = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to Database:'. mysql_error());
        @mysql_select_db (DB_NAME) OR die ('Could not select the Database: '.mysql_error());
    
        // Delete Row
        if(isset($_POST['delete'])){//java script function somewhere
         echo "<script>";
         echo "deleteRow()";
         echo "</script>";
    
         }
    
        //Check if phone no. is duplicate and if not, insert data
        if(isset($_POST['result'])){
        $phone = $_POST['phone'];
        $query_string = "select phone from $table_info where phone='$phone'";
        $result = @mysql_query($query_string);
        $num_row = mysql_num_rows($result);
        if($num_row){
         echo "A same phone number has been found. Please enter a different phone number.";
        }else{
        $query_string = "insert into $table_info(name, address, phone, gender, nation) values('".$_POST['name']."','".$_POST['address']."','".$_POST['phone']."','".$_POST['gender']."','".$_POST['nation']."')";
        $result = @mysql_query($query_string);
         }
        }
    
        // Display table
        $query_string = "select * from $table_info";
        $result = @mysql_query($query_string);
        $num_row = mysql_num_rows($result);
    
        if($num_row){
         echo "<table border=1>";
         echo "<tr><th>Name</th><th>Address</th><th>Phone no.</th><th>Gender</th><th>Nationality</th><th>Created</th><th>Modified</th><th>Action</th></tr>";
    
                 while($row = mysql_fetch_array($result)){
                 echo "<tr><td>", $row['name'], "</td>";
                 echo "<td>", $row['address'], "</td>";
                 echo "<td>", $row['phone'], "</td>";
                 echo "<td>", $row['gender'], "</td>";
                 echo "<td>", $row['nation'], "</td>";
                 echo "<td>", $row['createdTime'], "</td>";
                 echo "<td>", $row['modifiedTime'], "</td>";
                 ?>
    
                <!--Delete button-->
                <td><form id="delete" method="post" action="">
                <input type="hidden" name="deleteRow" value="<?php echo $row['user_id']; ?>"/>
                <input type="button" name="delete" value="Delete" onclick="return deleteRow(<?php echo $row['user_id']; ?>);"/></td></form></tr>
    
                <?php
                 }
                echo "</table>";
                }
         else{
          echo "0 results";
        }
      ?>
    
        <form method="post" action="index.html">
        <input type="submit" name="goBack" value="Back"/>
        </form>
    

    这是function.js文件:

    function checkFields(){
     var name = document.getElementById("name");
     var address = document.getElementById("address");
     var phone = document.getElementById("Phone");
    
      if(confirm('Do you want to submit')){
       if(name == null, name == ""||address == null, address == ""||phone == null, phone == ""){
        alert("Please fill in all your details.");
        return false;
        }
         else{
         var page = "database.php";
         var xmlhttp = new XMLHttpRequest();
    
      if(xmlhttp==null){
       alert("Your browser does not support AJAX!");
       return false;
      }
      xmlhttp.onreadystatechange=function(){
       if(xmlhttp.readyState==4 && xmlhttp.status==200){
       document.getElementById("divTable").innerHTML=xmlhttp.responseText;
       }
      }
      xmlhttp.open("GET", page, true);
      xmlhttp.send(null);
      return false;
     }
    }
     else{
      return false;
     }
    }
    
    function deleteRow(id){
     if(confirm("Are you sure you want to delete this contact?")){
     //$id = $_POST['user_id'];
     $query_string = "delete from $table_info where user_id='id';
     $result = mysql_query($result) or die ('Could not execute.'. mysql_error());
     return false;
     }
    }
    

1 个答案:

答案 0 :(得分:0)

看起来你错过了这一行末尾的结束双引号:

$query_string = "delete from $table_info where user_id='id';

应该是:

$query_string = "delete from $table_info where user_id='id'";

也可能存在其他错误。您应该学会使用浏览器的内置脚本调试功能(和/或如果您的浏览器没有,则下载一个)。例如,尝试Firebug和Firefox的Web Developer Toolbar。