php - how to update and display an option without reloading the page

Hello I have a select box in which when one item is selected a update_option event fires and get's the selected ite

Hello I have a select box in which when one item is selected a update_option event fires and get's the selected item's value and echo's it on the page without having to reloading the page. I've put a simple example below of what I am trying to do. I read that I have to use AJAX but I have no idea how this works.

<form action="" method="post">
<select name="SelectItem" size='4'>
   <option>item1</option>
   <option>item2</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(!empty($_POST["SelectItem"])){
        update_option('SelectItem', $_POST["SelectItem"]);
    }
}
echo get_option('SelectItem');
?>

Hello I have a select box in which when one item is selected a update_option event fires and get's the selected item's value and echo's it on the page without having to reloading the page. I've put a simple example below of what I am trying to do. I read that I have to use AJAX but I have no idea how this works.

<form action="" method="post">
<select name="SelectItem" size='4'>
   <option>item1</option>
   <option>item2</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(!empty($_POST["SelectItem"])){
        update_option('SelectItem', $_POST["SelectItem"]);
    }
}
echo get_option('SelectItem');
?>
Share Improve this question asked Feb 25, 2017 at 11:01 iani garthalskiiani garthalski 471 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

Hi mate this is a really good tutorial for using Ajax in WordPress. Everything you'll need for what you want is basically covered. https://premium.wpmudev/blog/using-ajax-with-wordpress/

Essentially Ajax is just a custom jquery function in your JavaScript file that listens for an action on the page. It fires to the server to run the php function you specify, and then returns data from the server.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745574802a4633898.html

相关推荐

  • php - how to update and display an option without reloading the page

    Hello I have a select box in which when one item is selected a update_option event fires and get's the selected ite

    14小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信