Jump to content

echo speechmark problem


MDanz

Recommended Posts

the code works like this outside of php

<img src="stackm.jpg" id="Image2" alt="test!" onmouseover="ChgText('<?php echo $number; ?>'); MM_swapImage('Image2','','stackmrollover1.jpg',1);" onmouseout="MM_swapImgRestore()"/>

 

in php when i have to echo

 

echo " <img src='stackm.jpg' id='Image2' alt='test!' onmouseover='ChgText($number); MM_swapImage('Image2','','stackmrollover1.jpg',1);' onmouseout='MM_swapImgRestore()'/>
";

 

it doesn't work.. i know its to do with speech marks, because the top one works.. any help?

Link to comment
https://forums.phpfreaks.com/topic/174143-echo-speechmark-problem/
Share on other sites

echo " <img src='stackm.jpg' id='Image2' alt='test!' onmouseover='ChgText({$number}); MM_swapImage('Image2','','stackmrollover1.jpg',1);' onmouseout='MM_swapImgRestore()'/>
";

 

if this one not work, try this:

 

echo " <img src='stackm.jpg' id='Image2' alt='test!' onmouseover='ChgText(".$number."); MM_swapImage('Image2','','stackmrollover1.jpg',1);' onmouseout='MM_swapImgRestore()'/>
";

it  has nothing to do with ChgText($number) its the speech marks. how can i change the speech marks of the bottom code to get it working?

the difference with echo, it seems everything inside it has to be an apostrophe..

 

<img src="stackm.jpg" id="Image2" alt="test!" onmouseover="ChgText($number); MM_swapImage('Image2','','stackmrollover1.jpg',1);" onmouseout="MM_swapImgRestore()"/>

^^

this works

 

 

this doesn't

vv

echo " <img src='stackm.jpg' id='Image2' alt='test!' onmouseover='ChgText($number); MM_swapImage('Image2','','stackmrollover1.jpg',1);' onmouseout='MM_swapImgRestore()'/>
";

 

 

 

i tried this and it still doesnt work..

 

echo "<body onload=\"MM_preloadImages('stackmrollover1.jpg')\">
		 <img src=\" stackm.jpg\"  id=\" Image2\"  alt=\" test!\"  onmouseover=\" ChgText($number); MM_swapImage('Image2','','stackmrollover1.jpg',1);\"  onmouseout=\" MM_swapImgRestore()\" />
";

 

:wtf:

echo '<img src="stackm.jpg" id="Image2" alt="test!" onmouseover="ChgText($number); MM_swapImage(\'Image2\',\'\',\'stackmrollover1.jpg\',1);" onmouseout="MM_swapImgRestore()" />';

 

Basically you're having to do multiple escaping, because you are dealing with multiple parsers.

 

First you have PHP parsing the code so anything in between [echo " "] that contains a " has to be escaped like \" - hence we have echo "<img src=\"...jpg\">"; - however if we use single quotes we only have to escape single quotes rather than double quote and because we need double quotes more in the HTML it's easier to just start with single quotes.

 

The reason the code doesn't work with all single quotes within the double quotes is because in the HTML you are beginning a javascript block with single quotes and using unescaped single quotes within it.

echo '<img src="stackm.jpg" id="Image2" alt="test!" onmouseover="ChgText($number); MM_swapImage(\'Image2\',\'\',\'stackmrollover1.jpg\',1);" onmouseout="MM_swapImgRestore()" />';

 

Basically you're having to do multiple escaping, because you are dealing with multiple parsers.

 

First you have PHP parsing the code so anything in between [echo " "] that contains a " has to be escaped like \" - hence we have echo "<img src=\"...jpg\">"; - however if we use single quotes we only have to escape single quotes rather than double quote and because we need double quotes more in the HTML it's easier to just start with single quotes.

 

The reason the code doesn't work with all single quotes within the double quotes is because in the HTML you are beginning a javascript block with single quotes and using unescaped single quotes within it.

 

it doesn't work?!? i am confused.. it should work..

 

i've tried it this way..

 

echo "<body onload=\"MM_preloadImages('stackmrollover1.jpg')\">
          <img src=\" stackm.jpg\"  id=\" Image2\"  alt=\" test!\"  onmouseover=\" ChgText($number); MM_swapImage('Image2','','stackmrollover1.jpg',1);\"  onmouseout=\" MM_swapImgRestore()\" />
";

 

and your way and it doesnt work ... it only works in html?

heres the working code not in  a while loop on a seperate page for testing

.. heres how it look running http://www.u-stack.com/test.php

<?php $number= "1";
$text1 = "Hello";

?>
<script type="text/JavaScript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<body onload="MM_preloadImages('stackmrollover1.jpg')">

<div style='display:none;' id='<?php echo $number; ?>'><?php echo $text1; ?></div>

<script type='text/javascript'>
function ChgText(number){

   var newtext = document.getElementById(number).innerHTML
   document.getElementById('reply').value += newtext;
}
</script>
<img src="stackm.jpg" id="Image2" alt="test!" onmouseover="ChgText('<?php echo $number; ?>'); MM_swapImage('Image2','','stackmrollover1.jpg',1);" onmouseout="MM_swapImgRestore()"/>

<textarea name="reply" cols="50" rows="10" wrap="hard" id="reply"></textarea>

 

 

now when i copy it to the while loop in php and echo it, i can't seem to get the two onmouseover events to work.  its getting two onmouseover events to work in the echo""; which is the problem i can get one to work like this but not two.

 

 

echo "<img src='stackm.jpg' id='Image2' alt='test!' onmouseover='ChgText($number)' onmouseover='MM_swapImage('Image2','','stackmrollover1.jpg',1)' onmouseout='MM_swapImgRestore()'/>
";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.