Php “如何发送”&&引用;在ajax中作为请求参数
我使用jquery代码将表单数据传递给php,然后php代码将数据保存到mysql数据库。如果标题是Php “如何发送”&&引用;在ajax中作为请求参数,php,jquery,ajax,Php,Jquery,Ajax,我使用jquery代码将表单数据传递给php,然后php代码将数据保存到mysql数据库。如果标题是“如何使用php保存和到mysql数据库”,则标题将作为“如何保存”存储在数据库中。我怎样才能解决这个问题 这是我的JS代码 var dataString = 'title='+ title + '&url=' + url + '&description=' + description + '&published=' + published+ '&catid
“如何使用php保存和到mysql数据库”
,则标题将作为“如何保存”
存储在数据库中。我怎样才能解决这个问题
这是我的JS代码
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');
});
}
});
var-dataString='title='+title+'&url='+url+'&description='+description+'&published='+published+'&catid='+catid;
//警报(数据串);返回false;
$.ajax({
类型:“POST”,
url:“process.php”,
数据:dataString,
成功:函数(){
$(“.button”).hide();
$('#messages').html(“”);
$('#message').html(“已提交Web链接表单!”)
.hide()
.fadeIn(1500,函数(){
$(“#消息”);
});
}
});
和php代码
<?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";
这个&
是URL中的一个特殊字符。它是多个参数的分隔符。您需要自己对参数值进行URL编码
或者将参数作为JS对象{}
提供,以便jQuery自己处理它
var data = {
"title": title,
"url": url,
...
};
&
是URL中的一个特殊字符。它是多个参数的分隔符。您需要自己对参数值进行URL编码
或者将参数作为JS对象{}
提供,以便jQuery自己处理它
var data = {
"title": title,
"url": url,
...
};
如果符号是数据而不是段分隔符,则需要对其进行编码
<?php
$var = "some title with an & in it";
$title = htmlentities($var);
...
?>
如果是数据而不是段分隔符,则需要对符号和进行编码
<?php
$var = "some title with an & in it";
$title = htmlentities($var);
...
?>