GET、PHP 中的 &

2023-10-12php开发问题
1

本文介绍了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 页面,然后该页面将条目提交到数据库.Ajax 成功完成后,用户被发送到上传页面,并带有一个 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-encode 任何不完全是字母数字的内容,当您将它们作为 URL 的一部分传递时.

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 码).

In URL-encoding, & is replaced with %26 (because 0x26 = 38 = the ASCII code of &).

要在 Javascript 中执行此操作,您可以使用函数 encodeURIComponent:

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);
    }
});

请注意,escape 的缺点是 + 未编码,将在服务器端解码为空格,因此应避免使用 (来源).

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 级别执行此服务器端操作,则需要使用函数 urlencode.

If you wish to do this serverside at the PHP level, you'll need to use the function urlencode.

这篇关于GET、PHP 中的 &的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17