docs:browsers:posting_forms

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
docs:browsers:posting_forms [2009/06/23 15:55] – created billhdocs:browsers:posting_forms [2009/11/03 10:17] (current) billh
Line 3: Line 3:
  
 ===== user presses enter in a text box and the form submits ===== ===== user presses enter in a text box and the form submits =====
-Actually, the first submittable object on the form is activated.  To control which element this is, add the attribute 'default'+Actually, the first submit-able object on the form is activated.  Use scripting to disable the form: add onsubmit="return false;to the FORM tag attributes.  Then when the user clicks the actual submit button, you use scripting to clear the form onsubmit value, and call form submit().  This way, users who have scripting enabled will have a form that works well by nothing happening when enter is pressed in a form field, and users with scripting disabled will only suffer the problem of having the first submit-able object activated.
-<code> +
-<input type="submit" name="submit" default="1" value="Submit" /> +
-</code>+
  
- Be careful checking for the submit key in the $_POST array because IE will not send it unless the user actually clicks the submit button, or presses enter on the button.  If the user presses enter in a text field, the 'submit' key will not be set in the $_POST array.+Also, be careful checking for the submit array key in $_POST because IE will not send it unless the user actually clicks the submit button, or presses enter on the button.  If the user presses enter in a text field, the 'submit' key will not be set in the $_POST array.
  
 ===== upload a file ===== ===== upload a file =====
-FIXME+To allow a file to be uploaded in a form, you need to add the **enctype** attribute, and provide a input of type "file" Remember that php has a limit on the maximum file size that can be uploaded. 
 +<code php> 
 +<form action="<?=$_SERVER['PHP_SELF']?>" method="POST" enctype="multipart/form-data"> 
 +<label>Please specify a file:</label> 
 +<input type="file" name="datafile" size="40"> 
 +<input type="submit" value="Send"> 
 +</form> 
 +</code> 
 + 
 +It is common to test for this to determine if a form was posted: 
 +<code php> 
 +if( $_POST ){ 
 +  // do something 
 +
 +</code> 
 +However, if the only thing in your form was a file input and a submit button, then the post array may be empty.  You should test for the files array instead: 
 +<code php> 
 +if( $_FILES ){ 
 +  // do something 
 +
 +</code> 
 + 
 +===== handling max file size during uploads ===== 
 +It is advisable to handle file sizes during uploads.  The easiest way to do this is to find out how large the file can be for uploads from apache and/or a php info page.  Then you add this element to the form:<code> 
 +<input type="hidden" name="MAX_FILE_SIZE" value="10485760" /> 
 +</code> 
 + 
 +Once the form is posted, you can check for errors like this:<code> 
 +if( $_POST ){ 
 + if( isset($_FILES['datafile']) ){ 
 + if( $_FILES['datafile']['error'] === UPLOAD_ERR_OK ){ 
 + // process the uploaded file 
 + }else{ 
 + // there was an error 
 +
 +
 +
 +</code> 
 + 
 +You can run a switch/case block if you prefer, and handle each error code differently.  See this page for more information about the file upload error codes: 
 +  * http://www.php.net/manual/en/features.file-upload.errors.php
  
 ===== multi select as an array ===== ===== multi select as an array =====
  • docs/browsers/posting_forms.1245794115.txt.gz
  • Last modified: 2009/06/23 15:55
  • by billh