通过弹出窗口在url中转义空格和特殊字符

时间:2016-11-08 05:39:08

标签: javascript php

我的弹出窗口工作正常,直到变量有一些空格或特殊字符,如#等。 例如,如果我的引用变量类似于'12345',则会出现弹出窗口,但是当涉及空格和特殊字符时,它将不起作用。 我正在使用php结合javascript弹出窗口查看每个参考项目。

<script type="text/javascript">
function PopupCenter(pageURL, title,w,h) 
{
var left = (screen.width/2)-(w/2);
                                var top = (screen.height/2)-(h/2);
                                var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no,  copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
                            } 
                            </script>


     <?php
    $sql = "SELECT * from purchase_tb ORDER BY ponumber  ASC   ";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
    echo " 
    <div class='table-responsive'>
    <table class='table table-bordered table-hover table-condensed '>
     <tr class='success'><th>PO.Number</th><th> Model </th><th>Date Created</th> 
    </tr>";
     $current_cat = null;
    while($row = $result->fetch_assoc()) {
     if ($row["ponumber"] != $current_cat) {
    $current_cat = $row["ponumber"];
    $current_cat2 = $row["datecreated"];
    echo "
    <tr><td>";  
    echo"   
    {$current_cat}</td> 
    <td><a href='#' onclick=PopupCenter('viewpurchase.php?Rid=".$row['ponumber']."','myPop1',1000,800)>View</a></td><td>{$current_cat2}</td>";
?>

1 个答案:

答案 0 :(得分:1)

网址必须是...网址编码。

像空格这样的字符必须替换为有效字符才能形成有效的网址。

示例:

viewpurchase.php?id = 2 334

变为

viewpurchase.php%3Fid%3D2%20334

在代码中尝试php函数urlencode

<a href='#' onclick=PopupCenter('viewpurchase.php?Rid=". 

urlencode($row['ponumber']).

"','myPop1',1000,800)>View</a></td><td>{$current_cat2}</td>";