找不到php文件

时间:2015-01-20 13:58:01

标签: php html

我有一个将表单数据发布到PHP文件的HTML文件(或者无论如何都应该这样)。 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文件。有什么想法吗?

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您的PHP脚本失败,因为它永远不会进入main if($ _POST vars未设置)。 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="">

答案 2 :(得分:0)

你说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">

答案 3 :(得分:0)

您的表单操作是提交给&#34; form_json.php&#34;但是你说你的文件名是&#34; form_data.php&#34;,把你的行动改成那个名字