Hi I was just wondering if anyone knew how to deal with the window.open js function when you want to echo it.

Can somebody tell me how to fix the syntax error the below.

onClick="window.open('viewhotel.php?id='$row['id']','mywindow','width=400,height=200')">'

Any suggestions would be great!

Try this:

<?php
// php code here
?>
<!-- HTML code here -->
<input type='button' name='myButton' id='myButton' onClick="window.open('viewhotel.php?id=\'<?php echo $row['id']; ?>\',\'mywindow\',\'width=400,height=200\')'" />

Is that what you meant?

Thanks for getting back to me Darkagn,

Yea thats exactly what I'm looking to do, however I'm still getting an error on it.

This is the full line which is concatonated on either side with a tonne of other stuff and echoed.

.'<input type="image" src="ViewHotel.PNG" name="image" width="90" height="24" onClick="window.open('viewhotel.php?id=\'<?php echo $row['id']; ?>\',\'mywindow\',\'width=400,height=200\')'"/>'.

Just wondering if you can spot the mistake in it?

Thanks for your time.

Hmm, I can't see what's wrong there, can you post the whole line of code please? Also, what is the error message you are getting when you run the code?

I dont think its the code around it Darkagn as it is only when I include the onclick window.open part that I'm finding a problem.

When the page is called im just getting a white screen which lead me to think its a syntax error.

I'm using notepad++ and it seems to be highlighting the

viewhotel.php?id=

and

$row as the problem areas

Try it like this perhaps?

echo "<input type=\"image\" src=\"ViewHotel.PNG\" name=\"image\" width=\"90\" height=\"24\" onClick=\"window.open('viewhotel.php?id={$row['id']}, 'mywindow', 'width=400, height=200')'\"/>";

I think the problem is that you are already in php and echoing the string, so you can't nest a <?php ?> tag inside the html. There is also a problem with the quote marks, but I think the above code should take care of that.

EDIT: Also, in order to evaluate an array element inside double quotes, it needs to be surrounded by braces, thus the {$row}.

Thanks Darkagn

I managed to get the page loading by tidying it up a little.

while($row = mysql_fetch_array($result))
 {
	echo '<div class="rbroundbox">
	<div class="rbtop"><div></div></div>
	<div class="rbcontent"><div class="mydiv">&nbsp;'.$row['hotelname'].',&nbsp;'.$row['address1'].',&nbsp;'.$row['address2'].',&nbsp;'. $row['county'].'</div>
	<p>&nbsp;&nbsp;</p>
	<div class="urdiv"><br/>&nbsp; &nbsp; &nbsp; &nbsp;<img src=http://www..com/Website/images/'.$row['logo'] .' alt="logo" rel="lightbox[mando]" border="1" height="100" width="100">&nbsp;</div>
	<div class="hisdiv"><br/><font size = "6"><strong><b>Hotel Bids &#8364 </b></strong></font><font size = "6"><strong>'.$row['hotelprice'].'</strong></font size><br />Includes: &nbsp;'.$row['addons'].'<br />Reference Number: ' . $row['bookingref'].'<br/><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
		<input type="image" src="BookThisHotel.PNG" name="image" width="121" height="24" onclick="window.location=\'bookview.php?em='.$row['email'].'&id='.$row['bookingref'].'&te='.$row['telephone'].'&nh='.$row['hotelname'].'\'">&nbsp;
		<input type="image" src="ViewHotel.PNG" name="image" width="90" height="24" onClick="window.open(\'viewhotel.php?id='.$row['id'].', \'mywindow\', \'width=400, height=200\')"/></div></div><!-- /rbcontent -->  
	<div class="rbbot"><div></div></div>
</div><!-- /rbroundbox -->';
				
 }
		
	    ?>

However, the onClick window.open still doesnt seem to be working.

When I press on it no new window is opened.

Could anyone suggest why this might be the case?

Thanks

Does a window open and the problem is that it is just blank? Or does no window open at all? Truly, I can't see what's wrong with your code, the syntax looks correct, maybe try echoing $row beforehand to make sure that you are using the correct value?

try it now

<input type="image" src="ViewHotel.PNG" name="image" width="90" height="24" onClick="window.open(\'viewhotel.php?id='.$row['id'].'\'', \'mywindow\', \'width=400, height=200\')"/>
Member Avatar for diafol

What does the browser's 'view source' give you? Is the onclick complete?

Try to use normal html with embedded php echoes:

<?php
...(your code)...
?>

<input type="image" src="ViewHotel.PNG" name="image" width="90" height="24" onclick="window.open('viewhotel.php?id=<?php echo $row['id']; ?>', 'mywindow','width=400,height=200');" /> 

<?php
...(your code)...
?>

Thanks Dasatti

onClick="window.open(\'viewhotel.php?id='.$row['id'].'\', \'mywindow\', \'width=400, height=200\')

This worked fine.

You are welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.