簡體   English   中英

PHP + jQuery + MySQL +表單操作

[英]php + jquery + mysql + form action

我正在使用php和jquery創建表單以將數據插入數據庫而不刷新頁面,但是問題是頁面刷新並將我定向到php頁面,任何人都可以幫助我

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>feedback page</title>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link rel ="stylesheet" href = "css/default.css" />

<script type = "text/javascript">

$(function(){

   $('#submit').click(function(){
     $('#container').append('<img src = "img/loading.gif" alt="Currently loading" id = "loading" />');

         var name = $('#name').val();
         var email = $('#email').val();
         var comments = $('#comments').val();

           console.log(name, email, comments);
        return false;

   });


});

</script>




</head>

<body>
   <form action = "submit_to_db.php" method = "post">
   <div id = "container">
      <label for = "name">Name</label>
      <input type = "text" name = "name" id = "name" />

      <label for = "email">Email address</label>
      <input type = "text" name = "email" id = "email" />

      <label for = "comments">Comments</label>
      <textarea rows = "5"cols = "35" name = "comments" id = "comments"></textarea>
      <br />

      <input type = "submit" name = "submit" id = "name" value = "send feedBack" />
    </div>
   </form>



   </div>
</body>
</html>

Submit_to_db.php

<?php
  $conn = new mysqli('localhost', 'root', 'root', 'my_db');
  $query = "INSERT into comments(name, email, comments) VALUES(?, ?, ?)";

  $stmt = $conn->stmt_init();
  if($stmt->prepare($query)){

     $stmt->bind_param('sss', $_POST['name'], $_POST['email'], $_POST['comments']);
     $stmt->execute();

  }

  if($stmt){

  echo "thank you .we will be in touch soon";
  }
  else{
   echo "there was an error. try again later.";
   }  


?>

更換

<input type = "submit" name = "submit" id = "name" value = "send feedBack" />

通過

<input type = "submit" name = "submit" id = "submit" value = "send feedBack" />

注意: id

您還應該在(form).submit();上觸發事件(form).submit(); 而不是('submit').click();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM