Jump to content

Trying to call an external JavaScript file from PHP page and not having much luck.


Go to solution Solved by Ch0cu3r,

Recommended Posts

Been beating my head against the wall on this and can't figure it out.  I thought I had the syntax down, and I can't see anything wrong with it.

 

So I have a web page which uses PHP to allow you to create a new folder on the server.  It's a text box, with a submit button next to it.  You type in your folder name and PHP creates the folder if it does not already exist. 

 

That part works.  I'm trying to slip in some JavaScript to do input validation.  The problem is that I can't seem to make the JavaScript piece run, at all.  I'm just trying to make the basic connection before doing the actual validation coding, so my JavaScript is nothing but a single function with an alert inside it right now.  Here's my JavaScript, entitled simpl

function validateNewDir()
{
	alert("HI THERE");
} 	

Here's my PHP script.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New directory page</title>

<script src="javascript/validationScript.js"></script>

</head>
<?php
session_start();

if(isset($_POST['makeDir']))
{

	//Flag variable to indicate that a folder already exists.
	$folderExists = FALSE;
	
	//Get the current directory.
	$currentDir = $_SESSION['currentDirectory'];
	
	//First make a list of everything in this folder.
	$allfiles = scandir($currentDir);
	
	//Array to hold just the folder names.
	$folders = array();
	
	//Look through, move only the directories to a secondary array.
	foreach($allfiles as $thisfile)
		if(is_dir("$currentDir/$thisfile"))
			array_push($folders,$thisfile);
	
	//Load new directory name into local variable.
	$newName = $_POST['dirName'];
	
	//Compare name to the list of existing directories.
	foreach($folders as $thisFolder)
		//If proposed folder name already exists, set flag to indicate such.
		if(strcasecmp($newName,$thisFolder) == 0)
			$folderExists = TRUE;
		
		//If folder exists, alert user
		if($folderExists)
		{
			echo '<p class="errormessage">Folder <span class="highlight">' .$newName .'</span>'; 
			echo ' already exists!  Please choose a new ';
			echo 'name.  Be aware that folder names are <strong>NOT</strong> case sensitive.</p>';
		}
		else 
		{
			echo '<p class="informationmessage">Folder <span class="highlight">' .$newName .'</span>';
		 	echo ' successfully created!</p>';
			mkdir("$currentDir/$newName");
		}
			

}
		
echo <<<GETDIRECTORYNAMEBLOCK

<div id="directorymaker">
<span>Enter the name of the directory you wish to create:   </span>
<form action="directorypage.php" method="post">
<input type="text" name="dirName" />													
<input type="submit" name="makeDir" value="Create this directory!" id="dirBox" onclick="validateNewDir()"/>
</form>
<a href="uploadpage.php">Click here to return to the main page.</a>
GETDIRECTORYNAMEBLOCK;

?>

It's really baffling.  I have an almost identical script named "javascriptTest.js" in the same folder.

function hi()
{
alert("Hello!");
}

If I alter the last line of the form in my PHP script to say <input type="submit" name="makeDir" value="Create this directory!" id="dirBox" onclick="hi()"/> and add <script src="javascript/javascriptTest.js"></script> in my page's header, THAT works fine.  The alert pops up when I hit the submit button.  I even ran both scripts through a Javascript emulator at http://writecodeonline.com/javascript/ and both work.  What the heck is going on? 

  • Solution

Make sure the browser is loading the javascript/validationScript.js file. To check this when you run the code above press F12 should bring up the Web Inspector or Development Tools. Click the console tab and refresh your page. If your browser cant find the javascript file you should see an error message logged.

 

Also this is bad way of handling sessions as

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New directory page</title>

<script src="javascript/validationScript.js"></script>

</head>
<?php
session_start();

session_start() should be called before any output is sent the browser, this includes text,html,white space etc. Should be on first line of any .php file that uses sessions.

Edited by Ch0cu3r

For equally baffling reasons, this seems to have started working with no additional modifications on my part.  RRRRRRRRRRRRRRRRRRGGGGGGGGGGGHHHH........

 

Thanks for your tip.  I'll keep that in mind for the future.

 

Make sure the browser is loading the javascript/validationScript.js file. To check this when you run the code above press F12 should bring up the Web Inspector or Development Tools. Click the console tab and refresh your page. If your browser cant find the javascript file you should see an error message logged.

 

Also this is bad way of handling sessions as

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New directory page</title>

<script src="javascript/validationScript.js"></script>

</head>
<?php
session_start();

session_start() should be called before any output is sent the browser, this includes text,html,white space etc. Should be on first line of any .php file that uses sessions.

 

I think I've heard this but don't get why.  If nothing is accessing the session variables, why should it matter that the session hasn't been started?

I think I've heard this but don't get why.  If nothing is accessing the session variables, why should it matter that the session hasn't been started?

It doesn't have anything to do with stuff accessing session variables, it has to do with the fact that session_start needs to send headers in order to assign the session cookie and cache restrictions. When you put your session_start after your HTML or other output like you did, whether it works or not is dependent on whether the server has been configured to use output buffering. If it has, things will appear to work fine. If it has not, you'll get a couple big fat WARNING errors and your session code will not work at all.

 

By calling session_start before any output, you ensure that it will work properly without being dependent on the server's configuration.

Thanks for the explanation kicken.  And thanks for the troubleshooting tip Ch0cu3r..  I have a feeling that my Javascript may not have been working because of some miniscule syntax error somewhere, like a character where it shouldn't be or a missing semicolon or something.  One of those little details that is so maddening because it's so hard to detect.

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.