我无法从PHP代码中调用javascript函数

时间:2017-06-15 02:15:29

标签: javascript php angularjs

注意:我尝试了所有可以想到的方法,但没有成功。我搜索了stackoverflow和互联网的其余部分,但无法找到一个有效的解决方案。我在stackoverflow中找不到关于确切问题的问题。

我正在使用PHP和AngularJS制作虚拟应用程序,以便在我的空闲时间练习。我遇到问题的应用程序部分是它应该从新条目的表单中获取输入,如果成功,它应该将条目中的字典添加到通用数组中。 (然后,在另一个页面上,这个数组用于形成一个显示条目的表,使用AngularJS。这部分有效。)

我从表单中获取输入,如果一切正常,那么它应该形成一个字典(这也有效),然后进行JavaScript函数调用,将字典推送到一个字典数组。最后一部分不起作用。我更改了被调用的函数,因此它只有document.write(dict);,它也不起作用。所以我认为我在执行JavaScript函数调用时犯了一个错误,但是再次,我不确定。

这是我的代码:

//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>

它没有任何错误,但它根本没有做任何事情。

1 个答案:

答案 0 :(得分:0)

从文档准备中移动JavaScript函数。

//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);
}