Jump to content

Moved Javascript into .php file and now it isn't running?


coreysnyder04

Recommended Posts

Ok I had this javascript file

 

list.js

// JavaScript Document

function SelectSubCat(){

// ON selection of category this function will work

removeAllOptions(document.registerPlayer.skillLevel);

addOption(document.registerPlayer.skillLevel, "", "skillLevel", "");



if(document.registerPlayer.league.value == 'highschool'){

addOption(document.registerPlayer.skillLevel,"Freshman", "Freshman");

addOption(document.registerPlayer.skillLevel,"JV", "JV");

addOption(document.registerPlayer.skillLevel,"Varsity", "Varsity");

}

else if(document.registerPlayer.league.value == 'Chiller'){

addOption(document.registerPlayer.skillLevel,"A - Thursday", "A - Thursday");

addOption(document.registerPlayer.skillLevel,"B - Monday", "B - Monday");

addOption(document.registerPlayer.skillLevel,"C West - Tuesday", "C West - Tuesday");

addOption(document.registerPlayer.skillLevel,"C East - Tuesday", "C East - Tuesday");

addOption(document.registerPlayer.skillLevel,"C East - Wednesday", "C East - Wednesday");

addOption(document.registerPlayer.skillLevel,"C West - Wednesday", "C West - Wednesday");

addOption(document.registerPlayer.skillLevel,"C2 - Sunday", "C2 - Sunday");

addOption(document.registerPlayer.skillLevel,"C2 - Friday", "C2 - Friday");

addOption(document.registerPlayer.skillLevel,"D East - Thursday", "D East - Thursday");

addOption(document.registerPlayer.skillLevel,"D West - Sunday", "D West - Sunday");

addOption(document.registerPlayer.skillLevel,"D North - Sunday", "D North - Sunday");

addOption(document.registerPlayer.skillLevel,"D South - Sunday", "D South - Sunday");

addOption(document.registerPlayer.skillLevel,"E - Monday", "E - Monday");

//document.getElementById('skillLevel').value = <?=$skillLevel?>;

document.getElementById('skillLevel').value = 'C West - Tuesday';

//<?php print($skillLevel);?> 





}

else{

addOption(document.registerPlayer.skillLevel,"none", "No Skill Bracket Found.");

}



}

////////////////// 



function removeAllOptions(selectbox)

{

var i;

for(i=selectbox.options.length-1;i>=0;i--)

{

	//selectbox.options.remove(i);

	selectbox.remove(i);

}

}





function addOption(selectbox, value, text )

{

var optn = document.createElement("OPTION");

optn.text = text;

optn.value = value;



selectbox.options.add(optn);

}

 

Ok and I have a .php page with a form on it and I call the javascript via:

<script language="javascript" src="list.js"></script>
body onLoad="SelectSubCat();">

 

The java script code builds the drop down list and sets it to a value, in this case 'C West - Tuesday'. That is a hard coded value and I wanted to make it a dynamic value. But I read you can't use $php values inside of the .js file. So my idea was to add the js code into my .php file inside of a

<script language="JavaScript" type="text/javascript">

 

After doing so, my java script was no longer getting executed. I put a alert('hello'); at the top of my javascript code so it would be very clear that it was being ran, and nothing popped up. Any ideas?

 

I mean, it worked when the two files were separate.

I change a ..

<script language="javascript" src="list.js"></script> 

to a...

<script language="javascript">
// JavaScript Document
alert('hello');

