简体   繁体   English

如何将数据从HTML表单传递到处理AJAX调用的JavaScript文件

[英]How can I go about passing data from a HTML form to a JavaScript file that handles AJAX calls

How can I go about passing data from a HTML form to a JavaScript file that handles AJAX calls? 如何将数据从HTML表单传递到处理AJAX调用的JavaScript文件?

submission_form.php submit_form.php

<?php echo elgg_view("input/file", array("name" => "icon", "class" => "icon")); ?>

<p>
    <?= elgg_view('input/text', ["name" => 'title', "class" => "title"]); ?>
</p>

submit.js Submit.js

function myajax_function(){
    var Ajax = require('elgg/Ajax');
    var ajax = new Ajax();

    ajax.action('service_comments/add_service_comment', {
        data: {
            picture: 1,
            description: 2
        },
    }).done(function (output, statusText, jqXHR) {
        if (jqXHR.AjaxData.status == -1) {
            return;
        }
        alert(output.sum);
        alert(output.product);
    });
}

action file : submission_form.php 动作文件: submitt_form.php

// Not working
$title = get_input('title');

// I'm not sure how to go about extracting the file from input/file and text from input/text

Thank you all in advance. 谢谢大家。

I'm working with Elgg version 2.X, and following this tutorial: Ajax Actions but I think this would be standadised (data exchange from HTML to Javascript to PHP for AJAX persistence) 我正在使用Elgg 2.X版,并按照本教程进行操作:Ajax操作,但是我认为这是标准的(从HTML到Javascript到PHP的数据交换,以实现AJAX持久性)

add a class or ID to the form tag. 在表单标签中添加一个类或ID。

echo elgg_view_form('submission_form', array('class' => 'elgg-form-submission_form'), array());

then in your JS file 然后在您的JS文件中

$(".elgg-form-submission_form").submit(function( event ) {
    $.post("service_comments/add_service_comment", $(this).serialize(), function(result) {

}

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

相关问题 我该如何使用Java编写HTML表单 - How should I go about this html form with Javascript 使用JavaScript或AJAX从带有输入的HTML表单中提取数据,然后将其传递给PHP - Extracting data from HTML form with inputs with JavaScript or AJAX and then passing it on to PHP 如何将数据从HTML表单获取到JavaScript文件并添加MySQL? - How can I get data from a HTML form to JavaScript file and to add MySQL? 用Javascript处理来自JSF Ajax的数据响应 - Handles data response from JSF Ajax in Javascript 将数据从 html 表单传递到 Javascript object - Passing data from an html form into a Javascript object 如何使用PHP和AJAX从html表单和javascript数组获取数据 - How can get data from html form and javascript array using PHP and AJAX 如何在提交表单的HTML文件中显示使用post方法提交的值? - How do I go about displaying the values submitted using the post method In the HTML file which the form was submitted to? 如何将JSON数据嵌入到HTML文件中? - How would I go about embedding JSON data into my HTML file? 在使用Javascript或jQuery传递隐藏参数的同时,如何进入html页面? - How can I go to an html page while passing a hidden parameter using Javascript or jQuery? 如何进行 JavaScript 调用以从网站上抓取数据? - How can I make JavaScript calls to scrape data from a website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM