1

Is there other solution without ajax with I can call from js php file with other js functions? If I call with ajax js functions in php file doesn't work.

<script>$.ajax(
      url: "getSubcategorieFormUpdate.php?id=" + activeSubcategorie + "&sifra_novogradnja=<?php echo $sifra_novogradnja ?>",
       method: "GET",
       dataType: 'text',
       success: function (result) { 
              document.getElementById("advertForm").innerHTML = result;
       }
});</script>
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • press f12 and look for errors in console – Kavin Smk Aug 17 '17 at 07:47
  • I don't have errors. I'm just trying to do what is impossible. Call with ajax php script with js functions. So js functions are not loaded and included. I need other way to call my php script with js because I have js variables that I have to proceed to my called php file. – Mario Petrović Aug 17 '17 at 09:02

2 Answers2

2

Looks like load is the apt function here

$( "#advertForm" ).load( "getSubcategorieFormUpdate.php?id=" + activeSubcategorie + "&sifra_novogradnja=<?php echo $sifra_novogradnja ?>l", function() {
  alert( "Load was performed." );
}); 

instead of making get and setting response as html. Load do it directly.

If you are getting any errors, your url must be wrong. Since it is a get request try hitting the url in the browser directly and see what server returning.

http://api.jquery.com/load/

Load data from the server and place the returned HTML into the matched element.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Still wont load js code from my loaded php file. It loads only php part. Even if I put js code in echo it doesn't even show on Inspect in chrome. – Mario Petrović Aug 17 '17 at 07:57
  • @MarioPetrović see if this helps https://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml – Suresh Atta Aug 17 '17 at 07:58
  • Nothing helps. Still my js code in my php file called by js is not working. My js scripts are working but they are not showing when I call them from js. – Mario Petrović Aug 17 '17 at 08:27
0

You need to load js as regular and add needed header to php. Try code below.

Javascript

 var script = document.createElement("script")
    script.type = "text/javascript";
    script.src = "somephp.php";

    script.onload = function() {
       test();
    };

    document.getElementsByTagName("head")[0].appendChild(script);

PHP

    <?php
     header('Content-type: text/javascript');
    ?>

    function test()
    {
       alert("Hello");
    }
Dimash
  • 581
  • 1
  • 5
  • 13