开发者

Dynamically load form values and submit at same time?

I have a form of radio check boxes (yes/no), which I want to have dynamically update values as people click on the values.

So for example, if the user clicks on a No radio box, jQuery should submit the value immediately to my PHP which will update my database at that moment.

It should go both ways, meaning that when the form is first loaded, it will pull the data in reverse from my PHP form that will query the database and then update the form with the proper Yes/No values.

I have the PHP/Mysql backend already prepared. What I cant figure out is how to do the jQuery portion to submit the values on the fly, and at the same time on initial load it will pull the values and fill in my form with the correct/current 开发者_运维技巧values.

Thanks!


There are lots of ways to do this, but the simplest looks like this:

  1. Set the initial values in the HTML page with PHP.

  2. Use jQuery to add a click handler to the radio buttons. The click handler will use a $.get() or $.post() method to send an asynchronous request to the server. Note that this is not the same as submitting the form - it doesn't reload the page, and doesn't involve the actual form at all.

  3. The AJAX request goes to a PHP page on the server that receives the request, performs any necessary validation, updates the database, and (usually) sends back a success or failure message.

The jQuery code might look like this: http://jsfiddle.net/nrabinowitz/PyN7v/2/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