"icon")); ?> -6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:26:02 25 4
gpt4 key购买 nike

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

submission_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

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);
});
}

操作文件: submission_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

提前谢谢大家。

I'm working with Elgg version 2.X, and following this tutorial: Ajax Actionsbut I think this would be standadised (data exchange from HTML to Javascript to PHP for AJAX persistence)

最佳答案

向表单标记添加类或 ID。

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

然后在你的JS文件中

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

}

关于javascript - 如何将数据从 HTML 表单传递到处理 AJAX 调用的 JavaScript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38651742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com