找不到 php 文件


Cannot find php file

我有一个HTML文件,它将表单数据发布到PHP文件(或者无论如何都应该)。HTML 和 PHP 文件位于我项目中的同一文件夹中,但是当我提交表单时,我收到一条通知,指出"找不到资源。我的PHP文件的名称是"patient_json.php"。

这是我的网页

@using OnboardingProject.App_Code
@using OnboardingProject.Controllers
@{
    ViewBag.Title = "Patients";
}
<div class="title">
    <div>
        <h1 style="float: left">@ViewBag.Title</h1>
    </div>
    <div class="rmm" style="float: right; display: inline-block">
        <ul>
            <li><button id="NewPatient">New Patient</button></li>
        </ul>
    </div>
</div>
<div id="modal_content">
    <div id="modal_window" title="Complete the form below to add a new patient:">
        <div style="text-align: right;"><a id="modal_close" href="#">close <b>X</b></a></div>
        <form id="add_patient" method="POST" action="patient_json.php" accept-charset="UTF-8">
        <p><label>First Name<strong>*</strong><br>
        <input type="text" autofocus required size="48" id="fname" value=""></label></p>
        <p><label>Last Name<strong>*</strong><br>
        <input type="text" autofocus required size="48" id="lname" value=""></label></p>
        <p><label>Birthdate (mm/dd/yyyy)<strong>*</strong><br>
        <input type="text" autofocus required size="48" id="bday" value=""></label></p>
        <p><label>Site Name<strong>*</strong><br>
        <input type="text" autofocus required size="48" id="location" value=""></label></p>
        <p><label>SSN<strong>*</strong><br>
        <input type="text" autofocus required size="48" id="pat_ssn" value=""></label></p>
        <p><input type="submit" id="addPatient" value="Add Patient"></p>
        </form>
    </div>
</div>
<div class="content">
    <div id="patient_table">
        <table id="patients">
            <tr>
                <th id="p_name">Patient Name</th>
                <th id="p_site">Site</th>
                <th id="dob">Date of Birth</th>
                <th id="ssn">SSN</th>
                <th id="edits"></th>
            </tr>
        </table>
    </div>
</div>
<script src="@Url.Content("~/Scripts/PatientInfo.js")" type="text/javascript"></script>

这是我的PHP文件:

<?php
// check if all form data are submited, else output error message
if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['bday']) && isset($_POST['location']) && isset($_POST['pat_ssn'])) {
  // if form fields are empty, outputs message, else, gets their data
  if(empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['bday']) || empty($_POST['location']) || empty($_POST['pat_ssn'])) {
    echo 'All fields are required';
  }
  else {
    // adds form data into an array
    $formdata = array(
      'firstname'=> $_POST['fname'],
      'lastname'=> $_POST['lname'],
      'bday'=> $_POST['bday'],
      'location'=> $_POST['location'],
      'ssn'=> $_POST['pat_ssn']
    );
    // encodes the array into a string in JSON format (JSON_PRETTY_PRINT - uses whitespace in json-string, for human readable)
    $jsondata = json_encode($formdata, JSON_PRETTY_PRINT);
    // saves the json string in "pat_data.txt"
    // outputs error message if data cannot be saved
    if(file_put_contents('pat_data.txt', $jsondata)) echo 'Data successfully saved';
    else echo 'Unable to save data in "formdata.txt"';
  }
}
else echo 'Form fields not submitted';
// path and name of the file
$filetxt = 'pat_data.txt';
// check if the file exists
if(file_exists($filetxt)) {
  // gets json-data from file
  $jsondata = file_get_contents($filetxt);
  // converts json string into array
  $arr_data = json_decode($jsondata, true);
  // Now you can use the array $arr_data with json-data saved in text file
  var_export($arr_data);        // Test to see the array
}
else echo 'The file '. $filetxt .' not exists';
// path and name of the file
$filetxt = 'pat_data.txt';
// check if all form data are submited, else output error message
if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['bday']) && isset($_POST['location']) && isset($_POST['pat_ssn'])) {
  // if form fields are empty, outputs message, else, gets their data
  if(empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['bday']) || empty($_POST['location']) || empty($_POST['pat_ssn'])) {
    echo 'All fields are required';
  }
  else {
    // gets and adds form data into an array
    $formdata = array(
      'firstname'=> $_POST['fname'],
      'lastname'=> $_POST['lname'],
      'bday'=> $_POST['bday'],
      'location'=> $_POST['location'],
      'ssn'=> $_POST['pat_ssn']
    );
    // path and name of the file
    $filetxt = 'pat_data.txt';
    $arr_data = array();        // to store all form data
    // check if the file exists
    if(file_exists($filetxt)) {
      // gets json-data from file
      $jsondata = file_get_contents($filetxt);
      // converts json string into array
      $arr_data = json_decode($jsondata, true);
    }
    // appends the array with new form data
    $arr_data[] = $formdata;
    // encodes the array into a string in JSON format (JSON_PRETTY_PRINT - uses whitespace in json-string, for human readable)
    $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);
    // saves the json string in "pat_data.txt"
    // outputs error message if data cannot be saved
    if(file_put_contents('pat_data.txt', $jsondata)) echo 'Data successfully saved';
    else echo 'Unable to save data in "pat_data.txt"';
  }
}
else echo 'Form fields not submited';
?>

同样,它们位于项目的同一文件夹中,因此我无法弄清楚为什么找不到PHP文件。有什么想法吗?

您正在提交 HTML 代码中的patient_json.php,而不是patient_data.php

你的 PHP 脚本失败了,因为它从未进入主 if(未设置 $_POST 变量)。POST 表单中的字段由名称引用,而不是 id。

只需添加 name="fname" 作为附加参数,紧挨着 id="fname",依此类推,即可为每个输入字段添加。

<input type="text" autofocus required size="48" id="fname" value="">

成为

<input type="text" autofocus required size="48" id="fname" name="fname" value="">

你说你的php文件的名字是patient_data.php但你的代码有:

<form id="add_patient" method="POST" action="patient_json.php" accept-charset="UTF-8">

该操作是表单将带您到的页面,因此它应该是:

<form id="add_patient" method="POST" action="patient_data.php" accept-charset="UTF-8">

表单操作正在提交到"form_json.php",但您说文件名是"form_data.php",请将操作更改为该名称