简体   繁体   English

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

[英]I can't call a javascript function from within PHP code

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. 我搜索了stackoverflow和Internet的其余部分,但无法提出可行的解决方案。 I couldn't find in stackoverflow a question about the exact problem. 我在stackoverflow中找不到有关确切问题的问题。

I'm making a dummy application using PHP and AngularJS to practice in my spare times. 我正在使用PHP和AngularJS制作一个虚拟应用程序,以在业余时间进行练习。 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.) (然后,在另一页上,此数组用于使用AngularJS形成显示条目的表。这部分工作。)

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. 我从表单获取输入,如果一切正常,那么它应该形成一个字典(这也可以),然后进行JavaScript函数调用,该函数将字典推入字典数组。 This last part isn't working. 最后一部分不起作用。 I changed the function that's being called so that it only has document.write(dict); 我更改了被调用的函数,使其仅具有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. 所以我认为我在执行JavaScript函数调用时犯了一个错误,但是我不确定。

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. 它不会给出任何错误,但也不会执行任何操作。

Move JavaScript functions from document ready. 准备将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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript函数无法在php代码中调用 - JavaScript Function not working on call within php code 如何在iframe中调用javascript函数? - How can I call a javascript function from within iframe? 我如何在 updatepanel 中从后端调用 javascript 函数 - How can i call javascript function from backend within updatepanel 如何在JavaScript中的for循环中调用函数? - How can i call a function from within a for loop in javascript? 如何在Javascript中调用embeded swfs函数? - How can I call an embeded swfs function from within Javascript? 为什么我不能从JavaScript中的另一个函数调用一个函数? - Why can't I call a function from another function in javascript? 我想在我的javascript代码中调用一个php函数,但是我收到一个sytax错误 - I want to make a call to a php function within my javascript code, but I get a sytax error 我有两个全局函数,如果我从函数1内调用函数2,可以从函数2内的函数1访问本地代码吗? - I have two global functions, if I call function 2 from within function 1, Can I access local code from function 1 within function 2 无法从PHP代码调用JavaScript函数? - Not able to call a JavaScript function from PHP code? PHP函数中的javaScript代码仅在首次调用时起作用 - javaScript code within PHP function only work in first call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM