EHEM EHEM - 5 months ago 42
Javascript Question

Escape spaces and special characters in url thru a pop window

My popup window works fine until if the variables have some spaces or special characters such as # etc.
For example popup window will appear if my reference variable is like '12345' but when it comes to spaces and special characters, it doesn't work.
I'm using php combine with javascript pop window for viewing of item each reference.

<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 = (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);

$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>
$current_cat = null;
while($row = $result->fetch_assoc()) {
if ($row["ponumber"] != $current_cat) {
$current_cat = $row["ponumber"];
$current_cat2 = $row["datecreated"];
echo "
<td><a href='#' onclick=PopupCenter('viewpurchase.php?Rid=".$row['ponumber']."','myPop1',1000,800)>View</a></td><td>{$current_cat2}</td>";


URL's must be ... URL encoded.

Characters like spaces must be replaced with valid characters to form a valid URL.


viewpurchase.php?id=2 334



Try the php function urlencode in your code:

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