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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配