dsyo9700 2014-02-18 13:38
浏览 104

当mysql数据库发生变化时,动态更改网站内容

I have a form like this in index.php file.

<form >
    <input type="text" id="fname" >
    <input type="text"  id="lanme" >
    <input type="button" id="submit" value="send">
</form>

I receive two input from user.when user click on send button a jquery function called and send fname and lname to record.php file.this is jquery function.

$(document).ready (function() {
    $("#submit").click(fucntion(){
        $fname=$("#fname").val();
        $lanme=$("#lname").val();
        $.post("record.php") , { fname: fname , lname : lname }).done(function(data){
            alert(data);
        });
    });
});

and this is record.php file

<?php 
    $fname=$_POST['fname'];
    $lanme=$_POST['lanme'];
    $mysqli=new mysqli(host,username,password,database);
    $stmt=$mysqli->prapare("INSERT INTO info (fname , lname) VALUES (?,?))";
    $stmt->bind_param("ss",$fname , $lname);
    $stmt->execute();
 ?>

In index.php I print the "table" .i use this code to print table in index.php file.

<?php
try {
    $dbh = new PDO('mysql:host=HOST;dbname=DATABASENAME', $user, $password);
    $sth = $dbh->prepare('SELECT * from info ');
    $sth->execute();
    $result = $sth->fetchAll();
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>
<table>
<tbody>
<tr>
    <td>fname</td>
    <td>lname</td>
</tr>
<?php foreach($result as $row) : ?>
<tr>
    <td><?php echo $row[0] ; ?></td>
    <td><?php echo $row[1] ; ?></td>
</tr> 
<?php endforeach ; ?>
</tbody>
</table>

All the above code work and I don't have any problems with it.

When user enters the value fname and lname and click on the send button, fname and lname send to the server and store in data base. If user wants to see result in table he/she must refresh page, but I want to show result on table without refreshing the page. Otherwise when user clicks on the send button and send the data the table in the index.php dynamically change and dynamically show the result to the user.

  • 写回答

4条回答 默认 最新

  • dongzenglin8292 2014-02-18 13:50
    关注

    You have to add the row manually (from js). In my script i use the underscore library:

    var template = _.template('<tr><td><%= fname %></td><td><%= lname %></td></tr>');
    
    $.post("record.php", {fname: fname, lname : lname}).done(function() {
        $('table tr:last-child').after(template({fname: fname, lname: lname});
    });
    

    BTW: You should use PDO or mysqli not both.

    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题