通过AJAX提交选择更改的表单

时间:2022-09-25 15:51:34

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.

我要做的是在选择change时发送表单,而不刷新页面。我知道我必须使用AJAX来实现这一点,但是我不知道如何实现它。

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 :

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 :

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:

<?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());
?>

1 个解决方案

#1


7  

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.

让跨浏览器处理更改事件和AJAX请求并非易事。我建议您使用某种javascript框架,它可以抽象出所有跨浏览器的问题,因此您不必担心它们。

Try a js framework

尝试一个js框架

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.

Jquery只是一个这样的框架,它有.change()等方法,它为

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

有关使用jquery的快速入门指南,请参见这里

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

这里是关于如何使用jquery ajax()方法的初学者教程

#1


7  

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.

让跨浏览器处理更改事件和AJAX请求并非易事。我建议您使用某种javascript框架,它可以抽象出所有跨浏览器的问题,因此您不必担心它们。

Try a js framework

尝试一个js框架

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.

Jquery只是一个这样的框架,它有.change()等方法,它为

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

有关使用jquery的快速入门指南,请参见这里

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

这里是关于如何使用jquery ajax()方法的初学者教程