如何发送"&安培;"在AJAX请求参数 [英] How to send "&" as request parameter in ajax

查看:148
本文介绍了如何发送"&安培;"在AJAX请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过表单数据使用jQuery code那么php code中的数据保存到MySQL数据库到PHP。如果标题是如何保存和放大器; MySQL数据库使用PHP,那么标题将存储在数据库中如何拯救。我怎样才能解决这个问题?

下面是我的JS code

  VAR dataString ='标题='+标题+'和; URL ='+ URL +'和;描述='+说明书+'和;出版='+出版+'和; CATID ='+ CATID;

  //警报(dataString);返回false;

  $阿贾克斯({
    键入:POST,
    网址:process.php
    数据:dataString,
    成功:函数(){
      $(按钮)隐藏();
      $('#信息)HTML(< D​​IV ID ='消息'>< / DIV>中);
      $('#消息)HTML(< H2>网站链接形式提交了<!/ H2>中)
      。隐藏()
      .fadeIn(1500,函数(){
        $('#消息');
      });
    }
  });
 

和PHP code

 < PHP
$ CON =的mysql_connect(localhost的,DB,通行证);
如果(!$ CON)
  {
  死亡(无法连接:mysql_error());
  }

mysql_select_db(dalanrep_dalanreportcom,$ CON);

    $标题= mb_convert_encoding(修剪($ _ POST ['标题']),HTML的实体,UTF-8);
    $ URL = mb_convert_encoding(修剪($ _ POST ['URL']),HTML的实体,UTF-8);
    $描述= mb_convert_encoding(修剪($ _ POST ['介绍']),HTML的实体,UTF-8);
    $公布= mb_convert_encoding(修剪($ _ POST ['出版']),HTML的实体,UTF-8);
    $ CATID = mb_convert_encoding(修剪($ _ POST ['CATID']),HTML的实体,UTF-8);

    $标题=函数mysql_escape_string($标题);
    $ URL =函数mysql_escape_string($网址);
    $描述=函数mysql_escape_string($说明);
$ SQL =INSERT INTO表(标题,URL,描述,CATID)
VALUES('$称号,$网址,$说明,$ CATID');



如果(!的mysql_query($的SQL,$ CON))
  {
  死亡(错误:mysql_error());
  }
回声1纪录补充;
 

解决方案

&安培; 是URL的一个特殊字符。它是对多个参数的分隔符。你需要的或者的URL-CN code自己通过的 连接codeURIComponent()

  VAR dataString ='标题'+ EN codeURIComponent(职称)+'&放大器; URL ='+ EN codeURIComponent(URL)...;
 

的提供的参数作为JS对象 {} 让jQuery将处理它本身。

  VAR数据= {
    头衔:标题,
    URL:URL,
    ...
};
 

I pass form data to php using jquery code then the php code save the data to the mysql database. If the title is "how to save & to mysql database using php", then the title will stored in database as "how to save". How can i fix this?

Here is my JS code

    var dataString = 'title='+ title + '&url=' + url + '&description=' + description + '&published=' + published+ '&catid=' + catid;

  //alert (dataString);return false;

  $.ajax({
    type: "POST",
    url: "process.php",
    data: dataString,
    success: function() {
      $(".button").hide();
      $('#messages').html("<div id='message'></div>");
      $('#message').html("<h2>weblink Form Submitted!</h2>")
      .hide()
      .fadeIn(1500, function() {
        $('#message');
      });
    }
  });

and php code

    <?php
$con = mysql_connect("localhost","db","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dalanrep_dalanreportcom", $con);

    $title        = mb_convert_encoding(trim($_POST['title']),'HTML-ENTITIES', 'UTF-8');
    $url          = mb_convert_encoding(trim($_POST['url']),'HTML-ENTITIES', 'UTF-8');
    $description  = mb_convert_encoding(trim($_POST['description']),'HTML-ENTITIES', 'UTF-8');
    $published    = mb_convert_encoding(trim($_POST['published']),'HTML-ENTITIES', 'UTF-8');
    $catid        = mb_convert_encoding(trim($_POST['catid']),'HTML-ENTITIES', 'UTF-8');

    $title =mysql_escape_string($title );
    $url = mysql_escape_string($url );
    $description = mysql_escape_string($description );
$sql="INSERT INTO table (title, url, description,catid)
VALUES ('$title','$url','$description','$catid')";



if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

解决方案

The & is a special character in URL's. It is the separator character for multiple parameters. You need to either URL-encode the parameter value yourself by encodeURIComponent()

var dataString = 'title' + encodeURIComponent(title) + '&url=' + encodeURIComponent(url) ... ;

or to supply the parameters as a JS object {} so that jQuery will handle it itself.

var data = {
    "title": title, 
    "url": url,
    ...
};

这篇关于如何发送&QUOT;&安培;&QUOT;在AJAX请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