function SelectSubCat(){
...............

 

And it stops working..

 

 

Here is my main page code but it is VERY UGLY!!

<?php
session_start();
if(!session_is_registered(myusername)){
$loggedin=0;}
else {$loggedin=1;}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Central Ohio Hockey.com</title>
<style type="text/css">
<!--
body {
background-color: #333333;
margin-top: 0px;
margin-bottom: 0px;
}
.style3 {color: #FFFFFF; font-weight: bold; }
.style19 {font-size: 16px}
.style20 {color: #FFFFFF}
-->
</style>
<script language="javascript" src="request.js"></script>
<script language="javascript">
// JavaScript Document
alert('hello');
//Fills the first Category But is not needed
function fillCategory(){ 
// this function is used to fill the category list on load
addOption(document.registerPlayer.Category, "Fruits", "Fruits", "");
addOption(document.registerPlayer.Category, "Games", "Games", "");
addOption(document.registerPlayer.Category, "Scripts", "Scripts", "");
}

function SelectSubCat(){
alert('hello');
// ON selection of category this function will work
removeAllOptions(document.registerPlayer.skillLevel);
addOption(document.registerPlayer.skillLevel, "", "skillLevel", "");

if(document.registerPlayer.league.value == 'highschool'){
addOption(document.registerPlayer.skillLevel,"Freshman", "Freshman");
addOption(document.registerPlayer.skillLevel,"JV", "JV");
addOption(document.registerPlayer.skillLevel,"Varsity", "Varsity");
}
else if(document.registerPlayer.league.value == 'Chiller'){
addOption(document.registerPlayer.skillLevel,"A - Thursday", "A - Thursday");
addOption(document.registerPlayer.skillLevel,"B - Monday", "B - Monday");
addOption(document.registerPlayer.skillLevel,"C West - Tuesday", "C West - Tuesday");
addOption(document.registerPlayer.skillLevel,"C East - Tuesday", "C East - Tuesday");
addOption(document.registerPlayer.skillLevel,"C East - Wednesday", "C East - Wednesday");
addOption(document.registerPlayer.skillLevel,"C West - Wednesday", "C West - Wednesday");
addOption(document.registerPlayer.skillLevel,"C2 - Sunday", "C2 - Sunday");
addOption(document.registerPlayer.skillLevel,"C2 - Friday", "C2 - Friday");
addOption(document.registerPlayer.skillLevel,"D East - Thursday", "D East - Thursday");
addOption(document.registerPlayer.skillLevel,"D West - Sunday", "D West - Sunday");
addOption(document.registerPlayer.skillLevel,"D North - Sunday", "D North - Sunday");
addOption(document.registerPlayer.skillLevel,"D South - Sunday", "D South - Sunday");
addOption(document.registerPlayer.skillLevel,"E - Monday", "E - Monday");
document.getElementById('skillLevel').value = <?=$skillLevel?>;
//document.getElementById('skillLevel').value = 'C West - Tuesday';
//<?php print($skillLevel);?> 
}
else{
addOption(document.registerPlayer.skillLevel,"none", "No Skill Bracket Found.");
}

}
////////////////// 
function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
	//selectbox.options.remove(i);
	selectbox.remove(i);
}
}
function addOption(selectbox, value, text )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;

selectbox.options.add(optn);
}
</script>

<script>
function handleOnChange(dd1)
{
  var idx = dd1.selectedIndex;
  var val = dd1[idx].text;
  var par = document.forms["registerPlayer"];
  var parelmts = par.elements;
  var prezsel = parelmts["skillLevel"];
  var country = val;
  if (country != "Select a League")
  {
   var directory = ""+document.location;
   directory = directory.substr(0, directory.lastIndexOf('/'));

   Http.get({
	url: "./" +  country + ".txt",
	callback: fillPrez,
	cache: Http.Cache.Get
}, [prezsel]);
  }
}
function fillPrez(xmlreply, prezelmt)
{
  if (xmlreply.status == Http.Status.OK)
  {
   var prezresponse = xmlreply.responseText;
   var prezar = prezresponse.split("|");
   prezelmt.length = 1;
   prezelmt.length = prezar.length;
   for (o=1; o < prezar.length; o++)
   {
     prezelmt[o].text = prezar[o];
   }
  }
  else
  {
   alert("Cannot handle the Ajax call.");
  }
}

