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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误
  • ¥40 想找个软件,但我不知道怎么找到它,我甚至不知道有没有