在GET中的&符号,PHP
我有一个简单的表单,生成一个新的照片库,将标题和描述发送到MySQL,并将用户重定向到他们可以上传照片的页面。
一切都很好,直到&符号进入等式。 信息从jQuery模式对话框发送到PHP页面,然后将该条目提交到数据库。 Ajax成功完成后,用户将被发送到带有GET URL的上传页面,告诉该页面要上传的相册 –
$.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; } });
如果标题有&符号,则上传页面上的标题字段无法正确显示。 有没有办法逃脱Gers的&符号?
谢谢
一般情况下,当您将其作为url的一部分传递时,您需要对任何非完全字母数字的内容进行url编码 。
在URL编码中, &
替换为%26
(因为0x26 = 38 = &
的ASCII码)。
要在Javascript中执行此操作,您可以使用函数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); } });
请注意, escape
的缺点是+
未编码,并且将在服务器端解码为空格,因此应该避免( 源 )。
如果您希望在PHP级别执行此服务器端,则需要使用函数urlencode
。
window.location.href = 'display_album.php?album=' + encodeURIComponent(title);
javascript escape
函数不会对这些字符进行编码:* @ – _ +。 /。 因此,如果您有“this + that”这样的标题,加号将被解释为空格,PHP将接收变量“this that”。
使用encodeURIComponent
还将编码以下字符:,/? :@&= + $#