</script>

<script language="JavaScript" type="text/javascript">
 /*
This is our function to use xmlhttp requrests, i did not write this part w3 schools did
[url=http://www.w3schools.com/ajax/ajax_browsers.asp]http://www.w3schools.com/ajax/ajax_browsers.asp[/url]
This was quicker than just making my own 
*/
var xmlHttp // xmlHttp variable

function GetXmlHttpObject()
{ // This function we will use to call our xmlhttpobject.
	var objXMLHttp=null // Sets objXMLHttp to null as default.
	if (window.XMLHttpRequest){ // If we are using Netscape or any other browser than IE lets use xmlhttp.
	objXMLHttp=new XMLHttpRequest() // Creates a xmlhttp request.
	}else if (window.ActiveXObject){ // ElseIf we are using IE lets use Active X.
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") // Creates a new Active X Object.
	} // End ElseIf.
	return objXMLHttp // Returns the xhttp object.
} // Close Function

function CheckUsername(username)
{ // This function we will use to check to see if a username is taken or not.
	xmlHttp=GetXmlHttpObject() // Creates a new Xmlhttp object.
	if (xmlHttp==null)
		{ // If it cannot create a new Xmlhttp object.
		alert ("Browser does not support HTTP Request") // Alert Them!
		return // Returns.
		} // End If.
	var url="usernamecheck.php?username="+username // Url that we will use to check the username.
	xmlHttp.open("GET",url,true) // Opens the URL using GET
	xmlHttp.onreadystatechange = function () 
		{ // This is the most important piece of the puzzle, if onreadystatechange is equal to 4 than that means the request is done.
			if (xmlHttp.readyState == 4) 
			{ // If the onreadystatechange is equal to 4 lets show the response text.
			document.getElementById("usernameresult").innerHTML = xmlHttp.responseText; // Updates the div with the response text from usernamecheck.php
			} // End If.
		}; // Close Function
	xmlHttp.send(null); // Sends NULL instead of sending data.
} // Close Function.

function validateForm(oForm)
 {
	//oForm refers to the form which you want to validate
	oForm.onsubmit = function() // attach the function to onsubmit event
	{
		var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; //Valid [email protected]
		var zipcode = /(^\d{5}$)|(^\d{5}-\d{4}$)/; //5 digit # || 5 digits_"-"_4 Digits
		var age = /(^\d{2}$)/; //2 Digit Number ONLY

		if(oForm.elements['username'].value.length<6)
		{
			alert("Your 'username' must be at least 6 characters.");
			return false;
		}
		if(oForm.elements['password'].value.length<1)
		{
			alert("You cannot leave the 'password' field empty.");
			return false;
		}
		if(oForm.elements['password'].value.length<6)
		{
			alert("Your password must be at least 6 characters.");
			return false;
		}
		if(oForm.elements['password2'].value.length<1)
		{
			alert("You must confirm the password.");
			return false;
		}
		if(oForm.elements['password'].value!=oForm.elements['password2'].value)
		{
			alert("Your password entries do not match.");
			return false;
		}
		if(!regex.test(oForm.elements['email'].value))
		{
			alert("Invalid email address format.");
			return false;
		}
		if(oForm.elements['email'].value!=oForm.elements['email2'].value)
		{
			alert("Your email entries do not match.");
			return false;
		}
		if(oForm.elements['firstName'].value.length<1)
		{
			alert("You cannot leave the 'First Name' field empty.");
			return false;
		}
		if(oForm.elements['lastName'].value.length<1)
		{
			alert("You cannot leave the 'Last Name' field empty.");
			return false;
		}
		if(oForm.elements['gender'].value.length<1)
		{
			alert("You cannot leave the 'gender' field empty.");
			return false;
		}
		if(oForm.elements['zip'].value.length<5)
		{
			alert("Your 'zip' must be at least 6 numbers.");
			return false;
		}
		if(!zipcode.test(oForm.elements['zip'].value))
		{
			alert("You must enter a valid zip.");
			return false;
		}
		if(!age.test(oForm.elements['age'].value))
		{
			alert("You must enter an age.");
			return false;
		}
		return true;
	}
 }
