0

I am perplexed. I have done this before and know this should work but cannot understand why the function is not found. Perhaps a second set of eyes will uncover the mystery.

I have tested its existence using JavaScript. See the console log below.

PHP (skip to the end to see statement).

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once "dbconnect.php";

$uploadDirectory = '/home/deje/public_html/writers-tryst/uploads/'; //specify upload directory ends with / (slash)
require_once "dbconnect.php";
$result = 0;
$File_Name          = basename($_FILES['file2upload']['name']);
$File_Ext           = substr($File_Name, strrpos($File_Name, '.')); //get file extention
$Random_Number      = rand(0, 9999999999); //Random number to be added to name.
$NewFileName        = $Random_Number.$File_Ext; //new file name

$target_path = $uploadDirectory . $NewFileName;

if (@move_uploaded_file($_FILES['file2upload']['tmp_name'], $target_path)) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime_type = finfo_file($finfo, $uploadDirectory . $NewFileName);
    finfo_close($finfo); 
    if ($mime_type != 'application/pdf') {
        unlink($UploadDirectory . $NewFileName);
        $data = array('File MUST be a PDF!');
        $result = 0;
    } else $result = 1;
}
if (!isset($_REQUEST["title"]) || empty(trim($_REQUEST["title"])))
    throw new Exception('You must enter a title.');       
else {
    $title = filter_var(trim($_REQUEST["title"]), FILTER_SANITIZE_STRING);
    $title = htmlspecialchars_decode($title, ENT_QUOTES);
}
if (!isset($_REQUEST["userid"]) || empty(trim($_REQUEST["userid"])))
    throw new Exception('Userid is missing.');       
else {
    $userid = filter_var(trim($_REQUEST["userid"]), FILTER_SANITIZE_STRING);
    $userid = htmlspecialchars_decode($userid, ENT_QUOTES);
}
if (!isset($_REQUEST["work-type"]) || empty(trim($_REQUEST["work-type"])))
    throw new Exception('You must enter a work type.');       
else {
    $worktype = filter_var(trim($_REQUEST["work-type"]), FILTER_SANITIZE_STRING);
    $worktype = htmlspecialchars_decode($worktype, ENT_QUOTES);
}
if (!isset($_REQUEST["genre"]) || empty(trim($_REQUEST["genre"])))
    throw new Exception('You must enter a title.');       
else {
    $genre = filter_var(trim($_REQUEST["genre"]), FILTER_SANITIZE_STRING);
    $genre = htmlspecialchars_decode($genre, ENT_QUOTES);
}
if (!isset($_REQUEST["subgenre"]) || empty(trim($_REQUEST["subgenre"])))
    throw new Exception('You must enter a sub-genre.');       
else {
    $subgenre = filter_var(trim($_REQUEST["subgenre"]), FILTER_SANITIZE_STRING);
    $subgenre = htmlspecialchars_decode($subgenre, ENT_QUOTES);
}
if (!isset($_REQUEST["nbrPages"]) || empty(trim($_REQUEST["nbrPages"])))
    throw new Exception('You must enter the number of pages your work contains.');       
else {
    $nbrPages = filter_var(trim($_REQUEST["nbrPages"]), FILTER_SANITIZE_STRING);
    $nbrPages = htmlspecialchars_decode($nbrPages, ENT_QUOTES);
}

$dbh = connect2DB();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare(
    "INSERT Writers(fkAccounts, Title, WorkType, Genre, SubGenre, Filename) 
    VALUES(:fk, :title, :worktype, :genre, :subgenre, :filename)"
);

$stmt->bindParam(':fk', $userid, PDO::PARAM_INT, 10);
$stmt->bindParam(':title', $title, PDO::PARAM_STR, 255);
$stmt->bindParam(':worktype', $worktype, PDO::PARAM_STR, 30);
$stmt->bindParam(':genre', $genre, PDO::PARAM_STR, 100);
$stmt->bindParam(':subgenre', $subgenre, PDO::PARAM_STR, 100);
$stmt->bindParam(':filename', $NewFileName, PDO::PARAM_STR, 30);

$stmt->execute();

//echo "<script type='text/javascript'>stopUpload(" . $result . ");</script>";

?>
<script type='text/javascript'>stopUpload(1);</script>;

JS

 function startUpload() {
    console.log("stopUpload=" + stopUpload)
    $("#userid").val(window.localStorage.getItem('user-id'));
    showMessage(1, "Uploading...");
    return true;
 }

function stopUpload (success) {
    var result = '';
    if (success == 1) {
        showMessage(1, "File uploaded successfully");
    }
    else {
        showMessage(0, 'There was an error uploading the file.');
    }
    return true;
}

console log

stopUpload=function stopUpload(success) { var result = ''; if (success == 1) { showMessage(1, "File uploaded successfully"); } else { showMessage(0, 'There was an error uploading the file.'); } return true; } writers.php:1 Uncaught ReferenceError: stopUpload is not defined(anonymous function) @ writers.php:1

EDIT:

Please note the commented within the PHP code. That does not work either.

ron tornambe
  • 9,364
  • 6
  • 31
  • 57
  • You haven't done anything to load the external JS. – Quentin May 17 '16 at 13:50
  • there's a ";" missing behind your *console.log* in *startUpload* – Rob May 17 '16 at 13:52
  • @Robert — That's allowed under the semi-colon insertion rules. – Quentin May 17 '16 at 13:52
  • Trailing semi-colon after `` why? – mferly May 17 '16 at 13:53
  • @Quentin - the JSfunctions are loaded, aren't they? If not, how would I go about loading it? – ron tornambe May 17 '16 at 13:55
  • @rontornambe — The only ` – Quentin May 17 '16 at 13:56
  • You might want to reduce the test case while you're at it, e.g. I don't see how the PHP is relevant to JS errors. – Quentin May 17 '16 at 13:59
  • Your log there, are those 2 separate entries? 'Cause it seems like the first entry shows that `stopUpload()` is within scope of `startUpload()` (since it's `startUpload()` that's logging `stopUpload()` to the console in the first place), then the second entry (Uncaught Reference) falls out of scope. Needless to say, mixing PHP and JavaScript like this is a bad idea for obvious reasons. – mferly May 17 '16 at 14:04
  • Can anyone tell me if the DOM is available? – ron tornambe May 17 '16 at 14:28

1 Answers1

1

You are calling the function:

<script type='text/javascript'>stopUpload(1);</script>

But before you call it you need to load your JS:

<script src='./pathto/something.js'></script>; <!-- something.js declares stopUpload -->
<script type='text/javascript'>stopUpload(1);</script>
Remo H. Jansen
  • 17,470
  • 9
  • 63
  • 88
  • I am getting there. I assume I have to load all related js files, including jQuery. Is there a better way of doing this? I am really only checking success/failure or failure and displaying a message. – ron tornambe May 17 '16 at 14:17
  • If you don't want to load external JS then you will need to write pure javascript for example instead of `$("#userid")` you need to use `document.getElementById("userid")` – Remo H. Jansen May 17 '16 at 14:27
  • I am trying that, but without luck. I am issueing "var msg = document.getElementById('message')" but the element is not found. I know it is in the DOM. Is the DOM available? – ron tornambe May 17 '16 at 14:31
  • That could be the problem check this other question http://stackoverflow.com/questions/9899372/pure-javascript-equivalent-to-jquerys-ready-how-to-call-a-function-when-the – Remo H. Jansen May 17 '16 at 14:43