简体   繁体   中英

Doing PHP Submit and Fileupload from same button

I am writing a page where a user can fill out form data, select two files for upload, and then hit Submit to pass the form data into the PHP page, and also the two files will be uploaded.

The problem is the submit button seems to be able to only be either a 'submit' or a 'File_Upload'. It can't do both... or can it?

Here is the gyst of HTML file (Just enough to get the point across... I hope)

  <form class='form' name="frm_new_session" method="POST" enctype="multipart/form-data">  
    <div class='frm_row'>  
        <label>Title</label>  
        <input id="titleF" type='text' name="title" size='50' />  
        <input type="hidden" name="_chkuser" value="1"/>  
    </div>  
    <div class='frm_row'>  
        <label>Description</label>  
        <textarea id="descF" rows='3' cols='53' name="desc"></textarea>  
    </div>  
    <div class='frm_row'>  
        <label>Image</label>  
        <input id="imageF" type="file" name="uploadedimage">  
        <input type="hidden" name=MAX_FILE_SIZE" value='50000000'/>  
        </input> <br />  
    </div>  
    <div class='frm_row'>  
        <label>Session Media</label>  
        <input type='radio' id="mediaF" name="media" />  
        Upload: <input type='file' name="fileupload">  
        </input> <br />  
        <input type='radio' id="mediaF" name="media" />  
            Enter media URL:  
            <input type='text' size='75' name="mediaFile"/>  
        </input>  
    </div>                  
    <div class='frm_row'>  
        <input type="submit" value="Save Session"/>                     
        <div class='btn' type="submit" style='float: left;'>  
        <a href='#' id='btn_save_session'><span>Save Session</span></a>  
        <div class='kill_clear'></div>  
    </div>  
 </form>  

I know I can do this using multiple forms, but I would like to avoid that and enjoy having just a single 'Save' button.

Any suggestions?

i don't understand the problem, the info will be in the $_POST array and the files in the $_FILES array. however, you are missing a double quote in MAX_FILE_SIZE, i tested the following code, and it works :D

<?php
print_r($_POST);
print_r($_FILES);
?>
  <form class='form' name="frm_new_session" method="POST" enctype="multipart/form-data">  
    <div class='frm_row'>  
        <label>Title</label>  
        <input id="titleF" type='text' name="title" size='50' />  
        <input type="hidden" name="_chkuser" value="1"/>  
    </div>  
    <div class='frm_row'>  
        <label>Description</label>  
        <textarea id="descF" rows='3' cols='53' name="desc"></textarea>  
    </div>  
    <div class='frm_row'>  
        <label>Image</label>  
        <input id="imageF" type="file" name="uploadedimage">  
        <input type="hidden" name="MAX_FILE_SIZE" value='50000000'/>  
        </input> <br />  
    </div>  
    <div class='frm_row'>  
        <label>Session Media</label>  
        <input type='radio' id="mediaF" name="media" />  
        Upload: <input type='file' name="fileupload">  
        </input> <br />  
        <input type='radio' id="mediaF" name="media" />  
            Enter media URL:  
            <input type='text' size='75' name="mediaFile"/>  
        </input>  
    </div>                  
    <div class='frm_row'>  
        <input type="submit" value="Save Session"/>                     
        <div class='btn' type="submit" style='float: left;'>  
        <a href='#' id='btn_save_session'><span>Save Session</span></a>  
        <div class='kill_clear'></div>  
    </div>  
 </form>  
<form action="/add-news.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
<input type="text" name="title" />
<input type="file" name="image" />
<textarea rows="40" cols="50" name="content"></textarea>
</form>

Try adding hidden field MAX_FILE_SIZE where value is max size of files in bytes. It works for me.

What you mean by: "It can't do both", what are the indications?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM