0

我是 web 开发的新手,我真的需要一些帮助。我想使用 ajax 显示一个 html 表。此代码来自两个不同的文件。我这样做对吗?

这是我的 index.html

<html>
    <body>

<script>
function loadXMLDoc()
{
    var xmlhttp;
    xmlhttp=new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","table.php",true);
    xmlhttp.send();
}
</script>

        <form action = "insert.php" method="post">

        Firstname: <input type="text" name="firstname"></br>
        Lastname: <input type="text" name="lastname"></br>
        Middlename: <input type="text" name="middlename"></br>

        <input type="submit" onclick="loadXMLDoc()">


        </form>
        <div id="myDiv"></div>

    </body>
</html>

这是我的table.php。当我单击提交按钮时,没有任何反应。有没有人可以告诉我我这样做是否正确?

<html>
    <body>
        <table border = 1>
            <tr>
                <th>FIRSTNAME</th>
                <th>LASTNAME</th>
                <th>MIDDLENAME</th>
                <th>DELETE</th>
            </tr>



    /*  <?php
                $con = mysqli_connect("localhost","root","","study");

                if (mysqli_connect_errno($con))
                    {
                        echo "Failed to connect to mysql" . mysqli_connect_error();
                    }

                    $result = mysqli_query($con,"SELECT * FROM sample_employers");

                    while($row=mysqli_fetch_array($result))
                    {
                        echo "<tr>";
                        echo "<td>" . $row['firstname'] . "</td>";
                        echo "<td>" . $row['lastname'] . "</td>";
                        echo "<td>" . $row['middlename'] . "</td>";
                        echo "<td> <input type='button' value='Delete' </td>"; 
                        echo "</tr>";
                    }

                    mysqli_close($con);
            ?>

        </table>
        </body>
</html>
4

3 回答 3

1

首先直接在您的浏览器中打开 ajax 页面,这是找出您将获得的 ajax 响应的最佳方法。其次,将您的 ajax 代码更新为:

$(function(){
    $.ajax({
        url     : 'table.php',
        data    : {},
        type    : 'GET',
        success : function(resp){
            $("#myDiv").html(resp);
        },
        error   : function(resp){
            //alert(JSON.stringify(resp));  open it to alert the error if you want
        }  
    });
});
于 2013-07-09T10:43:02.697 回答
0

there is something called client side scripting(with javascript) and server side language(with php in ths case)

You can have an AJAX call that will call your table.php and will get the response.

So all you have to do his code a ajax method in index.html.

  1. like you have coded above
  2. by jQuery reference of ajax http://www.w3schools.com/jquery/ajax_post.asp(tutorial)

jquery is a widely used library of javascript and surely you will find it interesting.

So i suggest to have two seperate files

  1. index.html that will have ajax method to call table.php
  2. table.php that will execute on ajax call and will return it the desired results

then its up to you how to display the results once you get.

于 2013-07-09T09:24:26.150 回答
0

试试这个代码 table.php

<?php

$con = mysqli_connect("localhost","root","","study");

if (mysqli_connect_errno($con))
    {
        echo "Failed to connect to mysql" . mysqli_connect_error();
    }
        echo '<table border = 1>';
        echo '<tr>';
            echo ' <th>FIRSTNAME</th>';
            echo '<th>LASTNAME</th>';
            echo ' <th>MIDDLENAME</th>';
            echo ' <th>DELETE</th>';
        echo ' </tr>';
    $result = mysqli_query($con,"SELECT * FROM sample_employers");
    while($row=mysqli_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>" . $row['firstname'] . "</td>";
        echo "<td>" . $row['lastname'] . "</td>";
        echo "<td>" . $row['middlename'] . "</td>";
        echo "<td> <input type='button' value='Delete' </td>"; 
        echo "</tr>";
    }
    mysqli_close($con);
    echo '</table>';
?>
于 2013-07-09T09:19:43.860 回答