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 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