A HREF link Fires my Javascript before click

  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

I'm messing with some javascript code that creates a popup that takes HTML code as a parameter and displays using innerhtml. It disables and covers the background. Before I try to explain all my code. My problem is simply I have a php script that displays a table cell with a link to open the popup.
PHP Code: [ Select ]
 
$html = "<form name='form1' method='post' action='proc.php'><table><tr><td>Email:</td><td><input name='email' type='text' id='email' value=''></td></tr><tr><td>Password:</td><td><input name='mypassword' type='password' id='mypassword' value=''></td></tr><tr><td></td><td><input name='pkey' type='hidden' id='id' value=''><input type='submit' name='Submit' value='Submit'></td></tr></table></form><br><a href='javascript&#058;afilter(7,31,2010)' onMouseOver=' return changeImage(4)' onMouseOut= 'changeImageBack(4)'>Close2Submit</a>";
 
 
$timelist .= "<td width='$w' height='50px' valign='top' onMouseOver=\"this.className='mouseover'\" onMouseOut=\"this.className='mouseout'\"><a href='javascript&#058;Popup($html,'OK',null,'Cancel',null);'>Link</a></td>";
 
  1.  
  2. $html = "<form name='form1' method='post' action='proc.php'><table><tr><td>Email:</td><td><input name='email' type='text' id='email' value=''></td></tr><tr><td>Password:</td><td><input name='mypassword' type='password' id='mypassword' value=''></td></tr><tr><td></td><td><input name='pkey' type='hidden' id='id' value=''><input type='submit' name='Submit' value='Submit'></td></tr></table></form><br><a href='javascript&#058;afilter(7,31,2010)' onMouseOver=' return changeImage(4)' onMouseOut= 'changeImageBack(4)'>Close2Submit</a>";
  3.  
  4.  
  5. $timelist .= "<td width='$w' height='50px' valign='top' onMouseOver=\"this.className='mouseover'\" onMouseOut=\"this.className='mouseout'\"><a href='javascript&#058;Popup($html,'OK',null,'Cancel',null);'>Link</a></td>";
  6.  



It seems that when the table cell is displayed it shows the $html content automatically inside the cell instead of ONLY the Link button which when clicked should open a popup (which really just creates a div id and displays innerhtml to it). In fact, the Link button doesn't even work. On display it prints Close2Submit"','OK',null,'Cancel',null);'>Link and if I hover over the Submit button is says "javascript:Popup(" (no quotes). The $html is what I'm testing with an will not be the actual table I want to display on the popup but I figured if that $html content worked then any should.

I've tried using onclick for the A element with # and void etc. Why is the javascript firing without a user click? Or it may not be firing just displaying, but I've looked through all my quotes and apostrophes but I can't sort it out.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

Slowly getting it worked out.

The problem is bad code not that it's not firing. The $html variable has problems. I basically got things going by escaping the html:
Code: [ Select ]

$html = "<form><table> etc etc </table></form>";

$html = mysql_real_escape_string($html);

$timelist = ".....<a href='javascript:Popup(\"$html\",\"OK\",null,\"CANCEL\",null);'>Link</a>";
  1. $html = "<form><table> etc etc </table></form>";
  2. $html = mysql_real_escape_string($html);
  3. $timelist = ".....<a href='javascript:Popup(\"$html\",\"OK\",null,\"CANCEL\",null);'>Link</a>";



Maybe there's a cleaner way.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

I'm back to revisiting this cause I noticed I can't display sentences in the $html variable that contain single quotes (apostrophes). How are html elements (div, table, tr, td, input) suppose to look in the Javascript?
Code: [ Select ]
$rs = 10;
$custname = "Donnie Kobold";

$html = '<div id="eventztable"><div id="closeevent"><a href="javascript:this.hide()" /><img src="img/closeButton_normal.png"></a></div><form name="addevent2"><table width="750" border="0" align="center"><tr><td>Name </td><td align="left"><input name="custname" type="text" id="custname" size="30" value="'.$custname.'" /></td></tr><tr><td valign="top">Comments</td><td valign="top"><p><textarea name="comments" cols="50" rows="8" id="comments">'.$commentz.'</textarea></p></td></tr></table></form><div id="addresponse"></div></div>';

$ecell.= '<td rowspan = "'.$rs.'" style="cursor:pointer;" valign="top" onMouseOver=this.className='.$dayover.'" onMouseOut="this.className='.$dayout.'" onClick="javascript:Popup(\"'.$html.'\","null","null","null","null");">'.$eventinfo.'</td>';
  1. $rs = 10;
  2. $custname = "Donnie Kobold";
  3. $html = '<div id="eventztable"><div id="closeevent"><a href="javascript:this.hide()" /><img src="img/closeButton_normal.png"></a></div><form name="addevent2"><table width="750" border="0" align="center"><tr><td>Name </td><td align="left"><input name="custname" type="text" id="custname" size="30" value="'.$custname.'" /></td></tr><tr><td valign="top">Comments</td><td valign="top"><p><textarea name="comments" cols="50" rows="8" id="comments">'.$commentz.'</textarea></p></td></tr></table></form><div id="addresponse"></div></div>';
  4. $ecell.= '<td rowspan = "'.$rs.'" style="cursor:pointer;" valign="top" onMouseOver=this.className='.$dayover.'" onMouseOut="this.className='.$dayout.'" onClick="javascript:Popup(\"'.$html.'\","null","null","null","null");">'.$eventinfo.'</td>';


This is what I'm messing with. I can't seem to get the quotes working right.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

to show a single quote in a string that is enclosed by single quotes you can escape it with the slash "\" ... Something like this:

PHP Code: [ Select ]
$test = 'Hello my my name is \'R_T\'.';


Which will echo: Hello my name is 'R_T'.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

Actually what I'm doing is putting $test into a javascript function that popups a modal of the $test contents which in my case is HTML. The javascript: Popup() creates a modal and if I use 'Hello my name is 'R_T'.' The function breaks cause I then have

Code: [ Select ]

javascript: Popuup('Hello my name is \'R_T\'', null, null, null, null);
  1. javascript: Popuup('Hello my name is \'R_T\'', null, null, null, null);


Is it possible to pass single quotes to a javascript function?
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

Okay how about this:

JAVASCRIPT Code: [ Select ]
javascript&#058; Popuup("Hello my name is 'R_T'", null, null, null, null);

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 90 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.