douna2633 2016-02-21 17:17
浏览 48
已采纳

如果所有代码都在一个文件中,如何指定Ajax jquery url路径

I have issue with specifying the Ajax jQuery url path. I have one function contains all Html markup codes, php sned mail codes and Ajax jQuery code as follow

How i can specify url property in ajax code?

Info: i use wordpress theme and this function within my plugin directory

<?php
    function bsncontact() {

        //All php codes here
?>
        <form id="ajax-contact" class="navbar-form" action="" method="POST">
            <!-- other html markup codes -->
        </form>
        <script type="text/javascript">
            jQuery(function(){
            jQuery.ajax({
                type: 'POST',
                url: '',
                data: "name=" + name + "&email=" + email + "&subject=" + subject + "&message=" + message,
                success: function(text) {

                }
           });
           });
       </script>
<?php
}
?>
  • 写回答

3条回答

  • drnrxv9383 2016-02-22 12:50
    关注

    As you say use wordpress theme. In wordpress theme this is very easy you can register ajax within your wordpress in backend(admin panel) and in front end of your theme

    First you may put your ajax code in separate (for example put ajax code within myajax.js file) and then put it within your js folder, and register the your ajax code (you create function and register these scripts within it) and wordpress admin ajax over your theme by using wp_localize_scrip

    function my_enqueue() {
        wp_enqueue_script( 'ajax-script', plugins_url( '/js/myajax.js', __FILE__ ), array('jquery') );
        wp_localize_script( 'ajax-script', 'ajax_object',array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
    }
    add_action( 'admin_enqueue_scripts', 'my_enqueue' );
    

    Second put your HTML form code within separate function for example

    <?php
        function myhtmlfunc() {
            //Your html contact form here
            //not important if form contain action="", you can remove it
        }
    ?>
    

    Until now very easy.

    Third put your php sent mail code within another function and Ajax on the Viewer-Facing Side for bot logged and visitor users abs below AJAX in Plugins

    <?php
        function myphpmail() {
            //Put all php codes here
        }
        add_action( 'wp_ajax_my_action', 'myphpmail' );
        add_action( 'wp_ajax_nopriv_my_action', 'myphpmail' );
    ?>
    

    Fourth then set your ajax url as below

    $.ajax({
        type: 'POST',
        url:ajax_object.ajaxurl,
        success: function() {}
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?