1

I have a PHP config document with these line

config.php:

define("BASE_URL","http://192.168.1.1");

But I'm need these in JS/JQuery. How can i do this?

Maybe I will have to create a JS config file too?

3 Answers 3

6

At the top of your document, you can use :

<script>
    var baseUrl = "<?php echo BASE_URL; ?>";
</script>

Then you can use baseUrl variable in your JS code.

Sign up to request clarification or add additional context in comments.

Comments

0

You should require the config file first, Echo the PHP value.

<?php
 require_once('pathToconfig/config.php')
?>

 <script>
   let baseUrl = "<?php echo BASE_URL; ?>";
 </script>

Comments

0

Once you have the baseurl variable in your html files, you can then use it for other loading your js scripts and ajax request. Like so:

<script>
    var base_url = "<?php echo BASE_URL;?>";
</script>

//load css
<link rel="stylesheet" type="text/css" href="<?php echo BASE_URL;?>css/bootstrap.min.css">

//load jquery 
<script type="text/javascript" src="<?php echo BASE_URL;?>js/jquery.min.js"></script>

//use js variable base_url for ajax request too
 $.ajax({
      url:base_url+"your-url-here",
      type: "POST",
      data: params,
      dataType: "json"
  })
  .done(function(res){
      //do something here

  })
  .fail(function(xhr, textStatus){
      console.log("Got error.");
      return;
 })          

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.