与号进去,PHP [英] Ampersand in GET, PHP
问题描述
我有一个简单的表单,生成一个新的照片库,发送标题和到MySQL的描述,将用户重定向到一个页面,在这里他们可以上传照片。
I have a simple form that generates a new photo gallery, sending the title and a description to MySQL and redirecting the user to a page where they can upload photos.
一切正常,直到符号输入公式。该信息是从一个jQuery模态对话框到PHP页面,然后提交条目发送到数据库。阿贾克斯成功完成后,用户被发送到上传页面用GET URL来告诉它上传到相册什么的页面 -
Everything worked fine until the ampersand entered the equation. The information is sent from a jQuery modal dialog to a PHP page which then submits the entry to the database. After Ajax completes successfully, the user is sent to the upload page with a GET URL to tell the page what album it is uploading to --
$.ajax ({
type: "POST",
url: "../../includes/forms/add_gallery.php",
data: $("#addGallery form").serialize(),
success: function() {
$("#addGallery").dialog('close');
window.location.href = 'display_album.php?album=' + title;
}
});
如果标题有一个符号,在上传页面上的标题字段不正确显示。有没有一种方式来逃避符号的GET?
If the title has an ampersand, the Title field on the upload page does not display properly. Is there a way to escape ampersand for GET?
感谢
推荐答案
在一般你要 URL-CN $ C $ Ç任何事情,当你把它们作为你的网址的一部分,是不是完全字母。
In general you'll want to URL-encode anything that isn't completely alphanumerical when you pass them as parts of your URLs.
在URL编码,&安培;
被替换%26
(因为0X26 = 38 =的ASCII $ C $了C &安培;
)
In URL-encoding, &
is replaced with %26
(because 0x26 = 38 = the ASCII code of &
).
要做到这一点在Javascript中,你可以使用函数<一href="http://www.w3schools.com/jsref/jsref_en$c$cURIComponent.asp"><$c$c>en$c$cURIComponent$c$c>:
To do this in Javascript, you can use the function encodeURIComponent
:
$.ajax ({
type: "POST",
url: "../../includes/forms/add_gallery.php",
data: $("#addGallery form").serialize(),
success: function() {
$("#addGallery").dialog('close');
window.location.href = 'display_album.php?album=' + encodeURIComponent(title);
}
});
请注意, 逃跑
具有<缺点code> + 不是连接codeD,并且将去codeD服务器端作为一个空间,因此应避免(的来源)。
Note that escape
has the disadvantage that +
is not encoded, and will be decoded serverside as a space, and thus should be avoided (source).
如果你想做到这一点的服务器端在PHP层,则需要使用该功能的 urlen code
。
If you wish to do this serverside at the PHP level, you'll need to use the function urlencode
.
这篇关于与号进去,PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!