douna2633 2016-02-21 09: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 04: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 pycharm倒入虚拟环境的时候,显示这个,但是我的虚拟环境已经创建了
  • ¥15 FPGA芯片60进制计数器
  • ¥15 前端js怎么实现word的.doc后缀文件在线预览
  • ¥20 macmin m 4连接iPad
  • ¥15 DBIF_REPO_SQL_ERROR
  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部