简体   繁体   English

如何从数据库中以更新形式获取文本字段的值

[英]How Can I Get The Value Of A Textfield From Database In Update Form

I'm using Yii framework Update form to get values from backend, I created a add/remove textfields feature in my form. 我正在使用Yii Framework Update表单从后端获取值,我在表单中创建了添加/删除文本字段功能。 I'm able to use the create form easily. 我可以轻松使用创建表单。 But, I have a problem in update form. 但是,我在更新表单时遇到问题。 Below is the code - 下面是代码-

$(function () {
    var json = {
        "welcomeList": ["Thanks for coming", "Please select from the following list", "dwadwadsds"],
            "endList": ["Press come again", "Press 0"]
    };

    $.each(json.welcomeList, function (_, vv) {
        $('<p><label for="p_scnts"><input type="text" class=cnt" size="20" name="p_scnt" value="' + vv + '"placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo('#p_scents');
    });

    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;

    $('#addScnt').live('click', function () {
        $('<p><label for="p_scnts"><input type="text" id="p_scnt_' + i + '" size="20" name="p_scnt_' + i + '" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
        i++;
        return false;
    });

    $('#remScnt').live('click', function () {
        if (i > 2) {
            $(this).parents('p').remove();
            i--;
        }
        return false;
    });
});

In the above code, I want to replace the hardcoded value of var json with the value from the database to the textfield. 在上面的代码中,我想用从数据库到文本字段的值替换var json的硬编码值。 How can I do this? 我怎样才能做到这一点?

You can do it like this, for example DefaultController 您可以这样做,例如DefaultController

public function actionIndex() {
    //Here you can get data from DB
    //and fill $result array, but I fill it manuallly

    $result = array(
        'welcomeList' => array(
            "Thanks for coming",
            "Please select from the following list",
            "dwadwadsds"
        ),
        'endList' => array(
            "Press come again",
            "Press 0"
        )
    );

    echo json_encode($result);
    Yii::app()->end();
}

Finally json hardcore in JS you can replace by this 最后JS中的json铁杆,您可以用此替换

var json = [];
$.getJSON( "/default", function( data ) {
    json = data;
});

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

相关问题 如何从表单容器中的文本字段获取和存储数据? - How can I get and store Data from Textfield in Form Container? 使用文本字段中的值更新隐藏的表单字段 - Update hidden form field with value from textfield 如何在不发布表单的情况下获取文本字段的值 - How do I get the value of a textfield without posting the form 如何根据 TextField 的值以编程方式更新 Material UI TextField 的 helperText? - How can I programmatically update the helperText of a Material UI TextField based on the value of the TextField? 如何从 reactJS 的另一个模块中的文本字段中访问值 - How can I acess value from textfield in another module in reactJS 如何从 html 中的复选框表单中获取值? - how can I get the value from a checkbox form in html? 如何从表单中获取查询参数值 - How I can get a query parameter value from my form 如何从表单中获取值并在控制器中使用它? - How can I get a value from a form and use it in a controller? 如何从残疾人那里获取表单值<input>元素 - How can I get the form value from a disabled <input> element 如何从React Native中的TextField获取值,由于空值而无法将数据存储到数据库 - How to get value from TextField in React Native, Unable to store data to database caused by null value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM