dongzhuo5185 2016-10-14 14:48
浏览 51
已采纳

如何在codeigniter中的单个表单操作中发布多个页面的数据?

Consider the following scenario.

View file code

<form id="userProfileForm" action="<?php echo $_SESSION['base_url'];?>home/edit_user" method="post" enctype="multipart/form-data" class="Paragraph-Text">
    <label>Name:
        <input type="text" name="name">
    </label>
    <label>Password:
        <input type="password" name="password">
    </label>
    <label for="1" onclick="open_dialog();" class="custom-file-upload font-s-10">
        <i class="glyphicon glyphicon-upload"></i> UPLOAD FILE
    </label>
    <input type="submit">
</form>

Script

<script>
    function open_dialog()
    {       
        $.ajax({
         type: "POST",
         url: "<?php echo site_url('home/upload_dialog');?>",
         data: {count:1},
         dataType: "html",
         success: 
              function(html){
                $('body').append(html);
                $('#child-overlay').animate({opacity: 1}, 400);
              }     
        });
    }
</script>

Controller home

function upload_dialog()
    {   
        $this->load->view('home/home-upload-dialog');
    }
function upload_area($count)
    {
        $data['count'] = $count;
        $data['accept_file_types']='gif|jpe?g|png|jpg|pdf|cr2|docx|avi|mp4';
        $this->load->view('home/upload-area' ,$data);
    }

View home-upload-dialog

<div id="child-overlay-upload">
    <div id="popup">
        <div class="heading-area">
            <p class="heading">UPLOAD FILE</p>
            <span id="close"><img src='<?php echo image_url("icons/Cancel-icon.png")?>'></span>
        </div>
        <div id="popup-contents">
            <iframe id="upload-frame" frameborder="0" scrolling="no" height="200" width="100%" src="<?php echo site_url('home/upload_area/' . $_REQUEST['count']);?>"></iframe>
        </div>
    </div>
</div>

View upload_area

<input type="hidden" name="file_name" value="someValue">

Here, how do I post the data (from inputs "name","password","file_name") through my form action to my controller ? Any help would be appreciated. Thank you.

PS: Please don't ask why I want to do this.

  • 写回答

1条回答 默认 最新

  • dongmangwei3822 2016-10-14 15:55
    关注

    First Looking at your HTML, when a user hits the submit button the function edit_user() in your home controller is called

    <form id="userProfileForm" action="<?php echo $_SESSION['base_url'];?>home/edit_user" method="post" enctype="multipart/form-data" class="Paragraph-Text">
    

    So In your edit_user() function in the home controller you would then do something like this

    edit_user() {
      $username = $_POST['name'];
      $password = $_POST['password'];
    }
    

    That only gets you access to the name and password. As for the file that your trying to upload, your missing an <input> in your html, see below.

    <form id="userProfileForm" action="<?php echo $_SESSION['base_url'];?>home/edit_user" method="post" enctype="multipart/form-data" class="Paragraph-Text">
    <label>Name:
        <input type="text" name="name">
    </label>
    <label>Password:
        <input type="password" name="password">
    </label>
    <label for="1" class="custom-file-upload font-s-10">
        <i class="glyphicon glyphicon-upload"></i> UPLOAD FILE
        <input type="file" name="filename">
    </label>
    <input type="submit">
    

    With that additional input tag you would then change your edit_user() function like so

    edit_user() {
      $username = $_POST['name'];
      $password = $_POST['password'];
      $filename = $_FILES['filename']['name'];
    }
    

    You might need to mess around a little bit to get exactly what you were looking for with that upload_dialog() function. Here is some references

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示