0

Note: I tried every means that I could think of, without success. I searched stackoverflow and the rest of the internet but couldn't come up with a working solution. I couldn't find in stackoverflow a question about the exact problem.

I'm making a dummy application using PHP and AngularJS to practice in my spare times. The part of the application I have problem with is that it should get input from a form for the new entry and if successful, it should add the dictionary made from the entry to a general array. (Then, on another page, this array is used to form a table showing the entries, using AngularJS. This part works.)

I get the input from the form and if everything is all right, then it should form a dictionary (this also works), then make a JavaScript function call, which pushes the dictionary to an array of dictionaries. This last part isn't working. I changed the function that's being called so that it only has document.write(dict);, which didn't work either. So I think I'm making a mistake in doing the JavaScript function call, but then again, I'm not sure.

This is my code:

//controllers.js
(function(){
    angular
        .module("resumeBase")
        .controller("tabularList", listController);

        function listController() {
            var vm = this;
            vm.data = applicants;
        }

        var applicants = [
            {
                firstname: "Nima",
                lastname: "Bavari",
                evaluation: 5,
                category: "IT & Computers",
                fileLocation: "",
                empConfirmed: "yes",
                confirmDate: "01-01-2017",
                employer: "EnDATA",
                payConfirmed: "yes"
            }, {
                firstname: "Ilkin",
                lastname: "Ali",
                evaluation: 5,
                category: "Design",
                fileLocation: "",
                empConfirmed: "no",
                confirmDate: "",
                employer: "",
                payConfirmed: "no"
            }
        ]

        function applicantFiller(applicant) {
            if (applicant.empConfirmed == "") {
                applicant.empConfirmed = "no";
                applicant.payConfirmed = "no";
            }
        }

        function addApplicant(dict) {
            applicants.push(dict);
            applicantFiller(dict);
        }
})();
//addNew.php
<!DOCTYPE html>
<html>
    <head>
        <title>resumeBase::Add New Entry</title>
        <link rel="stylesheet" type="text/css" href="style/main.css" />
    </head><body>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.js"></script>
        <script type="text/javascript" src="scripts/engine.js"></script>
        <script type="text/javascript" src="scripts/controllers.js"></script>
        <div id="container">
            <?php
            if (isset($_POST["submit"])) {
                $targetDir = "resumes/";
                $targetFile = $targetDir . basename($_FILES["resume"]["name"]);
                $uploadFinish = 1;
                $fileType = pathinfo($targetFile, PATHINFO_EXTENSION);

                $fname = $_POST["firstname"];
                $lname = $_POST["lastname"];
                $eval = $_POST["evaluation"];
                $cat = $_POST["category"];
                if ($fileType != "doc" && $fileType != "docx" && $fileType != "odt" && $fileType != "pdf") {
                    echo "<pre>Invalid file format!</pre>";
                    $uploadFinish = 0;
                } elseif ($_FILES["resume"]["size"] > 500000) {
                    echo "<pre>File too large! Choose another file.</pre>";
                    $uploadFinish = 0;
                } elseif (file_exists ($targetFile)) {
                    echo "<pre>File already exists! Choose another file or rename file.</pre>";
                    $uploadFinish = 0;
                } else {
                    $uploadFinish = 1;
                }

                if ($uploadFinish == 0) {
                    echo "<pre>File not uploaded!</pre>";
                } else {
                    if (move_uploaded_file($_FILES["resume"]["tmp_name"], $targetFile)) {
                        echo "The file " . basename($_FILES["resume"]["name"]) . " uploaded.";
                        $newApplicant = '{firstname: "' . $fname . '",';
                        $newApplicant .= 'lastname: "' . $lname . '",';
                        $newApplicant .= 'evaluation: ' . $eval . ',';
                        $newApplicant .= 'category: "' . $cat . '",';
                        $newApplicant .= 'fileLocation: "' . $targetFile . '"}';
            ?>
                        <script type="text/javascript">
                            addApplicant(<?php echo $newApplicant; ?>);
                        </script>
            <?php
                    } else {
                        echo "<pre>Error uploading file!</pre>";
                    }
                }
            }
            ?>
            <h1>Add New Entry</h1>
            <form action="" method="POST" enctype="multipart/form-data">
                <input type="text" name="firstname" id="id_firstname" maxlength="20" placeholder="First Name" required="required" />
                <input type="text" name="lastname" id="id_lasttname" maxlength="20" placeholder="Last Name" required="required" />
                <select name="evaluation">
                    <option selected disabled>Give an Evaluation</option>
                    <option value=1>1</option>
                    <option value=2>2</option>
                    <option value=3>3</option>
                    <option value=4>4</option>
                    <option value=5>5</option>
                </select><select name="category">
                    <option selected disabled>Choose Category...</option>
                    <option value="IT & Computers">IT & Computers</option>
                    <option value="Design">Design</option>
                    <option value="Services">Services</option>
                    <option value="Agriculture & Industry">Agriculture & Industry</option>
                    <option value="Finance">Finance</option>
                    <option value="Marketing & Sales">Marketing & Sales</option>
                    <option value="Administrative">Administrative</option>
                    <option value="Medical">Medical</option>
                    <option value="Science & Education">Science & Education</option>
                    <option value="Law">Law</option>
                    <option value="Other">Other</option>
                </select>
                <input type="file" name="resume" id="id_resume" required="required" />
                <input type="submit" name="submit" id="id_submit" value="Upload" />
            </form>
            [<a href="index.php">Search</a>]
        </div>
    </body>
</html>

It doesn't give any errors, but it doesn't do anything at all either.

1 Answer 1

0

Move JavaScript functions from document ready.

//controllers.js
(function(){
    angular
        .module("resumeBase")
        .controller("tabularList", listController);

    function listController() {
        var vm = this;
        vm.data = applicants;
    }

    var applicants = [
        {
            firstname: "Nima",
            lastname: "Bavari",
            evaluation: 5,
            category: "IT & Computers",
            fileLocation: "",
            empConfirmed: "yes",
            confirmDate: "01-01-2017",
            employer: "EnDATA",
            payConfirmed: "yes"
        }, {
            firstname: "Ilkin",
            lastname: "Ali",
            evaluation: 5,
            category: "Design",
            fileLocation: "",
            empConfirmed: "no",
            confirmDate: "",
            employer: "",
            payConfirmed: "no"
        }
    ]
})();

function applicantFiller(applicant) {
    if (applicant.empConfirmed == "") {
        applicant.empConfirmed = "no";
        applicant.payConfirmed = "no";
    }
}

function addApplicant(dict) {
    applicants.push(dict);
    applicantFiller(dict);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Upvoted. Thanks. Can you tell me what's the reasoning behind this, Shamil bhai?
Functions declared inside the $(function() are only accessible within that. If you need to use those outside, window object to make it global.
Your code doesn't work either. I think, it's because I can't access the array applicants from inside the function addApplicant... I tried taking the array out as a global, and then it became even worse. What to do?

Your Answer

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