An example analysis of the line-wrapping problem of Ajax asynchronous submitting data return value _ajax related

Source: Internet
Author: User
Tags commit

An example of this paper is to analyze the wrapping problem of the return value of Ajax asynchronous commit data. Share to everyone for your reference, specific as follows:

The previous analysis describes the JavaScript based on Ajax callback function transfer parameters and "IE browser and FF browser on the value of the ajax transfer parameter is Chinese," learning the Ajax callback function to participate in different browsers to deal with Chinese garbled problem method. Here's an analysis of the processing techniques of the wrapping problem in the Ajax asynchronous commit return value.

Background information:

The features you need to implement are: Select a category in the dropdown box, submit to PHP to return the member names under the category according to the selected category, and return the names of the members to the textarea in the form of a newline.

HTML code:

<form action= "Setduty.php?action=add" method= "post" name= "Setform" id= "Setform" >
<p> <label> Type:</label>
  <select name= "type" id= "Typeduty" >
  <option value= "-1" > Selection type </option>
  <option value= "1" > Duty chief </option>
  <option value= "2" > Duty director </option>
  <option Value= "3" > on duty </option>
  </select>
</p>
<p class= "Opertext" > 
    < Label> member:</label> <textarea name= "names" id= "names" cols= "a"
   rows= "ten" ></textarea>
</p>

JQuery Code:

$ (document). Ready (function () {
 $ ("#typeduty"). Change (function () {
  var type = $ ("#typeduty"). Val ();
  $.post ("setduty_do.php",
   {
    ' cid ': type,
   },function (data,status) {
    NewData = data.replace (/\r\n/g, ' ');
     $ ("#names"). HTML (newdata);
  });
 });


PHP Code:

if ($type) {
 switch ($type) {case
  "1":
   $arrs = Getchieflist ();
  break;
  Case "2":
   $arrs = Getdirectlist ();
  break;
  Case "3":
   $arrs = Getattendantlist ();
  break;
  Default: Break
  ;
 }
 $arr _names = "";
 foreach ($arrs as $arr) {
  if (strlen ($arr [1]) >2) {
   $arr _names.= $arr [1]. " \ n ';
  }
 }
 echo $arr _names;
}

The value returned in the PHP code is directly with a newline "\ n", and the line-break symbol for IE and FF in textarea is "\ n", and the processing return value of JQuery is noted, using

Copy Code code as follows:
NewData = Data.replace (/\r\n/g, ");

Handles the returned results, which are used to remove the first line break symbol, and the value returned by Ajax automatically comes with a newline. This is the AJAX itself.

I hope this article will help you with Ajax programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.