douewei1665 2014-09-23 02:53
浏览 65
已采纳

Jquery Ajax将数据传递到另一个php文件来更新数据库只允许更新一次(Wordpress)

I am trying to redirect the data using POST method to a file in Wordpress called functions.php. It is able to send the data detected from the textbox to that file and gets the return value by using wp_send_json($_POST). After getting the returned value, the value of the paragraph was able to change to its new value. But it only allows me to update it once unless the page is being refreshed or reloaded. What have i done wrong or have i missed? Below are my codes for this process.

functions.php

function test_load(){
    if(isset($_POST) && $_POST['element'] == 'header')
    {
        $name = $_POST['name'];
        $hidden = $_POST['hidden'];

        if( $hidden == 'tagline')
        {
            update_option( 'wpbst_tagline', $name );
        }
        else if( $hidden == 'footer')
        {
            update_option( 'wpbst_footer', $name );
        }

        wp_send_json($_POST);
    }
}

add_action('init', 'test_load');

script

<script>

  $(document).ready(function(){

    function custom_update_box( id ) {

        $(id).click(function () {

          if(id == '#tagline')
          {
            var dynamicDialog = $('<div>\ <label>\ Tagline :\ </label>\  <input type="hidden" value="tagline" id="hidden">\ <input  id="update" class="form-control" type="textbox" value=\'' + $(this).html().trim() + '\'/>\ </div>');
            title = "Update Tagline";
          }
          else if(id == '#logo')
          {
            var dynamicDialog = $('<div>\ <label>\ Logo :\ </label>\  <input type="hidden" value="logo" id="hidden">\ <input id="update" class="form-control" type="textbox" value=\'' + $(this).html().trim() + '\'/>\ </div>');
            title = "Update Logo";
          }
          else if(id == '#footer')
          {
            var dynamicDialog = $('<div>\ <label>\ Logo :\ </label>\  <input type="hidden" value="footer" id="hidden">\ <input id="update" class="form-control" type="textbox" value=\'' + $(this).html().trim() + '\'/>\ </div>');
            title = "Update Footer";
          }

          dynamicDialog.dialog(
          { 
            title: title, 
            modal: true, 
            buttons: {
                'Submit': function(e) {
                  console.log(e);
                  $.ajax({
                    type: "POST",
                    data: { name: $("#update").val() , hidden: $("#hidden").val() , element: "header"}
                  }) 
                  .done(function(msg) {

                    console.log($("#update").val());

                    if(id == '#tagline')
                      $(id).html(msg.name);
                    else if(id == '#logo')
                      $(id).html(msg.name);
                    else if(id == '#footer')
                      $(id).html(msg.name);
                  });
                  $(this).dialog('close');
              },
                'Cancel': function() {
                $(this).dialog('close');
              }
            }

          });

      });

    }

    custom_update_box("#tagline");
    custom_update_box("#logo");
    custom_update_box("#footer");

  });

</script>

HTML

    <center>
      <span id="logo" class="bs-docs-booticon bs-docs-booticon-lg bs-docs-booticon-outline">
        <?php echo $logo_decode->wpbst_logo_initial; ?>
      </span>

        <td>
          <p id="tagline" name="blogdescription" class="lead"><?php echo $result_tagline;?></p>
          <input id="tagline" type="hidden" name='blogdescription' value=<?php echo '"'.$result_tagline.'"';?> />
        </td>

      <p class="lead">
        <a id="button" class="btn btn-outline-inverse btn-lg" href=""><?php echo $button_decode->wpbst_button_name;?></a>
      </p>
    </center>
  • 写回答

1条回答 默认 最新

  • douwan7382 2014-09-23 03:09
    关注

    I Have found a way, where I have removed the data from #update by adding the code inside the jquery ajax calling function:

    success:function(data) {
        $('#update').remove();
    }
    

    The code above remove the data inside #update and it answered my problem. If ever there would be a better way, I would love to learn about it too. As for now, :D

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器