简体   繁体   English

将数据从textarea保存到HTML中的数据库

[英]Saving data from textarea to database in html

I´m looking for a way to save data from an input field to database. 我正在寻找一种将数据从输入字段保存到数据库的方法。

Now, on the HTML-page is a simple textarea -Field. 现在,在HTML页面上是一个简单的textarea -Field。 If the user presses the button to save the data a javascript function executed. 如果用户按下按钮以保存数据,则将执行javascript函数。

This javascript function get´s the data from the textarea with this statement: 此javascript函数使用以下语句从textarea获取数据:

test = $('#test1').val();

And then it will be given to the php-function with this statement. 然后将通过此语句将其提供给php函数。

 $.get(url+"test/saveTest/"+test, function (o){ 
        //Do something
  });

It works, but if I add a linebreak in textarea it will be lost. 它有效,但是如果我在textarea中添加换行符,它将丢失。

Anybody nows a solution? 现在有人解决吗? Or an alternative to textarea-input field? 还是textarea输入字段的替代方法?

https://jsfiddle.net/c7y0p6jw/ https://jsfiddle.net/c7y0p6jw/

Instead of $.get I would recomend to use $.post or $.ajax because $.get is like the name says to get data not to post data. 我建议使用$.post$.ajax代替$.get因为$.get就像名称中所说的那样获取数据而不是发布数据。

Ajax example Ajax示例

$.ajax({
    type: "POST",
    url: url + "test/saveTest/",
    data: {'test': test},
    dataType: "json",
    success: function (){
        // do what you want when the submit was successfully
    },
});

PHP PHP

$test = json_decode($_POST['test']);

For the problem with displaying the line breaks you can output your database field by using 对于显示换行符的问题,您可以通过使用输出数据库字段

echo nl2br(DATABASE FIELD);

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

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