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.

9
  • You haven't done anything to load the external JS. Commented May 17, 2016 at 13:50
  • there's a ";" missing behind your console.log in startUpload Commented May 17, 2016 at 13:52
  • @Robert — That's allowed under the semi-colon insertion rules. Commented May 17, 2016 at 13:52
  • Trailing semi-colon after </script> why? Commented May 17, 2016 at 13:53
  • @Quentin - the JSfunctions are loaded, aren't they? If not, how would I go about loading it? Commented May 17, 2016 at 13:55

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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")
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?
That could be the problem check this other question stackoverflow.com/questions/9899372/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.