簡體   English   中英

找不到php文件

[英]Cannot find php file

我有一個HTMLfile,可以將表單數據發布到PHP文件(或者應該是這樣)。 HTML和PHP文件在我的項目中的同一文件夾中,但是當我提交表單時,收到一條通知“找不到資源。我的PHP文件的名稱是“ Patient_json.php”。

這是我的html

@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文件。 有任何想法嗎?

您正在提交給patient_json.php在你的HTML代碼,而不是patient_data.php

您的PHP腳本失敗了,因為如果(如果未設置$ _POST vars),它將永遠不會進入主腳本。 POST表單中的字段是按名稱而不是ID引用的。

只需在id =“ fname”旁邊添加name =“ 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”,請將操作更改為該名稱

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM