Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery before submitting the form

I have a list, (a simple list) from which i am able to select and set elements (using js), and then a form that allows me to choose how many elements i want, and a submit form.if one doesn't select an element, there is a script that throws an exception. The problem is that i want that the form doesn't submit, if an element is not selected, but not throw an exception, but to show me a message down the submit button (using jquery). my script below:

<? foreach ($types as $type):?>
<ul class = "product_types">
    <? if ($type->stock_2 > 0):?>
    <li id = 'product_types'><a href="#" onclick='selecteazaElement(<?= $type->id; ?>,<?= $type->stock_2; ?>);'><?= $type->label; ?></a></li>
    <? else: ?>
    <li id = 'product_unavailable_types'><label><?= $type->label; ?></label></li>
    <? endif; ?>

</ul>
<? endforeach; ?>
<form  name="addtobasket" method="POST" action="<?= Route::url('Add to Basket', array('sale_id' => $sale->id)); ?>">
    <input type="hidden" name="idOfSelectedItem" id="idOfSelectedItem" value="-1">
    <select name="number" id="number">
        <option value=0>Alege numarul de produse</option>    </select>
    <button type="submit" name = "submit" onclick="addtobasket";>Adauga in cos</button><br />
</form>

and the js that sets the list elements:

   <script type="text/javascript">
   function selecteazaElement(id,stock)
    {
   document.addtobasket.idOfSelectedItem.value=id;
   window["canSubmit"] = true;
   var number23=document.addtobasket.number;
   number23.options.length=0;
   if (stock>=6)
   stock=6;
   for (i=1;i<=stock;i++)
    {
//alert ('id: '+id+'; stock: '+stock);
   number23.options[number23.options.length]=new Option(i, i);
   }
//window.status="my status";
}
like image 742
dana Avatar asked May 01 '11 22:05

dana


People also ask

How do you call a function before submitting a form?

You can put your form validation against this event type. The following example shows how to use onsubmit. Here we are calling a validate() function before submitting a form data to the webserver. If validate() function returns true, the form will be submitted, otherwise it will not submit the data.

How can we submit a form using jQuery?

Forms can be submitted either by clicking an explicit <input type="submit"> , <input type="image"> , or <button type="submit"> , or by pressing Enter when certain form elements have focus.

How do you check form is submitted or not in jQuery?

$("#something-%"). submit(function(e) { e. preventDefault(); $("#some-span"). html(data); });

Is jQuery submit deprecated?

No, it's not deprecated! Deprecated = Not current. Obsolete = no longer available.


1 Answers

Add a submit listener to the form. When it's submitted, check to see if an element is selected, and if not you can prevent the submission by using return false. Here's an example:

$('#myForm').submit(function()
{
    if (/* test case not true */) { 
        $('#myError').show();
        return false; 
    }

    // ... continue work
});

And here's the HTML:

<form id="myForm">
    <input type="text"/>
    <input type="submit"/>
</form>

If you don't want to use jQuery, you can also handle the submit event with plain JavaScript like so:

var myform = document.getElementById('myForm');
myform.addEventListener('submit', function() { console.log('Submitted!'); return false; });
like image 77
theabraham Avatar answered Sep 22 '22 13:09

theabraham