MYSQL数据查询不是刷新

时间:2014-07-02 08:36:48

标签: javascript php html mysql

我有这个HTML应用程序,它使用数组在数据库中插入一些信息并在同一个表中检索相同的数据。讨厌的是当我点击sumbit时它转到insert.php页面然后刷新回来到add.php页面,当它刷新回add.php页面时,添加的新数据将会出现,除非我再次刷新add.php页面,以便它将检索新数据。

我希望在不刷新页面的情况下添加数据。

任何Adive。 代码低于* 谢谢......


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
    <title>Settings Administration</title>
    <?php

        $host="localhost"; // Host name 
        $username="user"; // Mysql username 
        $password="password"; // Mysql password 
        $db_name="db"; // Database name 
        $tbl_name="table"; // Table name 
        // Connect to server and select database.

        mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 

        mysql_select_db("$db_name")or die("cannot select DB");
        $sql="SELECT * FROM $tbl_name";
        $result=mysql_query($sql);
        ?>

    <script>
        var originalCount = 1;
        var realCount = 0;
        function removeRowFromTable()
        {
            try
            {
                var table = document.getElementById('SlideTable');
                var rowCount = table.rows.length;

                for(var i=0; i<rowCount; i++)
                {
                    var row = table.rows[i];
                    var chkbox = row.cells[0].childNodes[0];
                    if(null != chkbox && true == chkbox.checked)
                    {
                        if(rowCount <= 1)
                        {
                            alert("Cannot delete all the rows.");
                            break;
                        }
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }
                }
            }
            catch(e)
            {
            alert(e);
            }
        }

        function addRowToTable()
        {

            var table = document.getElementById('SlideTable');

            var rowCount = table.rows.length;
            //Used for reference only
            realCount = rowCount;

            var row = table.insertRow(rowCount);

            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            element1.checked = false;
            element1.name="chkbox[]";
            cell1.appendChild(element1);


            var cell2 = row.insertCell(1);
            var element2 = document.createElement("input");
            element2.type = "text";
            element2.value = "30";
            element2.size = 10;
            element2.name = "slideTime[]";
            cell2.appendChild(element2);

            var cell3 = row.insertCell(2);
            var element3 = document.createElement("input");
            element3.type = "text";
            element3.value = "/path";
            element3.size = 50;
            element3.name = "slideSource[]";
            cell3.appendChild(element3);


              var cell4 = row.insertCell(3);
            var element4 = document.createElement("input");
            element4.type = "text";
            element4.value = "/path";
            element4.size = 50;
            element4.name = "slideComment[]";
            cell4.appendChild(element4);

            // To keep track of index
            originalCount = originalCount + 1;
        }

        function validateRow(frm)
        {
             var inputs = document.getElementsByTagName('input');
            for(var i = 0; i < inputs.length; ++i)
            {
                if(inputs[i].type == 'text')
                   if(inputs[i].value == '') 
                   {
                      alert("Form could not be sent. All text fields should be NOT empty.");
                      return;
                   }
            }

            // submit
            frm.submit();
        }

        function setNumberOfSlides()
        {
            document.getElementById('numberOfSlides').value=realCount;
        }

    </script>

</head>

<body>

    <form action="insert.php" method="post">
        <p>
            <h1>Fill the slides in this table</h1>
        </p>
        <table id="SlideTable" border="1">
            <tr>
                <td>Select to Remove</td>
                <td>Slide Time (sec)</td>
                <td>Slide Source</td>
                <td>Slide Comment</td>
            </tr>
            <? while($rows = mysql_fetch_array($result)){ ?>
            <tr>
                <td><input type="checkbox" name="chk"/></td>
                <td><input size=10 name="slideTime[]" type="text" id="slideTime" value="<? echo $rows['SlideTime']; ?>"/></td>
                <td><input size=50 name="slideSource[]" type="text" id="slideSource" value="<? echo $rows['SlideSource']; ?>"/></td>
                <td><input size=50 name="slideComment[]" type="text" id="slideComment" value="<? echo $rows['SlideComment']; ?>"/></td>

            </tr>
            <? } ?>
        </table>
        <p>
            <input type="button" size="15" value="Add" onclick="addRowToTable();" />
            <input type="button" size="15" value="Remove" onclick="removeRowFromTable();" />
            <INPUT TYPE="button" onClick="history.go(0)" VALUE="Refresh">
        </p>
        <p>
            <input type="button" size="35" value="Submit" onclick="validateRow(this.form);" />
        </p>
        <input type="hidden" id="numberOfSlides" name="NumberOfSlides" value="">
    </form>
          <?php
        mysql_close(); //close database
        ?>
</body>

Insert.php

<html>
<head>
<title>Insert</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META HTTP-EQUIV="refresh" CONTENT="5; URL=http://www.h2os.com.au/add.php">
<?php
$host="localhost"; // Host name 
$username="user"; // Mysql username 
$password="password"; // Mysql password 
$db_name="db"; // Database name 
$tbl_name="table"; // Table name 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysql_select_db("$db_name")or die("cannot select DB");

// Delete all the old data
$old_data_sql="DELETE FROM $tbl_name";
mysql_query($old_data_sql);

$slideTimeArray;
$slideSourceArray;
$slideCommentArray;
foreach ($_POST['slideTime'] as $i => $value) 
{
    $slideTimeArray[]=$value;
}

foreach ($_POST['slideSource'] as $i => $value) 
{
    $slideSourceArray[]=$value;
}
foreach ($_POST['slideComment'] as $i => $value) 
{
    $slideCommentArray[]=$value;
}

// String to add values
$values_sql="";
for($k=0;$k<count($slideTimeArray);++$k)
{
    $values_sql .= "             ('$slideTimeArray[$k]','$slideSourceArray[$k]','$slideCommentArray[$k]') ";
    if($k==count($slideTimeArray)-1)
        $values_sql .= "; ";
    else
    $values_sql .= ", ";
}

// Insert in the database
$sql="INSERT INTO $tbl_name(SlideTime,SlideSource,SlideComment) Values".$values_sql;
$result=mysql_query($sql);
mysql_close();
?>
<style type="text/css">
<!--
.style1 {
    font-size: x-large;
    font-style: italic;
    font-weight: bold;
}
-->
</style>
</head>
<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div align="center" class="style1">
  <p>Your new Slide show Settings is saved...You Can Check the New settings by clicking             The View Settings Button.</p>
      <p>Thank you</p>
  >
</div>
</body>
</html>

0 个答案:

没有答案