如何使用formdata上传多个输入

时间:2018-11-19 14:01:14

标签: php jquery ajax

我有一个具有两种不同类型输入的表单。一个是type ='file',另一个是type ='text'。 现在,我试图通过在jquery中使用formdata()将这些输入发送到数据库,但是无论如何我都无法发送两个输入。

<form class='form-group' id='first_banner_form' enctype='multipart/form-data'>

    <label class='btn btn-warning btn-block textbutton'>
    upload your image<input type='file' class='hidden' 
    name='banner_number_one' id='banner_number_one'/>
    </label>

    <br>

    <input type='text' class='form-control' name='banner_number_one_addr' id='banner_number_one_addr'
    placeholder='write your name' style='text-align:center'/>
    <hr>

    </form>
    <br>
    <hr>
    <br>
    <button class='btn btn-success btn-block textbutton' id='upload_a_banner'>Upload</button>

这是我的jQuery代码

    $(document).on('click','#upload_a_banner',function(){

    var banner_form = new FormData($('#first_banner_form')[0]);



    $.ajax({
        url:'phpScript/upload_new_banners.php',
        method:'POST',
        data:banner_form,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success:function(data)
        {
            if(data=='done')
            {
                alert('uploaded');
            }elseif(data=='file is not set')
      { alert('database couldn\'t recieve all data'); 
 }
else{
            alert(data);
            }

        },error:function()
        {
            alert('error');
        }
    });

});

这是php代码

if(isset($_FILES['banner_number_one']) && isset($_FILES['banner_number_one_addr']))
{
    # the image
    $fr_banner_name = $_FILES['banner_number_one']['name'];
    $fr_banner_temp = $_FILES['banner_number_one']['tmp_name'];

    #the link
    $fr_link = $_FILES['banner_number_one_addr']['name'];



}else{
    echo 'file is not set';
        exit;
}

我刚写

if(isset($_FILES['banner_number_one']))

一切正常,但是当我写

 if(isset($_FILES['banner_number_one']) &&   isset($_FILES['banner_number_one_addr']))

我收到“文件未设置”消息。我该怎么办?

3 个答案:

答案 0 :(得分:1)

这是因为isCollapsedusing namespace std; class HotelRoom{ private: int roomnum; //Room numbers int roomcap; //Room capacity int roomoccuoystst = 0; int maxperperroom; double dailyrate; public: HotelRoom() { roomcap = 0; maxperperroom = 2; dailyrate = 175; } int gettotal = 0; int gettotallist = 0; string room; string guestroom,message; void viewrooms() { char viewselect, back; cout<<"Which room list would you like to view ?. 1 - Add rooms, 2 - Reserved rooms : " ; cin>>viewselect; switch(viewselect) { case '1': viewaddromm(); break; case '2': viewresromm(); break; default: cout<<"Please select from the option provided or go back to the main menu. 1 - view rooms, 2 - to the mail menu or any other key to exit the program : "; cin>>back; switch(back) { case '1': viewrooms(); break; case '2': hotelmenu(); break; default: exitpro(); } } } void viewresromm() { int occup,rmchoose,up; string roomtochange, items; string guestroomdb; int newaccupancy; char decisionmade,savinf; string fname, lname, nationality; string checkaddroom; ifstream getdatafromaddroom; //creation of the ifstream object getdatafromaddroom.open("reserveroom.out"); if(getdatafromaddroom.fail()) //if statement used for error checking { cout<<"Could not open file"<<endl; //message that will be printed if the program cannot open the file } cout<<endl; cout<<"First Name"<<'-'<<"Last Name"<<'-'<<"Nationality"<<'-'<<"Guest(s)"<<'-'<<"Room #"<<endl; cout<<"-------------------------------------------------------"<<endl; string items; while(!getdatafromaddroom.eof()) { // getdatafromaddroom >>fname>>lname>>nationality>>occup>>guestroomdb; getline(getdatafromaddroom, items); //cout<<setw(5)<<fname<<' '<<setw(10)<<lname<<' '<<setw(10)<<nationality<<' '<<setw(10)<<occup<<' '<<setw(9)<<guestroomdb<<endl; gettotallist++; if( getdatafromaddroom.eof() ) break; cout<<items<<endl; } for(int getlist = 0; getlist < gettotallist; getlist++ ) { cout<<items<<endl; // cout<<setw(5)<<fname<<' '<<setw(10)<<lname<<' '<<setw(10)<<nationality<<' '<<setw(10)<<occup<<' '<<setw(9)<<guestroomdb<<endl; } } void viewaddromm() { int occup,rmchoose,up; string roomtochange; string guestroomdb; int newaccupancy; char decisionmade,savinf; string fname, lname, nationality; string checkaddroom; fstream getdatafromaddroom; //creation of the ifstream object getdatafromaddroom.open("addroom.out"); if(getdatafromaddroom.fail()) //if statement used for error checking { cout<<"Could not open file"<<endl; //message that will be printed if the program cannot open the file } cout<<endl; cout<<"First Name"<<'-'<<"Last Name"<<'-'<<"Nationality"<<'-'<<"Guest(s)"<<'-'<<"Room #"<<endl; cout<<"-------------------------------------------------------"<<endl; string items; while(!getdatafromaddroom.eof()) { // getdatafromaddroom >>fname>>lname>>nationality>>occup>>guestroomdb; getline(getdatafromaddroom, items); //cout<<setw(5)<<fname<<' '<<setw(10)<<lname<<' '<<setw(10)<<nationality<<' '<<setw(10)<<occup<<' '<<setw(9)<<guestroomdb<<endl; gettotallist++; if( getdatafromaddroom.eof() ) break; cout<<items<<endl; } for(int getlist = 0; getlist < gettotallist; getlist++ ) { cout<<items<<endl; // cout<<setw(5)<<fname<<' '<<setw(10)<<lname<<' '<<setw(10)<<nationality<<' '<<setw(10)<<occup<<' '<<setw(9)<<guestroomdb<<endl; } } void exitpro() { cout<<"Program closing......Goodbye"<<endl; system("Pause"); exit(0); } menu() { char menuchoice; cout<<"[-------------------------------------------------------]"<<endl; cout<<"[-Welcome to the hotel booking and reseration menu-]"<<endl; cout<<"[--------------------------------------------------------]"<<endl; cout<<setw(30)<<"Addroom -- 1"<<endl; cout<<setw(32)<<"Reserve a room -- 2"<<endl; cout<<setw(34)<<" Modify a room -- 3"<<endl; cout<<setw(36)<<"View roms -- 4"<<endl; cout<<setw(38)<<" Exist -- 5"<<endl; cin>>menuchoice; switch(menuchoice) { case '1': Addroom(); break; case '2': reserveroom(); break; case '3': modifyroom(); break; case '4': viewrooms(); break; } } }; #endif 数组中:

banner_number_one_addr

答案 1 :(得分:1)

banner_number_one_addr是$ _POST的一部分,因此请使用isset($_POST['banner_number_one_addr'])而不是isset($_FILE['banner_number_one_addr'])

if(isset($_FILES['banner_number_one']) &&   isset($_POST['banner_number_one_addr'])){
  ...
}

答案 2 :(得分:1)

这是因为banner_number_one_addr位于$ _POST数组中:

$ _ POST ['banner_number_one_addr']