Jump to content

Recommended Posts

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.

 

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.

 

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.

 

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.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.