Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form on select change via AJAX

Tags:

ajax

forms

submit

Let's say I have this form :

<form action="Change-status.php" method="post">
        <select class="changeStatus" name="changeStatus">
                <option value="0">Starting</option>
                <option value="1">Ongoing</option>
                <option value="2">Over</option>
        </select>
        <input class="projectId" type="hidden" name="projectId" value="<?php echo $data['id'];?>"/>
</form>

I am currently using this script to submit the form, but it implies refreshing :

$('select').change(function ()
{
    $(this).closest('form').submit();
});

What I want to do is to send the form on select change without refreshing the page. I know I have to use AJAX to do so but I couldn't exactly figure out how to implement it.

Could you orient me on how to do this?

Thanks for your help.

EDIT :

After taking comments into consideration, I ended up with the following code :

Html :

<form action="" method="post">
        <select class="changeStatus" name="changeStatus">
                <option value="0">Starting</option>
                <option value="1">Ongoing</option>
                <option value="2">Over</option>
        </select>
        <input class="projectId" type="hidden" name="projectId" value="<?php echo $data['id'];?>"/>
</form>

JS :

$(document).ready(function() {
    $('select.changeStatus').change(function(){
        $.ajax({
                type: 'POST',
                url: 'Change-status.php',
                data: {selectFieldValue: $('select.changeStatus').val(), projectId: $('input[name$="projectId"]').val()},
                dataType: 'html'
         });
    });
});

PHP :

<?php
    include('../Include/Connect.php');

    $changeStatus=$_POST['selectFieldValue'];
    $id=$_POST['projectId'];

    $sql='UPDATE project SET progress="'.$changeStatus.'" WHERE id="'.$id.'"';

    mysql_query($sql) or die("Erreur: ".mysql_error());
?>
like image 832
morgi Avatar asked Apr 28 '12 16:04

morgi


People also ask

How do I submit a form on select Change?

You can do it using a simple javascript. Here onChange event will be invoked whenever user changes the value in the select box and this. form. submit() method will submit the form.

Can we submit form using ajax?

We can submit a form by ajax using submit button and by mentioning the values of the following parameters. type: It is used to specify the type of request. url: It is used to specify the URL to send the request to. data: It is used to specify data to be sent to the server.


1 Answers

Getting cross browser onchange events and AJAX requests working isn't trivial. I'm recommend you use a javascript framework of some kind, which abstracts away all of the cross browser issues so you don't have to worry about them.

Try a js framework

Jquery is just one such framework which has methods such as .change() which attaches a handler to the change event for elements like <select> and .get() which performs a GET request.

Here's a little bit of code to get you started:-

// The $ is the shorthand for a jquery function, you can then use jquery 
// selectors which are essentially the same as css selectors, so here
// we select your select field and then bind a function to 
// it's change event handler
$('select.changeStatus').change(function(){

    // You can access the value of your select field using the .val() method
    alert('Select field value has changed to' + $('select.changeStatus').val());

   // You can perform an ajax request using the .ajax() method
   $.ajax({
       type: 'GET',
      url: 'changeStatus.php', // This is the url that will be requested

      // This is an object of values that will be passed as GET variables and 
      // available inside changeStatus.php as $_GET['selectFieldValue'] etc...
      data: {selectFieldValue: $('select.changeStatus').val()},

      // This is what to do once a successful request has been completed - if 
      // you want to do nothing then simply don't include it. But I suggest you 
      // add something so that your use knows the db has been updated
      success: function(html){ Do something with the response },
      dataType: 'html'
    });

});

Some references that will be better than my explanations

Please note for this code to work you will need to include the jquery library on you page with a <script> tag.

See here for a quick start guide on using jquery

And here for a beginners tutorial on how to use jquery's ajax() method

like image 158
rgvcorley Avatar answered Sep 20 '22 09:09

rgvcorley