简体   繁体   English

如何使用 AJAX 调用 php function 显示基于输入的 ZFC35FDC70D5FC69D269883A82C

[英]How can I use AJAX to call a php function that shows html form input based on conditionals?

In my form I have a checkbox that I want to use to trigger showing more fields.在我的表单中,我有一个复选框,我想用它来触发显示更多字段。 Those fields would be based on a set of variables.这些字段将基于一组变量。 From my understanding is that AJAX would be the best to do this, but I'm not sure how/where to start with it.据我了解,AJAX 将是最好的,但我不知道如何/从哪里开始。 Idealy this is done in real time prior to submitting for a mysql update.理想情况下,这是在提交 mysql 更新之前实时完成的。

Here is my input: <input type="checkbox" name="emp_acd" id="emp_acd" value="Y" onclick="showACD()" <?php if (,(strcmp($row_employees['emp_acd'];"Y"))) {echo "checked=\"checked\""?} ?> >这是我的输入: <input type="checkbox" name="emp_acd" id="emp_acd" value="Y" onclick="showACD()" <?php if (,(strcmp($row_employees['emp_acd'];"Y"))) {echo "checked=\"checked\""?} ?> >

Before realizing that php couldn't check the status in real-time, this is the php code I was attempting to use:在意识到 php 无法实时检查状态之前,这是我尝试使用的 php 代码:

if(file_exists($existing_acd) && (isset($_POST['emp_acd']) || $row_employees['emp_acd'] == 'Y')){
    echo '<a href="//www/Departments/HR/documents/Advance%20Care%20Directives/' . $row_employees['emp_fname'] . ' ' . $row_employees['emp_lname'] . '.pdf" class="view_acd">View Current ACD</a>';
} elseif (!empty($row_employees['emp_acd_file'] && (isset($_POST['emp_acd']) || $row_employees['emp_acd'] == 'Y')){
    echo '<a href="' . $row_employees['emp_acd_file'] . '" class="view_acd">View Current ACD</a>';
} else {
    echo '<label for="emp_acd_file" id="acd_up_label" class="button" style="display:none; float: left; width: 80%;">Upload ACD</label>';
    echo '<input type="file" name="emp_acd_file" class="file_upload" id="emp_acd_file" style="display:none;">';
} 

$existing_acd = dirname(__DIR__,3) . '\Departments\HR\documents\Advance Care Directives\\' . $row_employees['emp_fname'] . ' ' . $row_employees['emp_lname'] . '.pdf';

The idea being that once the checkbox is checked (or previously checked), php would check if a file already existed, then show a link to the file.这个想法是,一旦复选框被选中(或之前选中),php 将检查文件是否已经存在,然后显示文件的链接。 If the file doesn't exist and the checkbox is checked, then show a file upload.如果文件不存在并且复选框被选中,则显示文件上传。

The onclick function I'm using here is more for style, however, i tried adding an alert to trigger my php code to no avail我在这里使用的onclick function 更适合风格,但是,我尝试添加alert以触发我的 php 代码无济于事

<script>
        function showACD() {
            // Get the checkbox
            var checkBox = document.getElementById("emp_acd");
            // Get the output text
            var input = document.getElementById("acd_up_label");

            // If the checkbox is checked, display the output text
            if (checkBox.checked == true){
                alert("<?php showMore(); ?>");
                input.style.display = "block";
                checkBox.style.cssFloat ="left";
                checkBox.style.margin ="4px 20px 0px 0px";
            } else {
                input.style.display = "none";
                checkBox.style.cssFloat ="none";
                checkBox.style.marginRight ="4px auto 0px";
            }
        }
    </script>

and then changing my php code to:然后将我的 php 代码更改为:

<?php function showMore(){
    if(file_exists($existing_acd) && (isset($_POST['emp_acd']) || $row_employees['emp_acd'] == 'Y')){
        echo '<a href="//www/Departments/HR/documents/Advance%20Care%20Directives/' . $row_employees['emp_fname'] . ' ' . $row_employees['emp_lname'] . '.pdf" class="view_acd">View Current ACD</a>';
    } else {
        echo '<label for="emp_acd_file" id="acd_up_label" class="button" style="display:none; float: left; width: 80%;">Upload ACD</label>';
        echo '<input type="file" name="emp_acd_file" class="file_upload" id="emp_acd_file" style="display:none;">';
    } 
}?>

If I got your question correctly, and thinking about it - you don't need any checkbox at all.如果我正确地回答了您的问题并考虑了它-您根本不需要任何复选框

Create a emp_acd.php file:创建emp_acd.php文件:

<?php
if (
  $_SERVER['REQUEST_METHOD'] === 'GET'
) {

  // Other necessary $row_employees code here....

  $dir = dirname(__DIR__, 3);
  $emp_fname = $row_employees['emp_fname'];
  $emp_lname = $row_employees['emp_lname'];
  $emp_acd   = $row_employees['emp_acd'];

  $existing_acd = "$dir/Departments/HR/documents/Advance Care Directives/$emp_fname $emp_lname.pdf";

  // Send response data to AJAX call:

  $response = [
    "emp_acd_file" => $existing_acd,
    "emp_acd_file_exists" => file_exists($existing_acd),
    "emp_acd"   => $emp_acd,
    "emp_fname" => $emp_fname,
    "emp_lname" => $emp_fname,
  ];

  echo json_encode($response);
  exit;
}

Now for JavaScript, using the Fetch API .现在对于 JavaScript,使用Fetch API
Create a function in JS that will communicate with PHP.在 JS 中创建一个 function 将与 PHP 通信。 Let's call it get_emp_acd我们称之为get_emp_acd

const get_emp_acd = () => fetch("emp_acd.php").then((res) => res.json());

and add a function that will reflect that returned JSON data from PHP - and hide / show / generate elements accordingly:并添加一个 function ,它将反映从 PHP 返回的 JSON 数据 - 并相应地隐藏/显示/生成元素:

const handle_emp_acd_form = () => get_emp_acd().then((res) => {
  console.log(res); // The JSON response from PHP
  // Your form logic goes here
  // Hide / show / create elements according to the returned `res` data
});

Wherever you need to handle the form HTML elements call:无论您需要在哪里处理表格 HTML 元素,请调用:

handle_emp_acd_form();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM