转义和字符串
Escape & in a string?
人们对我之前问题的建议似乎没有帮助。所以,我想在这里以完整的代码再次发布它:mysql_depricated.php:
<script>
function fun()
{
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
var name = document.getElementById("name").value;
var url = "mysql_depricated2.php";
var param = "name=" + name;
ajaxRequest.open("POST", url, true);
//Send the proper header information along with the request
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", param.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(param);
}
</script>
<input type="text" id="name">
<input type="button" value="ajax&" onclick="fun()">
mysql_depricated2:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="example"; // Database name
// Connect to server and select databse.
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$content = $_POST['name'];
$content=mysql_real_escape_string($content);
$sql = "insert into e values('$content')";
mysql_query($sql);
?>
如果我尝试插入包含 & 符号的字符串,它不起作用。请问有什么解决方案吗?
替换以下行:
ajaxRequest.send(param);
对于这个:
ajaxRequest.send( encodeURIComponent(param) );
因此,您将&
特殊字符编码为其 url 编码形式,即 %26
.
相关文章:
- 使用Jinja2的JavaScript转义字符串
- Javascript-正则表达式,用于在未转义字符上拆分字符串,例如|,但忽略|
- 如何转义字符串中的html实体
- “转义字符串”在Regex中是什么意思?(Javascript)
- 正在对java中的javascript字符串进行转义
- javascript中不带转义字符串的${variable}用法
- 如何存储包含反斜杠的字符串(无需手动双转义)
- 如何转义php中的新行字符以输出为javascript字符串值
- jQuery 字符串转义 onclick='deleteRow(item“+count+”)' 如何引用项目和计数
- 聊天应用程序字符串转义
- 将 FileReader.readAsBinaryString 转换为 Unicode 转义字符串
- 控制转义字符串
- 等价于str.decode('字符串转义')
- 从变量中的字符串转义 JavaScript 中的双引号
- Angular.js 中的部分 HTML 字符串转义
- 字符串转义,从文本文件中读取
- 将字符串转义为单引号字符串
- Javascript regex验证密码字符串(转义标点符号)
- JavaScript中的字符串转义
- 跨语言字符串转义