EHEM EHEM - 25 days ago 10
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 = 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>";
?>

Answer

URL's must be ... URL encoded.

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

example:

viewpurchase.php?id=2 334

becomes

viewpurchase.php%3Fid%3D2%20334

Try the php function urlencode in your code:

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

urlencode($row['ponumber']).

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