Javascript PHP-如何避免在更改路由时重新加载表单

Javascript PHP-如何避免在更改路由时重新加载表单,javascript,php,jquery,ajax,single-page-application,Javascript,Php,Jquery,Ajax,Single Page Application,我在index.php中有一个简单的表单: <div class="form-container"> <form id="create-form" action="create.php" method="POST"> <label for="name">Name:</label> <input type=&quo

我在
index.php
中有一个简单的表单:

<div class="form-container">
    <form id="create-form" action="create.php" method="POST">
      <label for="name">Name:</label>
      <input type="text" id="name" name="name">
      <br/>
      <label for="score">Amount:</label>
      <input type="number" id="score" name="score">
      <br/>
      <input type="submit" name="addBtn" id="addBtn" value="Add" />
    </form>
</div>
目前,我在
index.js
中使用JQuery和AJAX编写了这个脚本,但由于调用
index.php
而一直在重新加载页面

$(document).ready(function () {
  $('#create-form').submit(function(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: 'create.php',
        data: $(this).serialize(),
        success: function(response)
        {
            console.log('agregado');
            location.href = 'index.php';
       }
   });
 });
});
目前,这很好,但在单击按钮和重新加载数据时(在
create.php
中的最后一行),会重新加载页面。我试图做的是实现AJAX和/或JQuery,以避免这种情况,并拥有一个完整的单页应用程序


免责声明:我开始学习PHP。所以,我犯了任何错误,请首先让我知道。我会注意你的回答。

试着在评论中对代码
location.href='index.php'
success
方法中。 就像这样:

$(document).ready(function () {
  $('#create-form').submit(function(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: 'create.php',
        data: $(this).serialize(),
        success: function(response)
        {
            console.log('agregado');
           // location.href = 'index.php';
       }
   });
 });
});

有很多Ajax示例。。。你试过什么吗?这个问题已经解决了。SQL查询对于SQL注入是开放的。在little bobby tables发现您的formI需要更新进入
index.php
路径的数据之前,请查看准备好的语句。
$(document).ready(function () {
  $('#create-form').submit(function(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: 'create.php',
        data: $(this).serialize(),
        success: function(response)
        {
            console.log('agregado');
           // location.href = 'index.php';
       }
   });
 });
});