</script>
<link href="coh.css" rel="stylesheet" type="text/css" />
</head>
<?php 
		//Open COnnection
		$con = mysql_connect("xxxxx","xxxxx","xxxxx");
		if (!$con)
		{
			die('Could Not Connect: ' . mysql_error());
		}

		//Select Database
		mysql_select_db("xxxxx",$con);

		//Setup Variables	
		$username=$_SESSION['myusername'];

		//Build Select Statement
		$SQL="SELECT * FROM players_stats WHERE username='" .$username. "'";

		//Execute SQL STMT

		$result=mysql_query($SQL); 

		//Insert Data	
		while($row = mysql_fetch_array($result))
		{
		$username = $row['username'];
		$firstName = $row['firstName'];
		$lastName = $row['lastName'];
		$email = $row['email'];
		$password = $row['password'];
		$gender = $row['gender'];
		$age = $row['age'];
		$zip = $row['zip'];
		$league = $row['league'];
		$skillLevel = $row['skillLevel'];
		$seekingTeam = $row['seekingTeam'];
		$positionPlayed = $row['positionPlayed'];

		echo $firstName . "|" . $lastName . "|" . $email . "|" . $password . "|" . $gender . "|" . $age . "|" . $zip . "|" . $league . "|" . $skillLevel . "|" . $seekingTeam . "|" . $positionPlayed;
		}
	?>
<body onLoad="SelectSubCat();"><table width="987" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1" rowspan="5" bgcolor="#666666"> </td>
    <td width="900" height="10" align="right" bgcolor="#666666"><?php include("inc/Header.php"); ?></td>
    <td width="1" rowspan="5" bgcolor="#666666"> </td>
  </tr>
  <tr>
    <td width="987" height="95" background="Images/bannerbackground.gif"><div align="center"><a href="index.html"><img src="Images/banner2.jpg" alt="Central Ohio HOcke" width="454" height="95" border="0" /></a></div></td>
  </tr>
  <tr>
    <td width="900" height="12" align="center" bgcolor="#09A0F2"><table width="352" border="0" cellpadding="0" cellspacing="0" bgcolor="#0886CB">
      <tr>
        <td width="33%" align="center" class="Navigation"><span class="Navigation">Ice Hockey </span></td>
        <td width="33%" align="center" class="Navigation"><span class="Navigation">Street Hockey </span></td>
        <td width="33%" align="center" class="Navigation"><span class="Navigation">Field Hockey </span></td>
      </tr>
    </table>
    <!-- #BeginLibraryItem "/Library/NavBar.lbi" -->
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
<table width="352" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr align="center">
        <td width="20" bgcolor="#0886CB"><span class="style1"><a href="index.php" class="style20 style1">Home</a></span></td>
        <td width="11" bgcolor="#0886CB"><span class="style1">|</span></td>
        <td bgcolor="#0886CB"><span class="style20 style1"><a href="locations.php" class="style20 style1">Locations</a></span></td>
        <td width="11" bgcolor="#0886CB"><span class="style1">|</span></td>
        <td bgcolor="#0886CB"><span class="style20 style1"><a href="leagues.php" class="style20 style1">Leagues</a></span></td>
	<td width="11" bgcolor="#0886CB"><span class="style1">|</span></td>
        <td bgcolor="#0886CB"><a href="playersearch.php" class="style20 style1">Players</a></td>
        <td width="11" bgcolor="#0886CB"><span class="style1">|</span></td>
        <td bgcolor="#0886CB"><a href="http://www.centralohiohockey.com/forum/phpBB3/index.php" class="style20 style1">Forum</a></td>
        <td bgcolor="#0886CB"><span class="style20"></span></td>
        <td bgcolor="#0886CB"><span class="style20"></span></td>
        <td bgcolor="#0886CB"><span class="style20"></span></td>
        <td bgcolor="#0886CB"><span class="style20"></span></td>
        <td bgcolor="#0886CB"><span class="style20"></span></td>
      </tr>
