第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Ajax 調(diào)用 PHP 函數(shù) - 從 Wordpress 中的設(shè)置頁(yè)面保存圖片庫(kù)

Ajax 調(diào)用 PHP 函數(shù) - 從 Wordpress 中的設(shè)置頁(yè)面保存圖片庫(kù)

PHP
三國(guó)紛爭(zhēng) 2023-03-04 17:49:21
有人可以給我一個(gè)很好的例子來說明如何使用 Ajax 調(diào)用 PHP 函數(shù)嗎?我正在嘗試將圖片庫(kù)(具有動(dòng)態(tài)數(shù)量的圖像)保存到管理設(shè)置部分的數(shù)據(jù)庫(kù)中。下面的代碼不完整或者可能有一些錯(cuò)誤。請(qǐng)給我一個(gè)很好的例子或更好的方法:<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貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

在這里看一下我的一個(gè)插件中的一個(gè)有效的 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


}


查看完整回答
反對(duì) 回復(fù) 2023-03-04
  • 1 回答
  • 0 關(guān)注
  • 140 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)