亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Ajax 調用 PHP 函數 - 從 Wordpress 中的設置頁面保存圖片庫

Ajax 調用 PHP 函數 - 從 Wordpress 中的設置頁面保存圖片庫

PHP
三國紛爭 2023-03-04 17:49:21
有人可以給我一個很好的例子來說明如何使用 Ajax 調用 PHP 函數嗎?我正在嘗試將圖片庫(具有動態數量的圖像)保存到管理設置部分的數據庫中。下面的代碼不完整或者可能有一些錯誤。請給我一個很好的例子或更好的方法:<button type="button" class="btn btn-primary" onclick="jsAddSettingsFields()">Save Changes</button>function jsAddSettingsFields(){    var elements = document.getElementsByTagName("img");    var urlsdata = new Array();    for (var i = 0, element; element = elements[i++];) {        if (elements[i].id == "idGalPic") {            urlsdata[i] = elements[i].src;             // now we have all image urls in array, now need to             // call add_settings_fields            var data = {                action: 'php_addsettingsfields',                p_urls: urlsdata //I think i need to JSON this            };            jQuery.post( "", function( data ) );        }    }}//end of js function<?php    add_action( 'wp_ajax_update_options', 'php_addsettingsfields_callback' );    add_action( 'wp_ajax_nopriv_update_options', 'php_addsettingsfields_callback' );    function php_addsettingsfields_callback() {        $pic_urls = $_POST['p_urls'];  // De-JSON-fy this variable        foreach ($pic_urls as &$url) {            add_settings_field($url);        }        add_settings_field(            'pic_url_id', // ID - slug name of field            '', // Title             array( $this, 'pic_gal_callback' ), // Callback            'TestPlugin', // Page            'setting_section_id' // Section ID - slug name of page                 );           public function pic_gal_callback()  {            <input type="hidden" id="idPic" name="idPic" value=$url />        }        // after all hidden fields(with gallery pic urls) are added to setting section submit         // the page and save the gallery urls to database
查看完整描述

1 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

在這里看一下我的一個插件中的一個有效的 ajax 示例。


Javascript 部分:


//setting click event on button click to fire a function

$('#YourButtonIdHere').click(function () {

    YourFunctionNameHere();

});


//function to execute

function YourFunctionNameHere() {

    //formdata variable consists of

    //action: this is ajax action name for WordPress which we define in PHP with a callback function with same name. See in PHP code part.

    //$('#YourFormIDHere').serialize();    //this gets content from form and serialize it. 

    var frm_data = "action=your_action_name_here&" + $('#YourFormIDHere').serialize();

    $.ajax({

        type: "POST",

        url: ajaxurl, // since WordPress version 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php

        data: frm_data,

        cache: false,

        success: function(data, textStatus, jqXHR) {

            //do stuff here in case of success

        },

        error: function(jqXHR, textStatus, errorThrown) {

            //do stuff here in case of error

        }

    });

}

PHP部分:


 //here wp_ajax is the required prefix for your custom actions

 //first parameter is action name with wp_ajax prefix

 //second parameter is callback function to execute with same name as your action

 //for example if your action name is wp_ajax_save_settings then your callback will be save_settings

add_action( 'wp_ajax_your_action_name_here', 'your_action_name_here' );


function your_action_name_here() {

    global $wpdb; // this is how you get access to the database


    //do stuff here and echo response

    echo "ajax call success";

    wp_die(); // this is required to terminate immediately and return a proper response


}


查看完整回答
反對 回復 2023-03-04
  • 1 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號