RLJ Posted November 25, 2010 Share Posted November 25, 2010 Hi, I had an html document with javascript that was running fine, but then I put the whole html document inside a PHP script with the print command (making sure " and ' was correct everywhere, etc.) and now the javascript doesn't work anymore. Is it possible to run javascript inside html inside PHP like this? If so, can someone see what I have done wrong? If not, is there a way around this (perhaps there is a PHP script that will do the same as my javascript?) My PHP script is as follows: <?php //some PHP commands here print" <html> <head> <script language='JavaScript'> function logreg(radio1,radio2,nametag,first_name,last_name) { if(document.getElementById(radio1).checked == true) {document.getElementById(nametag).disabled= true; document.getElementById(nametag).style.background='#DADADA'; document.getElementById(first_name).disabled= false; document.getElementById(first_name).style.background='#FFFFFF'; document.getElementById(last_name).disabled= false; document.getElementById(last_name).style.background='#FFFFFF'; } if(document.getElementById(radio2).checked == true) {document.getElementById(nametag).disabled= false; document.getElementById(nametag).style.background='#FFFFFF'; document.getElementById(first_name).disabled= true; document.getElementById(first_name).style.background='#DADADA'; document.getElementById(last_name).disabled= true; document.getElementById(last_name).style.background='#DADADA'; } } </script> </head> <body> //more html here </body> "; ?> Any help is much appreciated! Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/219830-running-javascript-inside-html-inside-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2010 Share Posted November 25, 2010 When you do a 'view source' in your browser, is the HTML/Javascropt present and correct? Are you calling the logreg() function in your HTML? Posting enough of your code that produces/reproduces the problem is the fastest way of getting a solution. And, php is a server side scripting language. It outputs content (HTML, Javascript, CSS, media) to the browser. The HTML/Javascript is not technically HTML/Javascript until the browser receives it and renders the HTML and executes the Javascript. As long as the content that php outputs to the browser is valid, this is not really a php problem. Quote Link to comment https://forums.phpfreaks.com/topic/219830-running-javascript-inside-html-inside-php/#findComment-1139568 Share on other sites More sharing options...
RLJ Posted November 25, 2010 Author Share Posted November 25, 2010 OK thanks. Hmmm yeah javascript & html shows up fine in page source. I call logreg as follows: (this is html) <input id="radio1" name="radio" type="radio" checked="checked" value="radio1" onchange="logreg('radio1','radio2','nametag','first_name','last_name')">some text <input id="radio2" name="radio" type="radio" value="radio2" onchange="logreg('radio1','radio2','nametag','first_name','last_name')">some more text Although it seems to be a html/javascript error, not PHP, as you say, since it's now no longer working in the original html file either..... I need sleep. If you can spot the error pls let me know, otherwise don't worry, since it doesn't appear to be PHP issue as I originally thought. Quote Link to comment https://forums.phpfreaks.com/topic/219830-running-javascript-inside-html-inside-php/#findComment-1139611 Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2010 Share Posted November 25, 2010 Your code works for me (clicking on the radio buttons alternates between the nametag field and the other two fields being enabled/disabled and what background color they are.) I'm going to guess that you don't have any elements with id's of 'nametag','first_name', and 'last_name', but since you didn't bother to post code that demonstrates the problem, just a guess. Also, stating what 'doesn't work' means would go a long way toward someone actually being able to help you. We are not standing right next to you and are totally dependent on seeing the code responsible for the symptoms and the symptoms through what you post in the thread. Quote Link to comment https://forums.phpfreaks.com/topic/219830-running-javascript-inside-html-inside-php/#findComment-1139623 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.