douluogu8713 2018-04-26 15:06
浏览 49
已采纳

&符号后的子字符串丢失[重复]

This question already has an answer here:

$mystring = "DTI ORIENTATION: CONSUMER PROTECTION & LEMON LAW";

After doing this,

$mystring = htmlspecialchars("DTI ORIENTATION: CONSUMER PROTECTION & LEMON LAW");

I'm now getting an echo of just "DTI ORIENTATION: CONSUMER PROTECTION". Even if I removed the htmlspecialchars after, the string is different now.

The words after the & sign are missing along with it. What just happened? I just want to make the & sign to be included in my string to be used in mysqli_query. Please enlighten me on this one. Thank you.

code from file1.php:

$(".t_title").click(function(){
            var title = $(this).data("title");
            var training_date = $(this).data("tdate");

            location.href = "viewTrainingAttendees.php?title=" + title + "&tdate="+ training_date; 
        });

code from viewTrainingAttendees.php:

<?php 
                        $ttitle = $_GET['title'];
                        echo $ttitle;

                     ?>
</div>
  • 写回答

1条回答 默认 最新

  • douba8819 2018-04-26 15:20
    关注

    & has special meaning in a query string, it marks the start of the next key=value pair.

    If you want to represent it as data, you have to percent encode it.

    Use encodeURIComponent() on any plain text string you are inserting into a URL.

    Better yet, use the URL API to construct query strings instead of mashing strings together. (You'll need a polyfill for old browsers).

    var url = new URL(location.href);
    url.pathname = "viewTrainingAttendees.php";
    url.searchParams = new URLSearchParams();
    url.searchParams.append("title", "DTI ORIENTATION: CONSUMER PROTECTION & LEMON LAW");
    url.searchParams.append("tdate", "example example");
    console.log(url.href);

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型