开发者

How to save long text from jquery to mysql

How would 开发者_JAVA百科you send long text from jquery to mysql becuase i have been trying use the .load function and for some reason i cannot get the text through.

 Var new_text = "oehieifewfewiofeio";
 $("#test").load("actions.php?act=save&new_text="+new_text);

Thank you,


You should not normally save a string directly to a database from JQuery, which runs on the client. You should send it to the server-side code (PHP) first and then have the server save it to the database. You can do this using an AJAX call, for example.


Update after you described your problem in more detail

There are a couple of problems with your current approach. The msot serious problem is that URLs of over 2000 characters are a bad idea and will likely break. (Source)

Secondly from the documentation of .load:

The POST method is used if data is provided as an object; otherwise, GET is assumed.

You should use a POST instead of a GET if you are changing the state of the system.


I can provide you with the thought process behind posting data to your server via jQuery, but we're not going to write the code for you. You should attempt to implement this yourself and post specific questions you have if you encounter trouble.

You need two components:

  1. A client-side form with jQuery to accept input

  2. A server-side PHP script which accepts data and passes it to MySQL.

You can divide these components into two pages, one for outputting the HTML page for the client containing the form and JavaScript, and one for accepting the post data. Or, you can put both parts in a single page, and decide which action to perform based on the request method: GET serves up the page and POST accepts and inserts the data.

The flow of logic runs something like this:

  • User enters text in a form, and clicks "submit"
  • jQuery intercepts the form submit action, pulls the text from the field and sends it to your PHP script via $.post()
  • Your PHP script is invoked and accepts the data via $_POST
  • Your PHP script connects to the database and inserts the data

There are various additional niceities you can add, like displaying a loading spinner on your form until the postback is complete, and sending a response indicating success or failure from your PHP script back to the client.


Use this jquery plugin:

https://www.articlage.com/adrianillo/article/datauploader

you can upload large amount of text easily.

First of all download the script from: https://github.com/adrianillo/datauploader

Add reference to JQuery and DataUploader script

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script  src="js/ajaxdatauploader.js"></script>

After Initialize DataUploader

jQuery.ajaxdatauploader(
{
    additional:,
    data:,
    pagerequest:,
    piecelenght:,
    encodedata:,
    success: function (datauploader, status){ 
    }
    error: function (req, status, error) {               
    }
  }
);

aditional:Additional data you want to send to the server, here you can set one identifier what data are sending.

data: Data to send to the server.

pagerequerest: Page to send the data (aspx, php, jsp, ...).

encodedata: If true then it encodes the data by replacing all special characters with their UTF-8 escape sequences. On the server will have to decode the data. EG in aspx: HttpUtility.UrlDecode data = (data);

piecelenght: Size of the pieces to send. DataUploader divides data into pieces then is sending to the server.

When the data process sending this completed, it will take the function: 'success'. If there was a problem and could not complete the sending of data it will lead to the 'error' function.

Finally the server collects the data piece by piece until it is completed

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