mstabosz Posted September 27, 2013 Share Posted September 27, 2013 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? Link to comment https://forums.phpfreaks.com/topic/282475-trying-to-call-an-external-javascript-file-from-php-page-and-not-having-much-luck/ Share on other sites More sharing options...
Ch0cu3r Posted September 27, 2013 Share Posted September 27, 2013 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. Link to comment https://forums.phpfreaks.com/topic/282475-trying-to-call-an-external-javascript-file-from-php-page-and-not-having-much-luck/#findComment-1451398 Share on other sites More sharing options...
mstabosz Posted September 27, 2013 Author Share Posted September 27, 2013 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? Link to comment https://forums.phpfreaks.com/topic/282475-trying-to-call-an-external-javascript-file-from-php-page-and-not-having-much-luck/#findComment-1451418 Share on other sites More sharing options...
kicken Posted September 27, 2013 Share Posted September 27, 2013 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. Link to comment https://forums.phpfreaks.com/topic/282475-trying-to-call-an-external-javascript-file-from-php-page-and-not-having-much-luck/#findComment-1451479 Share on other sites More sharing options...
mstabosz Posted September 28, 2013 Author Share Posted September 28, 2013 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. Link to comment https://forums.phpfreaks.com/topic/282475-trying-to-call-an-external-javascript-file-from-php-page-and-not-having-much-luck/#findComment-1451539 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.