[validate] Form Validation Plugin and File Upload

[validate] Form Validation Plugin and File Upload

Hi All!

I have a form which I'm currently validating and processing using the validate() plugin.

My issue is this - I have a form which includes a file input for upload of an image.

I have tested the processing php independent of the .validate() plugin (by inputting the php file name directly into the action attribute of the form), and it works fine - image uploads and is renamed as intended. When using the validate plugin the upload appears to just not occur. I have added:

  1. ini_set('display_errors',1);
to the processing file, but this does not throw any errors.

Is it actually possible to upload files through forms being processed through the validate() plugin? And if so, can anyone shed some light on what I'm doing wrong?

HTML form code and script:

  1. <script type="text/javascript">
            //$("#adminform").load('forms/add_record.php');});


                $("#add_form").validate({
                    debug: true,
                    rules: {
                        maker: "required",
                        origin: "required"                                   
                    },
                    messages: {
                        maker: "<br /><span style=\"color:#f00;\">A manufacturers name is required</span>",
                        origin: "<br /><span style=\"color:#f00;\">A country of origin is required</span>"
                    },
                    submitHandler: function(form) {
                        //
                        //alert($("#add_form").serialize());
                        $('#status').html('<p class="body_text">Please wait, your input is being processed...</p>');
                        $.post('../resources/process_add_record.php', $("#add_form").serialize(), function(data) {$('#adminform').html(data);});
                        $('#status').html('');
                        }
                });           
    </script>

    <p>
      <?php

        //connect to db as viewer
        require($_SERVER['DOCUMENT_ROOT'].'/cbsdb/resources/viewer_connect.php');
       
        $mainsection="tableware"; //The name of the table where web links are stored
       
        $query="SELECT * FROM ".$mainsection;
       
        $result= mysql_db_query($dbname,$query) or die('query error');
       
        $count = mysql_num_rows($result);
       
        //echo '<p style="text-align:center;">Current Number of Records: '.$count.'</p>';
       
    ?>

    <p class="body_text">
        Please enter the details of the new record in the following form:
       </p>
       <div id="add_form_container">
       <form name="add_rec" id="add_form" method="post" action="" enctype="multipart/form-data">
           <input type="hidden" name="tw_id" value="T<?php echo $count+1; ?>" />
        <input type="hidden" name="uri_string" value="<?php echo 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['QUERY_STRING']; ?>" />
        <input type="hidden" name="order_type" value="add_record" />
        <p><label for="maker" class="label">Maker's Name*:</label><input type="text" name="maker" id="maker" size="40" /></p>
        <p><label for="origin">Country of Origin*:</label><input type="text" name="origin" id="origin" size="20" /></p>
        <p><label for="g_ref">Godden Ref:</label><input type="text" name="g_ref" size="20" />
        <p><label for="g_date_st">Godden Start Date:</label><input type="text" name="g_date_st" size="20" />
        <p><label for="g_date_end">Godden End Date:</label><input type="text" name="g_date_end" size="20" /></p>
        <p><label for="k_ref">Kowalsky Ref:</label><input type="text" name="k_ref" size="20" />
        <p><label for="k_date_st">Kowalsky Start Date:</label><input type="text" name="k_date_st" size="20" />
        <p><label for="k_date_end">Kowalsky End Date:</label><input type="text" name="k_date_end" size="20" /></p>
        <p><label for="o_ref">Other Ref:</label><input type="text" name="o_ref" size="20" />
        <p><label for="o_date_st">Other Start Date:</label><input type="text" name="o_date_st" size="20" />
        <p><label for="o_date_end">Other End Date:</label><input type="text" name="o_date_end" size="20" /></p>
        <p><label for="device">Backstamp Devices:<br /> (comma separated)</label><input type="text" name="device" size="40" /> </p>
        <p><label for="mark_type">Mark Type:</label><input type="text" name="mark_type" size="40" /> </p>
        <p><label for="prov">Provenance:</label><input type="text" name="prov" size="40" /> </p>
        <p><label for="notes">Notes:</label><textarea name="notes" rows="5" cols="40" /></p>
        <!--<p><input type="hidden" name="MAX_FILE_SIZE" value="204800" />-->
           <label for="image">Image File:</label><input type="file" name="image" size="40" /></p>
        <p style="margin-left:250px;">
            <input type="button" value="Add Record" onclick="reveal_submit()"/><input type="reset" value="Reset Form" style="margin-left:50px;" />
           </p>
           <p class="body_text" id="hidden_submit"></p>
        </form>
        </div>
        <script type="text/javascript">
        function reveal_submit(){
            $('#hidden_submit').html('<span style="padding:25px; text-align:center; position:absolute; top:200px; left:550px; border:3px solid #F00;"<b>Please Confirm your inputs, then click the button below</b><br /><input type="submit" value="Submit New Record" /></span>');
        }
        </script> 












































































PHP Processing script:

  1. <?php

    ini_set('display_errors',1);

    //RECORD ADDITION SCRIPT
    if($_POST['order_type']=='add_record'){
            $tw_id=$_POST['tw_id'];
          $maker=$_POST['maker'];
          $origin=$_POST['origin'];
          if($_POST['g_ref']!=""){
              $g_ref=$_POST['g_ref'];
              $g_date_st=$_POST['g_date_st'];
              $g_date_end=$_POST['g_date_end'];
          }
          else{
              $g_ref=0;
              $g_date_st=0;
              $g_date_end=0;
          }
          if($_POST['k_ref']!=""){
              $k_ref=$_POST['k_ref'];
              $k_date_st=$_POST['k_date_st'];
              $k_date_end=$_POST['k_date_end'];
          }
          else{
              $k_ref=0;
              $k_date_st=0;
              $k_date_end=0;
          }
          if($_POST['o_ref']!=""){
              $o_ref=$_POST['o_ref'];
              $o_date_st=$_POST['o_date_st'];
              $o_date_end=$_POST['o_date_end'];
          }
          else{
              $o_ref=0;
              $o_date_st=0;
              $o_date_end=0;
          }
          if($_POST['device']){$device=$_POST['device'];}else{$device="";}
          if($_POST['mark_type']){$mark_type=$_POST['mark_type'];}else{$mark_type="";}
          if($_POST['prov']){$prov=$_POST['prov'];}else{$prov="";}
          if($_POST['notes']){$notes=$_POST['notes'];}else{$notes="";}
         
          $devices=explode(",",$device);
         
            require ($_SERVER['DOCUMENT_ROOT'].'/cbsdb/resources/admin_connect.php');
            $mainsection="tableware"; //The name of the table where web links are stored
           
            $unique_id==false;
           
            while($unique_id==false){       
            //check if proposed ID exists, and alter value if it does
            $check_query="SELECT * FROM $mainsection where TW_ID = '$tw_id'";
            $checkres = mysql_db_query($dbname,$check_query) or die("Query Error: ".mysql_error());
            if(mysql_num_rows($checkres)!=0){
                $id_no = substr($tw_id,1);
                $id_letter = substr($tw_id,0,1);
                $id_no = $id_no+1;
                $tw_id=$id_letter.$id_no;
            }
            else{$unique_id=true;}
            }
                           
           
            $query = "INSERT INTO ".$mainsection." VALUES ('$tw_id','$maker','$g_ref','$g_date_st','$g_date_end','$k_ref','$k_date_st','$k_date_end','$o_ref','$o_date_st','$o_date_end','$origin','$devices[0]','$devices[1]','$devices[2]','$devices[3]','$devices[4]','$mark_type','$prov','$notes')";
           
            $result = mysql_db_query($dbname, $query) or die("Failed Query of " . $query."<br />".mysql_error());
           
            echo 'Record '.$tw_id.' Added Successfully';
           
            $filedir=$_SERVER['DOCUMENT_ROOT'].'/cbsdb/images/tableware';
            foreach($_FILES as $file_name=>$file_array){
                echo 'path: '.$file_array['tmp_name'].'<br />'."\n";
                echo 'type: '.$file_array['type'].'<br />'."\n";
                if(move_uploaded_file($file_array['tmp_name'],$filedir.'/'.$tw_id.'.jpg')){
                    echo 'File Uploaded Successfully!';}
            }       
           
    }















































































Again - Thanks in advance for any help!

Cheers

Tim Bradley




















    • Topic Participants

    • admin