解析来自ajax调用的JSON数据

时间:2021-10-07 07:46:51

I have posted a few questions previously on how to do the same thing, and people have pointed me in the right direction. So my apologies if this is getting repetitive. However I am posting this time to see if someone could spare a couple of seconds to help me with javascript.

我之前已经发布了一些关于如何做同样的事情的问题,人们已经给我指明了正确的方向。如果这是重复的,我很抱歉。不过,我这次是想看看是否有人能抽出几秒钟的时间来帮助我学习javascript。

I have a form that already submits data into a different table, and want to implement a "pre-canned response feature". This would simply be a drop list selection that when the onChange event fires of an option list it runs the javascript that populates a following text area with appropriate data.

我有一个表单,它已经将数据提交到另一个表中,并希望实现“预先固定的响应特性”。这仅仅是一个drop list选择,当一个选项列表的onChange事件触发时,它运行的javascript将使用适当的数据填充下面的文本区域。

I believe as it is already in a form, I can't simply put it in another form and use submit, also, I want to be able to do this without refreshing the page.`

我相信,由于它已经是一种形式了,我不能简单地将它放在另一种形式中并使用submit,而且,我希望能够在不刷新页面的情况下做到这一点。

<form action="<?=base_url();?>ticket/addmessage/<?=$ticket_details['id'];?>/" enctype="multipart/form-data" method="post" name="message">

        <label for="frm_precan">Canned Response</label>
        <span class="input">                        
            <select id="frm_precan" name="precan" onchange="updateText()">
                <option value="">--Please Select--</option>
                <?php foreach($precan_list as $precan) :?>
                    <option value="<?=$precan['id']; ?>"><?=$precan['name'];?></option>
                <?php endforeach; ?>            
            </select>
        </span>

        <ul>
            <li><label for="message">Message<span class="req">*</span></label><span class="input"></span><br/></li> 
        </ul>   
        <textarea style="width: 100%; margin: 0; padding: 0; border-width: 1; font-family: courier;" name="message" rows="10" id="text_area"></textarea>
        <button type="submit"><span>Add Message</span></button>

</form>

This is my HTML form. It uses PHP via SQL to populate the option list, as the pre-canned messages are stored in SQL table.

这是我的HTML表单。它使用PHP通过SQL填充选项列表,因为预先录制的消息存储在SQL表中。

So what I need to do is somehow link this Javascript (that is called on the onChange):

所以我需要做的是将这个Javascript (onChange调用)链接起来:

        function updateText()
       {
       var message = document.getElementById('frm_precan').value;
       $('#text_area').val(message)
       };

(very basic, I know, but this is where I struggle) So this code wants to pass the ('frm_precan').value; (which is the ID in the table, and the field by which I want to query the correct message)... to a php file that looks like this:

(我知道这是非常基本的,但这是我的困难所在)所以这段代码想要传递('frm_precan').value;(这是表中的ID,以及我想要查询正确消息的字段)…对于如下所示的php文件:

    public function get_message($message_id)
    {
        $sql_list  =
        "
        SELECT *
        FROM  ".$this->tables_automessages."
        WHERE id = '".mysql_real_escape_string($message_id)."'";

        $query = $this->db->query($sql_list);
        if($query->num_rows() > 0)
            {
            foreach ($query->result_array() as $row)
                {
                $return[] =
                array(
                        'message'                   =>  $row['message']

                    );
                }
            return $return;
            }
        else return false;
        }
}

O, and I am using code ignitor, so this could also be a reason why I am getting confused. So the variable wants to come out of the javascript into a controller then go to the SQL query.

O,我正在使用代码点火器,所以这也可能是我感到困惑的原因。所以变量想要从javascript中取出到控制器然后转到SQL查询。

If someone can understand what I mean, I will be amazed... and very grateful.

如果有人能理解我的意思,我会很惊讶……,非常感激。

2 个解决方案

#1


1  

To use post in your ajax call:

要在ajax调用中使用post:

function updateText()
{
    $.ajax({
        url: '/controller/get_message',
        data: {message_id:$('#frm_precan').val()},//no need to json encode, jQ does this for you
        type: 'POST',
        dataType: 'json',//or omit, jQ does an intelligent guess
        success: function(response)
        {
            console.log(responese);
            //function will be called when the ajax call is completed
        }
    });
}

In your controller, the get_message member function could look like this:

在您的控制器中,get_message成员函数可以是这样的:

public function get_message()
{
    $id = $this->input->post('message_id');//=== data key used to send request
    //do your query 
}

Here is a similar question BTW
And here, you can find all sorts of things you can specify with the jQuery.ajax method ($.ajax is the same thing, $ and jQuery are synonyms)

顺便说一下,这里有一个类似的问题,在这里,您可以找到各种可以使用jQuery指定的内容。ajax方法(美元。ajax也是一样,$和jQuery是同义词)

#2


2  

Your updateText function can be changed to this to get the value from your controller and put the resulting text in the text area.

可以将updateText函数更改为this,以便从控制器获取值,并将结果文本放入文本区域。

function updateText()
{
    var messageId = $('#frm_precan').val();
    $.get('/yourcontroller/get_message/' + messageId, function(data) {
        $('#text_area').val(data)
    });
}

Looking at your get_message function you should either change it to return just the message text instead of an array or else you'll need to json_encode the results and change the jQuery .get call to .getJSON instead.

查看get_message函数,您应该将其更改为只返回消息文本,而不是返回数组,或者需要json_encode结果并更改jQuery .get调用. getjson。

#1


1  

To use post in your ajax call:

要在ajax调用中使用post:

function updateText()
{
    $.ajax({
        url: '/controller/get_message',
        data: {message_id:$('#frm_precan').val()},//no need to json encode, jQ does this for you
        type: 'POST',
        dataType: 'json',//or omit, jQ does an intelligent guess
        success: function(response)
        {
            console.log(responese);
            //function will be called when the ajax call is completed
        }
    });
}

In your controller, the get_message member function could look like this:

在您的控制器中,get_message成员函数可以是这样的:

public function get_message()
{
    $id = $this->input->post('message_id');//=== data key used to send request
    //do your query 
}

Here is a similar question BTW
And here, you can find all sorts of things you can specify with the jQuery.ajax method ($.ajax is the same thing, $ and jQuery are synonyms)

顺便说一下,这里有一个类似的问题,在这里,您可以找到各种可以使用jQuery指定的内容。ajax方法(美元。ajax也是一样,$和jQuery是同义词)

#2


2  

Your updateText function can be changed to this to get the value from your controller and put the resulting text in the text area.

可以将updateText函数更改为this,以便从控制器获取值,并将结果文本放入文本区域。

function updateText()
{
    var messageId = $('#frm_precan').val();
    $.get('/yourcontroller/get_message/' + messageId, function(data) {
        $('#text_area').val(data)
    });
}

Looking at your get_message function you should either change it to return just the message text instead of an array or else you'll need to json_encode the results and change the jQuery .get call to .getJSON instead.

查看get_message函数,您应该将其更改为只返回消息文本,而不是返回数组,或者需要json_encode结果并更改jQuery .get调用. getjson。