dsh8009271 2016-08-18 15:28
浏览 65

表单没有提交,页面正在重新加载php

I have been trying to submit a form using jquery ajax and trying that page should not reload or refresh but none of those objectives are getting accomplished. Am I missing anything?

form

<form method="POST" id="formReviews">
 <div>
  <label><b>Name</b></label>
  <input style="width:400px;" type="text" class="form-control" placeholder="Enter your name" name="txtName" required>
  </div>

  <div>
  <label><b>Area</b></label>
  <input style="width:400px;" type="text" class="form-control" placeholder="Enter the area you would want to improvise" name="txtArea" required>
 </div>

<div>
<label><b>Details</b></label>
<textarea style="width:400px;" type="comment" class="form-control" row="5" placeholder="Enter the some details" name="txtDetails" required></textarea>
</div>
 <br>
<div>
  <button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>

jquery:

$(document).on('submit','formReviews',function(e){
  $.ajax({
    url: $(this).attr('action'),
    type: $(this).attr('method'),
    data: $(this).serialize(),
    success: function(html){
        alert('Review Submitted');
    }
  });
    e.preventDefault();  
  });

reviews.php

<?php
$servername = "localhost:3306";
$username = "mebe";
$password = "bethe";
$dbname = "SpecsCompare";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql="INSERT INTO reviews (Name, Area, Details) VALUES
     ('".$_POST['txtName']."','".$_POST['txtArea']."','".$_POST['txtDetails']."')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
  • 写回答

1条回答 默认 最新

  • douhun8647 2016-08-18 15:31
    关注

    You should use a # sign in front since you are selecting the form id.

    Also, use e.preventDefault in the very first line.

    $(document).on('submit','#formReviews',function(e) {
      e.preventDefault();  // Add it here 
      $.ajax({
        url     : $(this).attr('action'),
        type    : $(this).attr('method'),
        data    : $(this).serialize(),
        success : function(html){
            alert('Review Submitted');
        }
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接