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

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() {}
});

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.

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.