Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Javascript image replacement


ghost's Avatar
0 0

i first want to ask, how php processing a script? as i written in the topic, i need a javascript, but i want it in a php file. so i need :

,...
?>
javascript.... //javascript
<?php //second php
....
?>```
so can it be done in this way?
also to the main problem, how can i do, i know, i think it can be only done in javascript, an image replacement? there are three radio buttons, and depends on selection, the proper image will be shown.
so how can i do this? thanks.

spyware's Avatar
Banned
0 0

Just like you said.

All the weblangs (css, JS, html whatever) can be used in-line as long as you use php tags around your PHP code.

Why even bother asking this? By trying it you have your answer much faster.


ghost's Avatar
0 0

yes, i have tryed this. my problem was that i cannot use the javascript inside the <?php ?>, because in php is used this character: " so when i runned it, it gived me an error. i must replace that character by \" or by single quote, so i cant include the js in php statements.


spyware's Avatar
Banned
0 0

Erh?

You can also use single quotes ('):

<?php echo '<script>alert("BOO")</script>'; ?>

Problem solved?


ghost's Avatar
0 0

thanks, i didnt know this so this problem is solved anyway, can you recommend any good article to that js and radio button? i google for this but i dont get that what i need. there was only some equivalent for str_replace() in php, but not for image replace.


spyware's Avatar
Banned
0 0

Replace the string of (image) code with your string of code, done.


ghost's Avatar
0 0

dancuc wrote: anyway, can you recommend any good article to that js and radio button? i google for this but i dont get that what i need. there was only some equivalent for str_replace() in php, but not for image replace.

Think about how an image actually works. You use the IMG tag and specify the image source with the SRC attribute. So, using Javascript and the DOM, just change the SRC attribute to whatever you want for each radio button; use the onclick() event.


ghost's Avatar
0 0

thanks, now i've got it ;)


spyware's Avatar
Banned
0 0

Can you post your solution, this might help/inspire others.

Thanks.


ghost's Avatar
0 0

i will, but i'm not on my computer right now, so when i'll be, i will post it.


ghost's Avatar
0 0

ok, i'm posting:

&lt;img id=&quot;image1&quot; src=&quot;img1.jpg&quot; /&gt;
&lt;img id=&quot;image2&quot; src=&quot;img2.jpg&quot; style=&quot;display:none&quot; /&gt;
&lt;br /&gt;
&lt;input type=&quot;radio&quot; name=&quot;radiobutton&quot; onclick=&quot;this.checked ? Change(&#39;image1&#39;) : Change(&#39;image2&#39;)&quot; checked=&quot;true&quot; /&gt;
&lt;input type=&quot;radio&quot; name=&quot;radiobutton&quot; onclick=&quot;this.checked ? Change(&#39;image2&#39;) : Change(&#39;image1&#39;)&quot; /&gt;
&lt;/form&gt;
&lt;script language=&quot;JavaScript&quot;&gt;
/*&lt;![CDATA[*/

function Change(img)
{
var image1 = document.getElementById(&quot;image1&quot;);
var image2 = document.getElementById(&quot;image2&quot;);

if(image1 != null && image2 != null)
{
switch(img)
{
case &quot;image1&quot;:
image1.style.display = &quot;&quot;;
image2.style.display = &quot;none&quot;;
break;
case &quot;image2&quot;:
image1.style.display = &quot;none&quot;;
image2.style.display = &quot;&quot;;
break;
}
}
}

/*]]&gt;*/
&lt;/script&gt;```