E
E
Evgeny Ivanov2017-12-07 09:53:37
PHP
Evgeny Ivanov, 2017-12-07 09:53:37

How to form url (path) in JS script?

I work with open server.
Part of the autocomplete.php script code

$("#autocomplete_search_box").keyup(function(){
$.ajax({type: "POST",url: "<?php echo $autocomplete['script_path'].'?box_number=1'; ?>",
.............................

The autocomplete.php script is included in many php files.
In files that are in the site folder, in subfolders of the site, etc.
Since the files are at different nesting levels, the url: path in the included autocomplete.php file will be different.
Therefore, I have to do this in every file where autocomplete.php will be used
$autocomplete['script_path']='common/autocomplete/select_data.php'; //url: относительно файла, куда будет включен текст скрипта autocomplete.php
include ($_SERVER['DOCUMENT_ROOT'].'/common/autocomplete/autocomplete.php'); //а вот и включение/инклуд скрипта

This approach creates a lot of confusion. Many files, many directories, different levels of nesting, different includes - it can be difficult to understand which file is called/included in which one.
I want to do it like this
......
$.ajax({type: "POST",url: "<?php $_SERVER['DOCUMENT_ROOT'].'/common/autocomplete/autocomplete.php?box_number=1'; ?>",
.............................

But this gives an absolute path and the script doesn't work.
D:/OpenServer/domains/my_site.ru/common/autocomplete/select_data.php?box_number=1
doesn't really look like a url.
But with this path
url:common/autocomplete/select_data.php?box_number=1
everything works.
How to form url (path) in JS script?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery, 2017-12-07
@it_monk

$.ajax({type: "POST",url: "/common/autocomplete/autocomplete.php?box_number=1",

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question