dqy1265 2010-11-04 14:31
浏览 64
已采纳

如何在ajax中发送“&”作为请求参数

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";
  • 写回答

2条回答 默认 最新

  • duanhanglekr37902 2010-11-04 14:37
    关注

    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,
        ...
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?