</table>
<!-- #EndLibraryItem --></td>
  </tr>
  <tr>
    <td width="900"><table width="985" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        
        <td valign="top" bgcolor="#EAEBDC">
	<form action="playeradded.php" method="post" name="registerPlayer">
	<table width="100%" height="102%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#eaebdc">
          <tr>
            <td height="100%" class="xbig"><p>Member Registration</p>
              </td>
          </tr>
          <tr class="small">
            <td height="100%" class="small">* - Denotes required field</td>
          </tr>
          <tr>
            <td height="100%"><table width="100%" border="0" align="center" cellpadding="3" cellspacing="5">
              <tr>
                <td height="10" bgcolor="#d9d9b7"><strong class="big">Step 1:  Member Name and Password</strong></td>
                  </tr>
              <tr>
                <td align="left">
                    <table width="610" border="0" align="left" cellpadding="0" cellspacing="5">
                        <tr>
                          <td width="141" align="right"><strong>*Username: </strong></td>
                          <td width="453" align="left"><table width="441" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td width="144"><input type="text" value="<?php echo $username; ?>" disabled name="username" onchange="CheckUsername(this.value);" /></td>
                                <td width="240"> <div id="usernameresult"></div></td>
                              </tr>
                            </table>                           </td>
                        </tr>
                        <tr>
                          <td align="right"><strong>*Password: </strong></td>
                          <td width="453"><input type="password" value="<?php echo $password; ?>" disabled name="password" /></td>
                        </tr>
                      </table>				      </td>
                  </tr>
              </table>              </td></tr>
          <tr>
            <td height="100%"><table width="100%" border="0" align="center" cellpadding="3" cellspacing="5">
              <tr>
                <td bgcolor="#D9D9B7" class="big">Step 2:  Member Information</td>
              </tr>
              <tr>
                <td>
                  <table width="556" border="0" cellpadding="0" cellspacing="5">
                    <tr>
                      <td align="right"><strong>*Email:</strong></td>
                      <td><input type="text" value="<?php echo $email; ?>" name="email" /></td>
                    </tr>
				<tr>
                      <td align="right"><strong>*Confirm Email:</strong></td>
                      <td><input type="text" name="email2" value="<?php echo $email; ?>"/></td>
                    </tr>
                    <tr>
                      <td width="141" align="right"><strong>*First Name:</strong></td>
                      <td width="249"><input type="text" value="<?php echo $firstName; ?>" name="firstName" /></td>
                    </tr>
                    <tr>
                      <td align="right"><strong>*Last Name:</strong></td>
                      <td><input type="text" name="lastName" value="<?php echo $lastName; ?>" /></td>
                    </tr>
                    <tr>
                      <td align="right"><strong>*Gender:</strong></td>
                      <td><select name="gender" value="<?php echo $gender; ?>">
                        <option value="M">Male</option>
                        <option value="F">Female</option>
                        <option value="N">N/A</option>
                        </select>                        </td>
                    </tr>
                    <tr>
                      <td align="right"><strong>*Zip/Postal Code: </strong></td>
                      <td><input type="text" name="zip" value="<?php echo $zip; ?>" /></td>
                    </tr>
                    <tr>
                      <td align="right"><strong>*Birthdate:</strong></td>
                      <td><input type="text" name="age" value="<?php echo $age; ?>"/> 
                        Example: '07/12/1980'</td>
                    </tr>
                  </table>                  </td>
              </tr>
            </table>
              <table width="100%" border="0" align="center" cellpadding="3" cellspacing="5">
                <tr>
                  <td bgcolor="#D9D9B7" class="big">Step 3:  Hockey Information </td>
                </tr>
                <tr>
                  <td height="100%"><table width="618" border="0" cellspacing="5" cellpadding="0">
                    <tr>
                      <td width="137" align="right"><strong>General League:</strong></td>
                      <td width="466"><label>
                        <select name="league" value="<?php echo $league; ?>" onChange="handleOnChange(this);">
                          <option value="Select">Select a League</option>
		  <option <?php if($league == "Chiller") {echo "selected";} ?> value="Chiller">Chiller League</option>
                          <option <?php if($league == "highschool") {echo "selected";} ?> value="highschool">High School Ice Hockey</option>
                          <option <?php if($league == "osh") {echo "selected";} ?> value="osh">Ohio Street Hockey League</option>
                          <option <?php if($league == "corh") {echo "selected";} ?> value="corh">Central Ohio Roller Hockey</option>
                          </select>
                      </label></td>
                    </tr>
                    <tr>
                      <td width="137" align="right"><strong>Skill Bracket:</strong></td>
                      <td><select name="skillLevel" size="1" accesskey="skillLevel" id="skillLevel">
                          <option value="blank"></option>
		  <option value="SelectSkill">Select Skill Level</option>
                          </select></td>
                    </tr>
                    <tr>
                      <td width="137" align="right"><strong>Seeking a Team: </strong></td>
                      <td><select name="seekingTeam">
                        <option value="blank"></option>
					<option <?php if($seekingTeam == "N") {echo "selected";} ?> value="N">No</option>
                        <option <?php if($seekingTeam == "Y") {echo "selected";} ?> value="Y">Yes</option>
                        </select>                        </td>
                    </tr>
                    <tr>
                      <td width="137" align="right"><strong>Position Played: </strong></td>
                      <td><select value="w" name="positionPlayed">
                        <option value="blank"></option>
		<option <?php if($positionPlayed == "w") {echo "selected";} ?> value="w">Wing</option>
                        <option <?php if($positionPlayed == "c") {echo "selected";} ?> value="c">Center</option>
                        <option <?php if($positionPlayed == "d") {echo "selected";} ?> value="d">Defense</option>
                        <option <?php if($positionPlayed == "g") {echo "selected";} ?> value="g">Goalie</option>
                      </select>                      </td>
                    </tr>
                    <tr>
                      <td align="right"> </td>
                      <td>
				  <input name="Submit" type="Submit" id="Submit" value="Submit" src="Images/signin.gif" width="63" height="18"/></td>
                    </tr>
                  </table>                  
                    <br /></td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td height="100%"> </td>
          </tr>
        </table>
	</form>
	<script language="JavaScript">
new validateForm(document.forms['registerPlayer']);
</script>		 </td>
         <td width="120" valign="top" bgcolor="#D9D9B7"><?php include("inc/RightAdBanner.php"); ?></td>
      </tr>
    </table></td>
  </tr>
  <tr><span class="footerlink">
    <td width="900" align="center" bgcolor="#666666"><?php include("inc/Footer.php"); ?></td>
  </span></tr>
</table>
</body>
</html>

seems like it should work,, except for that you haven't shown where $skillLevel comes from. PHP can't get that from the javascript in the 2 previous lines. Its hard to tell what you doing until you explain that.

 

also try changing

 

<script language="javascript">

 

to

 

<script type="text/javascript">

Ok I made some changes and the Javascript is actually running now. Unfortunately I still haven't accomplished the reason I moved this JavaScript into the php file. I need to next php code into my JavaScript so I can set that drop down to a dynamic $php value. So basically change:

document.getElementById('skillLevel').value = 'C West - Tuesday';

 

to something like:

document.getElementById('skillLevel').value = <?php print($skillLevel);?> ;

 

where:

$skillLevel is = to the what the drop down needs to be.

 

Any ideas? When I try this, the JavaScript no longer executes.

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.