0

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.

3
  • "checkform function is not executing at all" — It is when I run that code. Commented Jul 12, 2019 at 9:26
  • 1
    Danger: This code is vulnerable to XSS User input needs escaping before being inserted into an HTML document!. Commented Jul 12, 2019 at 9:27
  • Could you rather share a JS Fiddle of this ? Commented Jul 12, 2019 at 9:35

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is correct. Your form is not being validated because of that error. You should be able to see this if you open the Dev Tools of the browser (usually F12) and check the console output. Anyway, there are simpler ways to do what you want like adding a 'required' attribute to the required input. Apart from that as @Quentin told you there are security issues in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.