du521521521 2017-05-23 06:08
浏览 23
已采纳

JS中的下拉列表,希望使用PHP保存在数据库中。 我不知道,可以帮帮我吗?

I want to upload a pdf file but need to make option which pdf is belong. The problem is when i make a severel option then have a second option based on first option. The second option is use js and i dont know how to post it in database.

This is Upload.php

 <div class="graph-form agile_info_shadow">
 <h3 class="w3_inner_tittle two">Upload File Here </h3>
 <div class="form-body">
 <form action="UploadProcess.php" method="post" enctype="multipart/form-data"> 
    <!-- <div class="form-group"> <label for="exampleInputEmail1">Name of Folder</label>
    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email"> </div> -->
    <div class="form-group"> <label for="chooseOption">Choose >> </label>

    <select name="main" id="main" onchange="ChangeList();setValue()" required>
    <option value="">--------------  OPTION  --------------</option>
    <option value="IMS">Integrated Management System</option>
    <option value="PM">Personnel Manual</option>
    <option value="NP">Nursing Procedure</option>
    <option value="SQM">Service Quality Management</option>
    <option value="IC">Infection Control</option>
    <option value="TRM">Training Manual</option>
    </select>


    <div id="div1"></div>

    </div>
    <div class="form-group"> <label for="chooseDepartment">Choose Department >> </label>

    <select name="department" id="department" onChange="setValue();" required> 

        <script>

var department = {};
department['IMS'] = ['Accident & Emergency', 'Admin', 'Audiology','Billing', 'Critical Care Unit', 'Diagnosis', 'Dietition', 'Finance','General Ward', 'Haemodialysis', 'Information Technology', 'Medical Record', 'Marketing', 'Nursing Administrations', 'O&G ', 'Operation Theatre','Pharmacy', 'Physiotheraphy', 'Public Relation', 'Purchasing', 'Quality', 'Surgical', 'Talent Management'];
    department['PM'] = ['Manual'];
    department['NP'] = ['Volume 1', 'Volume 2', 'Volume 3'];
    department['SQM'] = ['Form', 'Manual'];
    department['IC'] = ['Manual', 'MOH Infectious Guideline'];
    department['TRM'] = ['Pharmacy Services', 'Hospital Engineering Services', 'Laboratory Services(Lablink)','Medical Record Services', 'PR Marketing Services', 'Patient & Customer Care(Clinic Assistant)-PCC','Quality Services', 'Admin & Outsource Services', 'Talent Management Services', 'IT Services', 'Purchasing Services','Rehabilition Services(Physiotherapist)', 'Radiology Services', 'Oncology Services', 'Dietetic Services', 'Services Quality Management(SQM) & Customer Services', 'Finance Services', 'Nursing Services'];

    function ChangeList() {
    var mainList = document.getElementById("main");
    var departmentList = document.getElementById("department");
    var combine = mainList.options[mainList.selectedIndex].value;
    while (departmentList.options.length) {
                departmentList.remove(0);
                                        }
    var mains = department[combine];
        if (mains) {
                    var i;
                for (i = 0; i < mains.length; i++) {
                    var main = new Option(mains[i], i);

                 departmentList.options.add(main);
                                            }
                                        }
                                    } 

                                    function setValue()
                                    {
                                        document.getElementById("selected_value").value = document.getElementById("department").value;
                                    }
                                    </script>
                                        </select>
                                  </div>
                                    <div class="form-group"> <label for="exampleInputFile"></label> 
                                        <input type="file" name="file"/> <p class="help-block">Upload PDF files only.</p> </div> 
                                        <button type="submit" name="btn-upload" class="btn btn-default">Submit</button> </form> 
                                        </div>
<?php
if(isset($_GET['success']))
{
    ?>
    <label>File Uploaded Successfully...  <a href="view.php">click here to view file.</a></label>
    <?php
}
else if(isset($_GET['fail']))
{
    ?>
    <label>Problem While File Uploading !</label>
    <?php
}
else
{
    ?>
    <label></label>
    <?php
}
?>



                    </div>
                <!-- //inner_content_w3_agile_info-->
            </div>
    <!-- //inner_content-->

Then this is UploadProcess.php

<?php
include_once("../../connection.php");
    $link=Connection();
if (!$link)
{
    die('Could not connect: ' . mysqli_error($link));
}
mysqli_select_db($link, "doc_db");

//if(isset($_POST['upload'])&&$_FILES['userfile']['size']>0)
  if(isset($_POST['btn-upload']))
{
    $fileName = $_FILES['file']['name'];
    //$fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileType = $_FILES['file']['type'];
    $folder="../../file/";

    // new file size in KB
$new_size = $fileSize/1024;  
// new file size in KB

// make file name in lower case
$new_file_name = strtolower($fileName);
// make file name in lower case

$final_file=str_replace(' ','-',$new_file_name);

$main = htmlentities(stripslashes(mysqli_real_escape_string($link,$_POST['main'])));
$department = htmlentities(stripslashes(mysqli_real_escape_string($link,$_POST['department']))); 

if(move_uploaded_file($tmpName,$folder.$final_file))
{
    $sql="INSERT INTO upload(main,department,name,type,size) VALUES('$main','$department','$final_file','$fileType','$new_size')";
    mysqli_query($link, $sql);
    ?>
    <script>
    alert('successfully uploaded');
    window.location.href='Success.php';
    </script>
    <?php
}
else
{
    ?>
    <script>
    alert('error while uploading file');
    window.location.href='../View/View.php';
    </script>
    <?php
}
} 
?>
  • 写回答

1条回答 默认 最新

  • dsg56465 2017-05-23 06:46
    关注

    In "Upload.php":

    1) You miss < in select name="department".... So, write:

    <select name="department" id="department" onChange="setValue();" required>
    

    2) You are missing the "selected_value" input. So, insert this right after the submit button:

    <input type="hidden" id="selected_value" name="selected_value" value="" />
    

    Otherwise, this file is ok.

    In "UploadProcess.php":

    3) Be sure that you create the "file" folder. Otherwise, this file and the inserting in the db is working properly.

    Don't forget to close the connection. And I recommend you to implement exception handling. See mysqli sql exception.

    EDIT 1:

    I recommend you to include setValue() inside ChangeList(). So, replace

    <select name="main" id="main" onchange="ChangeList();setValue()" required>
    

    with

    <select name="main" id="main" onchange="ChangeList();" required>
    

    and in ChangeList() function add setValue(); as the last line:

    function ChangeList() {
        //...
        setValue();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取