简体   繁体   English

使用 AJAX 提交表单并将数据作为 JSON 发送到文件

[英]Submit form with AJAX and send data to file as JSON

I've been trying to figure this out for a while now.我一直试图弄清楚这一点。 I want to submit a form and store its data in a file as JSON.我想提交一个表单并将其数据作为 JSON 存储在一个文件中。 I've got to find part of the solution, using PHP to store the data into a file, but now I want to avoid redirection after the form is submitted.我必须找到解决方案的一部分,使用PHP将数据存储到文件中,但现在我想避免在提交表单后重定向。

HTML HTML

<form action="saveit.php" method="POST">
    <div class="form-group" id="vaga-group">
        <label for="vaga">Job</label>
        <input type="text" class="form-control" id="vaga" name="vaga" placeholder="Ex.: UX Designer, Desenvolvedor Java">
    </div>
    <div class="form-group" id="cidade-group">
        <label for="cidade">City</label>
        <input type="text" class="form-control" id="cidade" name="cidade" placeholder="Ex. São Paulo">
    </div>
    <div class="form-group" id="tipo-group">
        <label for="tipo">Type</label>
        <select class="form-control" name="tipo" id="tipo">
            <option>Full time</option>
            <option>Freelance</option>
        </select>
    </div>  
    <button class="btn btn-primary" type="submit">Send</button>
</form>

saveit.php保存.php

<?php
    $filetxt = 'js/list.json';

    $formdata = array(
        'vaga'=> $_POST['vaga'],
        'cidade'=> $_POST['cidade'],
        'tipo'=> $_POST['tipo']
    );

    $arr_data = array();  
    $jsondata = file_get_contents($filetxt);
    $arr_data = json_decode($jsondata, true);
    $arr_data[] = $formdata;
    $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);
    file_put_contents('js/list.json', $jsondata);

    $form_state['redirect'] = false;

?>

By submitting exactly at it is right now my list.json file is updated with new information, as seen below:通过完全提交现在我的list.json文件更新了新信息,如下所示:

[
    {
        "vaga": "Desenvolvedor Front-End",
        "cidade": "New York",
        "tipo": "Freelance"
    },
    {
        "vaga": "Desenvolvedor Java",
        "cidade": "Chicago",
        "tipo": "Freelance"
    }
]

Now I want to stop the redirection from happening after the form is submitted, using AJAX.现在我想在提交表单后使用 AJAX 停止重定向。 I understand that because I'm using action="saveit.php" it is redirecting to that file (or to wherever I pointed out in that file), but I'm failing to find the proper use of AJAX to validate and submit the form without redirection.我知道因为我正在使用action="saveit.php"它正在重定向到该文件(或我在该文件中指出的任何地方),但是我没有找到正确使用 AJAX 来验证和提交没有重定向的表单。

I've found tons of examples of AJAX code to stop redirection, but none of them let me keep storing new data into list.json after submission, and that's the problem.我发现了大量的 AJAX 代码示例来停止重定向,但没有一个让我在提交后继续将新数据存储到 list.json 中,这就是问题所在。 Please, help!请帮忙! Thank you!谢谢!

  1. Get rid of the forms action摆脱表单action

  2. Using jQuery (orXMLHTTPRequest if you are feeling bold)使用 jQuery(或者 XMLHTTPRequest,如果你觉得大胆的话)

(pop it in a <script> tag) (将其弹出到<script>标签中)

function ajaxSubmit(){
  $.post('saveit.php', {
    vaga: document.getElementById('vaga').value,      // all the values you
    cidade: document.getElementById('cidade').value,  // want to send
    // and so on ...
  }).done(function(result){
    // result = server result
    // do what you must to notify client here
  }).fail(function(err){
    // oh dear ... error. tell user
  })
}
  1. change HTML button更改 HTML button

<button class="btn btn-primary" onclick='ajaxSubmit()'>Send</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM