Search code examples
javascriptphpjqueryformscodeigniter

jQuery - validate form


I tried this code but it still submit the form even the input has not been validated yet.

Here is the code for my form:

<a style="cursor: pointer;" id="upload_image">Upload Image</a></li>

<?php
 $attributes = array('id' => 'upload_form'); 
 echo form_open_multipart('c=web_page&m=upload_img', $attributes); 
?>

<input type='file' style="display:none;" name="photosubmit" id="photosubmit"/>
<?php echo form_cose(); ?>

Here is the code for my jQuery:

$("#upload_image").click(function(){
 $("#photosubmit").click();
  if( $('photosubmit').val() != "") //
   $("#upload_form").submit();
});

so here is my question:

How will I ensure that before I submit this form, my input has already a value..?


Solution

  • After one day of research, I tried this one and it works. It will only submit the form once you have made any changes to the file input type form(e.g you want to upload new file).

    $("#upload_image").click(function () {
      $("#photosubmit").click().change(function(){
        $("#upload_form").submit();
      });
    });
    

    I hope it will help someone in the future.