douna2633
2016-02-21 17:17
浏览 42

如果所有代码都在一个文件中,如何指定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
}
?>

图片转代码服务由CSDN问答提供 功能建议

我在指定Ajax jQuery url路径时遇到问题。 我有一个函数包含所有Html标记代码,php sned邮件代码和Ajax jQuery代码如下

如何在ajax代码中指定 url 属性?

信息:我在我的插件目录中使用wordpress主题和此功能

 &lt;?php 
 function  bsncontact(){
 
 //这里的所有php代码
?&gt; 
&lt; form id =“ajax-contact”class =“navbar-form”action =“”method =“POST”&gt; \  n&lt;! - 其他html标记代码 - &gt; 
&lt; / form&gt; 
&lt; script type =“text / javascript”&gt; 
 jQuery(function(){
 jQuery.ajax({  
类型:'POST',
 url:'',
 data:“name =”+ name +“&amp; email =”+ email +“&amp; subject =”+ subject +“&amp; message =”  +消息,
成功:函数(文本){
 
} 
}); 
}); 
&lt; / script&gt; 
&lt;?php 
} 
?&gt; 
 <  / code>  
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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() {}
    });
    
    已采纳该答案
    打赏 评论
  • douzong6649 2016-02-21 17:31

    Not really sure what you want to achieve,

    Try using "<?php echo $_SERVER['PHP_SELF']; ?>" in the url of your ajax code.

    PHP_SELF is a variable that returns the current script being executed.

    打赏 评论
  • dongqian5384 2016-02-22 12:36

    In wordpress you have to create hook firstly.

    To create hook we use add_action( 'wp_ajax_my_action', 'my_action_callback' );

    my_action_callback is a function that will called when "wp_ajax_my_action" action is called.

    I have updated your code Try this:

    <?php
            add_action( 'wp_bsncontact', 'bsncontact' );
            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: 'http://abcd.com/wp-admin/admin-ajax.php',
                        data: "action=wp_bsncontact&name=" + name + "&email=" + email + "&subject=" + subject + "&message=" + message,
                        success: function(text) {
    
                        }
                   });
                   });
               </script>
        <?php
        }
        ?>
    

    To call hook you need to pass "action" in data of your ajax as shown in above code.

    Please modify this code as per your requirement.

    Hope it will work for you.

    打赏 评论

相关推荐 更多相似问题