简体   繁体   中英

How to store value entered in an input textfield in a database

I am using springsourcetoolsuite, grails project and I am coming across this problem of storing the value entered in the textfield into a table in the database created in mysql and connected to grails. Now I have a domain class called property having variables address, city, zipcode, country etc. which are also fields of the table property in mysql database. When I ask user to fill in using this piece of code-(gsp views)

<body>
<g:textField name="address" maxlength="40" value="${propertyInstance?.address}" />
</body>

it works and the value is stored in database. However I am required to append an input field on each button click, so i have put this input field in a function called add(). Here is the code-

<head>
<script type="text/javascript">
function add() {
    var newP = document.createElement("p");
    var input1,
    area = document.getElementsByTagName("form")[0];
    input1 = document.createElement("input");
    input1.type = "g:textField";
    input1.placeholder = "street";
    input1.value = "${propertyInstance?.address}";
    newP.appendChild(input1);
    area.appendChild(newP);
}
</script>
</head>
<body>
<g:form name='prop' method="post" action="save">
<input type="button"  value="+Add" onclick= "add();" ><br>
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
</g:form>
</body>

Now when i do this and run it, it takes null value and prints an error saying 'address cannot be null'. Now i cannot see what is wrong, but if anyone is familiar with groovy/javscript.. please help me figure out whats wrong. Thanks a lot.

I'm guessing you did not use the scaffolding feature to generate your views in first place. If you didn't, it's a good way to start understanding the basics of grails. In your case specifically, you need to put your fields that you want to pass to the controller (like address) inside the form tag. Like:

<body>
    <g:form name='prop' method="post" action="save">
        <g:textField name="address" maxlength="40" value="${propertyInstance?.address}" />
        <input type="button"  value="+Add" onclick= "add();" ><br>
        <g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
    </g:form>
</body>

Another thing is you can't create a tag input and put its type as "g:textfield". The html input fields only have limited types. The one you want to use in this case is really "text". In any case, the grails' tags are rendered before the javascript (in the server-side) while javascript code is rendered client-side. So the first time the page is rendered they will work. But to insert something dynamically in your page, you need ajax because the grails' tags are already rendered. The value ${propertyInstance?.address} needs to be processed at the server, returned and established in your field. Therefore you need to make an async request. Anyway JQuery is your guy. Also, for what you're doing, JQuery helps to manipulate HTML DOM, that will make your work so much easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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