通过AJAX从单选按钮的形式将序列化数据添加到wp数据库

时间:2018-06-08 21:05:19

标签: php arrays ajax wordpress serialization

我有一个wordpress帖子,显示一个由几首列出的歌曲组成的表格,每首歌曲有2个单选按钮(“播放”和“不播放”)。

我想将这些单选按钮的数据存储在我的数据库中,作为2组序列化数据,即如果选中“播放”单选按钮

1:do_play =歌曲名称1 2:do_play =歌曲名称2 3:do_play =歌曲名称3 4:do_play =歌曲名称4 5:do_play =歌曲名称5

a:5:{i:0;s:11:"Song name 1";i:1;s:11:"Song name 2";i:2;s:11:"Song name 3";i:3;s:11:"Song name 4";i:4;s:11:"Song name 5";}

对于从“不播放”中选择的歌曲,相同,但在不同的数据库行中

1:do_play =歌曲名称6 2:do_play =歌曲名称7 3:do_play =歌曲名称8 4:do_play =歌曲名称9 5:do_play =歌曲名称10

a:5:{i:0;s:11:"Song name 6";i:1;s:11:"Song name 7";i:2;s:11:"Song name 8";i:3;s:11:"Song name 9";i:4;s:11:"Song name 10";}

我想通过AJAX执行此操作,并将条目添加到wp_postmeta表中。这是我的表格html的一个例子:

<form id="songChoices-12028" method="POST">
        <input type="hidden" id="booking_id" name="booking_id" value="12028">
        <ul class="setlist_container">


            <li class="song_entry ">Ain’t No Sunshine - 
                <em>Bill Withers</em>
                <br>

                    <input type="radio" id="doPlay" name="songChoice-397">
                    <label for="doPlay">Do</label>
                    <input type="radio" id="dontPlay" name="songChoice-397">
                    <label for="dontPlay">Don't</label>
                    <a href="#" id="clear-button">Clear</a>
            </li>

            <li class="song_entry ">No Diggity - 
                <em>Blackstreet</em>
                <br>

                    <input type="radio" id="doPlay" name="songChoice-12036">
                    <label for="doPlay">Do</label>
                    <input type="radio" id="dontPlay" name="songChoice-12036">
                    <label for="dontPlay">Don't</label>
                    <a href="#" id="clear-button">Clear</a>
            </li>

            <li class="song_entry 7-8-piece drinks-reception">Pricetag - 
                <em>Jessie J</em>
                <br>

                    <input type="radio" id="doPlay" name="songChoice-12038">
                    <label for="doPlay">Do</label>
                    <input type="radio" id="dontPlay" name="songChoice-12038">
                    <label for="dontPlay">Don't</label>
                    <a href="#" id="clear-button">Clear</a>
            </li>

            <li class="song_entry 4-5-piece">Rehab - 
                <em>Amy Winehouse</em>
                <br>

                    <input type="radio" id="doPlay" name="songChoice-12039">
                    <label for="doPlay">Do</label>
                    <input type="radio" id="dontPlay" name="songChoice-12039">
                    <label for="dontPlay">Don't</label>
                    <a href="#" id="clear-button">Clear</a>
            </li>

            <li class="song_entry 4-5-piece">Rolling in the Deep - 
                <em>Adele</em>
                <br>

                    <input type="radio" id="doPlay" name="songChoice-12035">
                    <label for="doPlay">Do</label>
                    <input type="radio" id="dontPlay" name="songChoice-12035">
                    <label for="dontPlay">Don't</label>
                    <a href="#" id="clear-button">Clear</a>
            </li>

            <li class="song_entry 4-5-piece">Sittin’ on the Dock of the Bay - 
                <em>Otis Redding</em>
                <br>

                    <input type="radio" id="doPlay" name="songChoice-398">
                    <label for="doPlay">Do</label>
                    <input type="radio" id="dontPlay" name="songChoice-398">
                    <label for="dontPlay">Don't</label>
                    <a href="#" id="clear-button">Clear</a>
            </li>

            <li class="song_entry 4-5-piece">Summer of ’69 - 
                <em>Bryan Adams</em>
                <br>

                    <input type="radio" id="doPlay" name="songChoice-12037">
                    <label for="doPlay">Do</label>
                    <input type="radio" id="dontPlay" name="songChoice-12037">
                    <label for="dontPlay">Don't</label>
                    <a href="#" id="clear-button">Clear</a>
            </li>
                        </ul>
    </form>

