简体   繁体   中英

Could not validate the form input fields using PHP and JavaScript

I am facing an issue while validating the input fields inside the form using PHP and JavaScript. I am providing my code below.

<form autocomplete="off" action="<?php echo base_url() . $tourModule; ?>/search" method="GET" role="search" onSubmit="return checkform();">
   <input type="text" data-module="<?php echo $module; ?>" class="hotelsearch locationlist<?php echo $tourModule; ?>" placeholder="Tourist Destination" value="<?php echo $_GET['txtSearch']; ?>">
   <input type="hidden" id="txtsearch" name="txtSearch" value="<?php echo $_GET['txtSearch']; ?>">
   <div class="col-md-12 form-group go-right colspecing col-xs-12 submit text-center">
      <button type="submit" class="btn btn-lg pfb0 loader">
              <?php echo trans( '012'); ?> </button>
  </div>
</form>
<script type="text/javascript">
    function checkform(){
        console.log('validate form');
        var textname=document.getElementById('txtsearch');
        if (textname.value=='' || textname.value==null) {
            alret('Please select Tourist Destination.');
            return false;
        }else{
            return true;
        }
    }
</script>

Here I need before submit the form the input field will validate but in my case when I am clicking on submit button checkform function is not executing at all. I need to check that validation.

There is a typo in your code. Change alret to alert

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