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


Escape spaces and special characters in url thru a pop window

我的弹出窗口工作良好,直到变量有一些空格或特殊字符,如#等。例如,弹出窗口将出现,如果我的参考变量是'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>";
?>

URL必须是…URL编码。

像空格这样的字符必须用有效的字符替换,以形成有效的URL。

的例子:

viewpurchase.php吗?id = 334

viewpurchase.php fid % 3 d2 % % 20334

在你的代码中试试php函数urlencode:

<a href='#' onclick=PopupCenter('viewpurchase.php?Rid=". 
urlencode($row['ponumber']).
"','myPop1',1000,800)>View</a></td><td>{$current_cat2}</td>";