然后在我的ajax JS文件中我有:

var response;

// AJAX for song request details
$('form[id^="songChoice-"]').on('change', function(e) {

e.preventDefault();

$.post( update_user_info.ajaxurl, {
      action : 'update_user_setlist',
      nonce : update_user_info.nonce,
      post : $(this).serialize()
  },
  function(response) {
      console.log(response);
      responseSuccess(response);
  });

return false;

});

然后,最重要的是,处理将数据发送到DB的函数:

function updateSetlistInfo() {

    if(empty($_POST) || !isset($_POST)) {
        self::ajaxStatus('error', 'Nothing to update.');
    } else {
        $data = $_POST;

        $dataString = $data['post'];
        parse_str($dataString, $dataArray);

        $nonce = $data['nonce'];

        if(wp_verify_nonce($nonce, 'update_user_info') !== false) {

            $user_ID = get_current_user_id();
            $post_id = $dataArray['booking_id'];
            //$dataArray['user_addressLine1'] = isset($dataArray['user_addressLine1']) ? $dataArray['user_addressLine1'] : false;


            if($user_ID != NULL) {
                foreach($dataArray as $key=>$value) {

                $status = update_post_meta($post_id, $key, $value);

            }
                self::ajaxStatus('success', 'Meta fields updated.', $dataArray);

            } else {
                self::ajaxStatus('error', 'You are unauthorized to perform this action.', $dataArray);
            }
        } else {
            self::ajaxStatus('error', 'Nonce check cannot fail.');
        }
    }
}

以上内容取自我页面上的其他表单,该表单通过ajax将用户地址详细信息提交到数据库。但是,这只是为每个表单项添加一个新行 - 我想要有2行,“do_play”和“dont_play”,并在这些行中构建一系列序列化数据。在这一点上任何建议都会有很大的帮助,因为我有点困难

1 个答案:

答案 0 :(得分:0)

好的,您没有包含保存updateSetlistInfo()方法的完整类。但你提供的东西看起来还不错。

我要调试的第一件事是$dataArray。 它确实包含2(do_play / dont_play)键吗?

添加此项(就在foreach调用之前):

error_log('$dataArray: <pre>'.print_r($dataArray,true).'</pre>');

你能查一下吗?

更新1: 调试后,我们知道dataArray如下所示:

array(
  "status" => "success",
  "message" => "Meta fields updated.",
  "data" => array(
    "booking_id" => "12031"
    "songChoice" => array(
      "2" => "dont_play",
      "7" => "dont_play",
    )
  )
)

你的foreach将每个(顶级)密钥保存为post_meta_key。

像这样:

update_post_meta($post_id, 'status', 'success');
update_post_meta($post_id, 'message', 'Meta fields updated.');
update_post_meta($post_id, 'data', ARRAY_THAT_HOLDS_BOOKINGID_SONGCHOICE);

我希望你了解现在发生了什么。作为开发人员,您必须使用调试日志ALOT。当你想要检查一切是否按预期运行时,它总是你要去的第一件事。

我希望我能指出你正确的方向,你可以在没有我帮助的情况下进一步调试你的应用程序。因为现在它已经超出了stackoverflow的范围,我无法帮助您逐步调试您的应用程序。

WP调试
wp-config.php中,更改:

define( "WP_DEBUG", false );

分为:

define( "WP_DEBUG", true );// just toggle this line to false to turn off
if ( WP_DEBUG ) {
    define( "WP_DEBUG_DISPLAY", false );
    define( "WP_DEBUG_LOG", true );
    @ini_set( "display_errors", 0 );
    define( "SCRIPT_DEBUG", true );
}

然后安装debug log viewer插件。