Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required attribute not working using javascript submit

I make some form different action within different button

<form id="form" method="post" class="form-horizontal" enctype="multipart/form-data">
    <input name="name" class="form-control" type="text" required>
</form>
<button type="button" class="btn btn-primary" onClick="submitForm('<?php echo base_url('order/add');?>')">Submit</button>
<button type="button" class="btn btn-warning" onClick="submitForm('<?php echo base_url('order/print');?>')">Print</button>

Javascript

function submitForm(action)
{
    document.getElementById('form').action = action;
    document.getElementById('form').submit(

    );
}

Then, my required attribute not working. Did I do something wrong? Let me know if there is other solution. Thanks,

like image 252
Remn Avatar asked Oct 28 '25 16:10

Remn


1 Answers

Please try bellow code. i hope solve your problem.

<html>
  <head>
        <title>Submit</title>
        <script type="text/javascript" language="javascript">
                function submitForm(action)
                {
                    document.getElementById('form').action = action;
                    document.getElementById('form').submit(

                    );
                    //alert(document.getElementById('form').action);
                }
        </script>
  </head>
  <body>
        <form id="form" method="get" class="form-horizontal" enctype="multipart/form-data">
            <input name="name" class="form-control" type="text" required="required">
            <button type="submit" class="btn btn-primary" onclick="return submitForm('<?php echo base_url('order/add');?>');" id="submit">Submit</button>
            <button type="submit" class="btn btn-warning" onclick="return submitForm('<?php echo base_url('order/print');?>');" id="print">Print</button>
        </form>
  </body>
</html>
like image 93
Razib Al Mamun Avatar answered Oct 31 '25 04:10

Razib Al Mamun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!