简体   繁体   English

Joomla和JS; 将数据从表单发送到控制器

[英]Joomla & JS; Sending data from form to Controller

I am having trouble with a form I created and trying to redirect the data someplace else by a JavaScript method that I call whenever the submit button is clicked. 我在创建表单时遇到了麻烦,并尝试通过单击提交按钮时调用的JavaScript方法将数据重定向到其他地方。

The first thing that is happening, is that the form is created and filled in, then the user will click on the HTML generated button with this action in it: 发生的第一件事是创建并填写了表单,然后用户将单击其中包含此操作的HTML生成按钮:

onclick="javascript:saveEssayScore(<?php echo $key . "," . $pid; ?>);"

Secondly the script file is read and performed. 其次,读取并执行脚本文件。 The code is below: 代码如下:

<script type="text/javascript" language="javascript">
document.body.className = document.body.className.replace("modal", "");

function saveEssayScore(key, pid){

    var user_id = document.getElementById("user-"+key).value;
    console.log("user " + user_id);
    var grade = document.getElementById("grade-"+key).value;
    console.log("grade " + grade);
    var teacher_answer = document.getElementById("feedback-"+key).value;
    console.log("teacher_answer " + teacher_answer);
    var question_id = document.getElementById("question-"+key).value;
    console.log("question_id " + question_id);
    var quiz_id = document.getElementById("quiz-"+key).value;
    console.log("quiz_id " + quiz_id);

    var req = new Request.HTML({
        method: 'post',
        url: "<?php echo JURI::base();?>index.php?option=com_guru&controller=guruAuthor&task=saveFeedback&tmpl=component&format=raw",
        data: { 'pid' : pid, 'user_id' : user_id, 'grade' : grade, 'teacher_answer' : teacher_answer, 'question_id' : question_id, 'quiz_id' : quiz_id},
        onSuccess: function(response, responseElements, responseHTML){
           alert('yeyy');
        }
    }).send();
}

Now in the URL, it is read to run the method (task) saveFeedback() in the controller guruAuthor which is in the component com_guru . 现在在URL中,将读取它以在组件com_guru中的控制器guruAuthor运行方法(任务) saveFeedback()

The problem I am having is, how do I read the data that I just send through the HTML request? 我遇到的问题是,如何读取刚刚通过HTML请求发送的数据? I am trying stuff like $user_id = JRequest::getVar("user_id"); 我正在尝试类似$user_id = JRequest::getVar("user_id"); But whenever I'm trying to echo or dump, it is returned to me but empty. 但是,每当我尝试回显或转储时,它都会返回给我,但为空。 Not the values that I am dumping in JavaScript console.log(user_id); 不是我在JavaScript console.log(user_id);转储的值console.log(user_id);

JRequest is deprecated JRequest弃用

Use JInput instead. 改用JInput。 Read - Retrieving request data using JInput 读取- 使用JInput检索请求数据

In your mentioned code - if the controller function is being called, try this code- 在您提到的代码中-如果正在调用控制器功能,请尝试以下代码-

$jinput = JFactory::getApplication()->input;
$user_id = $jinput->get('user_id', '', 'INT');

I hope this helps. 我希望这有帮助。

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

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