我如何使用AJAX post方法将表单提交导出到CSV文件?

时间:2017-12-03 13:27:25

标签: php jquery html ajax csv

//这是我的表格

<form id="blog-input" method="post">
  Title: <br>
  <input type="text" id="title" name="title"><br>
  Email: <br>
  <input type="text" id="email" name="email"><br>
  Description: <br>
  <input type="text" id="description" name ="description"><br>
  Type: 
  <select>
    <option value="sport">Sport</option>
    <option value="news">News</option>
  </select><br>
  Post: <br>
  <textarea name ="post" id="content2" rows="10" cols="100"></textarea><br>
  <input type="submit" value="Submit">
<form>

//使用AJAX post方法的JavaScript

$(function(){
  $('#blog-input').on('submit', function(e){
    e.preventDefault();
    var title =$('#title').val();
    var email= $('#email').val();

    $.ajax({
      type: 'POST',
      url: 'saveBlog.php',
      data: {email: email},
      success: function(){
        alert('Your blog post has been submitted');
      }
    });
  });
});

在saveblog.php中

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $title = strip_tags(trim($_POST["title"));
  $title = str_replace(array("\r","\n"),array(" ", " "),$name);
  $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
  $content = trim($_POST["content"]);

  file_put_contents($blogposts, $title)
}

我想将用户提交的值提交给CSV文件。我也希望能够回忆起用于编辑的博客文章等。谢谢

0 个答案:

没有答案