SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to keep image input on if other validation fails?

    Hi I have created a form that allow user to enter 2 input and an image, I've valided the fields using if empty, the problem is if i fill the image input and leave the other empty it gives the validation error but also removes the image field that I have already placed on the image input,
    I've tried to keep like below but still always empty the image input.
    PHP Code:
    <INPUT type="file" name="userfile" value="<?php echo $userfile;?>">
    also tried
    <INPUT type="file" name="userfile" value="<?php if (isset($userfile)) { echo $userfile;} ?>">

    here my full script
    PHP Code:
    <?php
         
    include "connection.php"// find file (connection.php)  
     
    $message ""// this variable starts empty then bellow it will be given variables depend on the error message
     
    $error_name "";
     
    $error_dogsize "";
    if (isset (
    $_POST["submit"])) { // if post has been set/clicked run the code below

    $first_name mysql_real_escape_string($_POST['first_name']);
    $dogsize mysql_real_escape_string($_POST['dogsize']);

    // start validate name
      
    if (empty($first_name)) // if name is empty
        
    {
            
    $error_name ='Please enter name';
        }    
         else if(!
    preg_match("#^[-A-Za-z' ]*$#",$first_name)) { // name can only contain letter
            
    $error_name ='Name must contain letter only';
        }
        
          if (empty(
    $dogsize)) // if dogSize is empty
        
    {
            
    $error_dogsize ='Please select size of the dog';
        }    

        
    // end validation name   
       
    /*START IMAGE VALIDATION */
       // Configuration - Your Options
          
    $name $_FILES['userfile']['name']; // get the name of the file
          
    $type $_FILES['userfile']['type']; // get the type of the file
          
    $size $_FILES['userfile']['size']; // get the size of the file
          
          
    $allowed = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
          
    $max_filesize 524288// Maximum filesize in BYTES (currently 0.5MB).
          
    $upload_path './images/'// The place the files will be uploaded to (currently a 'files' directory).
               
               
    $ext substr($namestrpos($name,'.'), strlen($name)-1); // Get the extension from the filename.
         
         
    $fileType in_array($ext$allowed); // add the files type inside array filetype
         
       // Check if the filetype is allowed, if not DIE and inform the user.
       
    if(!$fileType) :
          
    $message '<br />The file you attempted to upload is not allowed.';
       endif;
       
       
    // Now check the filesize, if it is too large then DIE and inform the user.
        
    if($size $max_filesize) :
           
    $message 'The file you attempted to upload is too large.';
       endif;
     
         
    $upload is_writable($upload_path);
       
    // Check if we can upload to the specified path, if not DIE and inform the user.
       
    if(!$upload) :
            
    $message 'You cannot upload to the specified directory, please CHMOD it to 777.';
       endif; 
          
        
    $filename time().$ext// this will give the file current time so avoid files having the same name
       
       // if file type is less than maximum amount and file is uploaded and the error name error variable is empty run the code
        
    if($fileType && $size $max_filesize && $upload && $error_name == "" && $error_dogsize == ""
            { 
                  
    // Upload the file to your specified path.
                
    if(move_uploaded_file($_FILES["userfile"]["tmp_name"],$upload_path $filename))
    /* END IMAGE VALIDATION */
                    
    {
       
    // insert value into the database
                        
    $query "INSERT INTO animals (id, first_name, dogsize,  image)  
                                   VALUES ('', '
    $first_name', '$dogsize',  '$filename')";
                        
    mysql_query($query) or
                        die (
    mysql_error()); 
       
                        echo 
    time(). ' Your file upload was successful, view the file <a href="' $upload_path $filename '" title="Your File">here</a>'
                         
    $current_url = (empty($_SERVER['HTTPS']) ? "http://" "https://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                        
    header ('Location: ' $current_url);
                        exit ();
                         
    // It worked.
                    
    }
            }
      
    //    else
       //      echo 'There was an error during the file upload.  Please try again.'; // It failed :(.
    //http://stackoverflow.com/questions/2666882/how-to-avoid-resending-data-on-refresh-in-php

    //scape string http://stackoverflow.com/questions/13034868/form-to-insert-data-in-database-works-but-does-not-show-success-page
             
    }

    ?>

    <form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
    <h2>Upload an image</h2>
    <?php echo $error_name .'<br />';  ?>
    What is your name?<br>
    <input type="text" name="first_name" maxlength="50" value="<?php if (isset($first_name)) { echo $first_name;} ?>" /><br />
    <br />
    <?php echo $error_dogsize .'<br />';  ?>

    Select dog size:
    <select name="dogsize" id="dogsize">
      <option value="">Select</option>
      <option value="large">Large</option>
      <option value="medium">Medium</option>
      <option value="small">Small</option>
    </select>
    <br />
    <?php echo $message.'<br />';  ?>
    Upload an image: <br /><INPUT type="file" name="userfile" value="<?php if (isset($userfile)) { echo $userfile;} ?>"> 
    <br />
    <input type="submit" name="submit" value="Submit">
    </form>

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,168
    Mentioned
    454 Post(s)
    Tagged
    8 Thread(s)
    Code:
    (isset($userfile)) { echo $userfile;}
    I don't see $userfile set as a variable in your code anywhere.

  3. #3
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I just thought because thats the name of the image input
    do you know which variable I need so that when validates doesnt remove the image input so that the user doesnt have to add the image again everytime the other fails

  4. #4
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can see the form here if you add add an image but leave the other fields empty it will remove the image
    http://adoptadog.eurico.co.uk/index.php

  5. #5
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,168
    Mentioned
    454 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by macaela View Post
    Hi I just thought because thats the name of the image input
    That's not enough. You need to store the user input for that name as a variable called $userfile. What do you actually want to be displayed there if the form fails? The name? Perhaps you should just echo $name. (I don't know enough PHP to know if the other code you have will work anyhow, so I'll leave that to someone else.)

  6. #6
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well what I want is to retain the detail when upload the image like when you upload the image the upload field will contain the above
    C:\Users\\Desktop\tick bullets.jpg

    but if other fields fail it automaticaly removes that field so the user need to get the image again I want to retain that on the upload field.

  7. #7
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It's a complicated manual process.

    Basically you want to upload the file (move from the temp directory that PHP originally uploads to) to a publicly accessible temp folder. Then rewrite the form to display the image (or just the file name) and have a hidden field that has the image path. On a new form submission, you would check for this hidden field and use it if they didn't upload a new image.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •