0

I have a php page,say demo.php and i am calling this page using <script src="demo.php"></script>. I need to know whether it is possible to load another page using javascript. I tried to include a javascript function ,but the required page is not loading.Please find me a solution.

10
  • I have a php page,say demo.php and i am calling this page using <script src="demp.php"></script> Commented Jul 26, 2013 at 10:07
  • 1
    Tell us a bit more about what the included page should do. I think you want to use ajax. Commented Jul 26, 2013 at 10:09
  • 2
    why you are calling server side page via client side coding ? Commented Jul 26, 2013 at 10:11
  • if i understand you correctly, you are confusing server side and client side... if you want demo.php always included, just include it when you build your site via a php include, if you need it dynamical, as a result of a user action, you need to make an ajax call to request it from the server. jQuery &co have easy to use ajax functions Commented Jul 26, 2013 at 10:24
  • 1
    @MarinSagovac definitely. But he is trying to load a php file via javascript source. Commented Jul 26, 2013 at 10:40

5 Answers 5

2

write method and save. and in file like filename.js

<head>
<script type="text/javascript" src="filename.js"></script>
<head/>

or use

<script type="text/php" src="demo.php"></script>

or

$(document).ready(function(){
    $('#div').load('demo.php');
});

you can also use ajax. like

$.ajax({
        type: "POST",
        url: "demo.php",
        data: { name: name, email: email }
            ,
            success: function(msg) {
               // alert(msg);
            }
        });

Hope it will help you.

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

2 Comments

That's EXACTLY what he did.
It is very much interesting, how Client side code, like <script> loading such Serverside code like demo.php this opens a lot of potential.
0

If you want to load another page in the current page, use ajax with jQuery (simple and cross-browser).

Create to javascript files (*.js):

  • "jquery.min.js" and copy/ paste [this][2] in.
  • "actions.js"

In actions.js :

jQuery(function($){

  $.ajax({
    url: "demo.php"     
  })
  .done(function(data){
    $("body").append(data) // add the loaded page as html at the body end
  })
  .fail(function(){
    console.log("fail to load demo.php");
  });

});

Comments

0

Skip this answer if you don't want like this, but this is using PHP as example. Someone need this example.

In PHP include using Javascript code:

<?php echo '<script type="text/javascript" src="somejavscript.js" />'

Check is really your javascript code:

Or check it like this:

<head>
<?php 

$javascript_file = "somedir/somejavscript.js";

if (file_exists($javascript_file))
{
    echo '<script type="text/javascript" src="'.$javascript_file.'" />' ?>
}
else
{
    die('file not found: '.$javascript_file);
}
?>
</head>

Note, if die() functions call it will stop script and show message error! Use it only for testing.

3 Comments

This is for redirection. I asked whether is it possible to load another php page using javascript if i am calling demo.php from script source.
please read the question! s/he is trying to call a serverside php script, not js!
I know what he is trying but he can try.
0

According to comments, I'm not sure, what are you looking for. I assume, if you want to load another page, that means you want to load another HTML page, thus it is called redirect:

Try modifying the window.location property from javascript. window.location.href = "demo.php";

Or more precisely: window.top.location.href = "demo.php";

But if you want to dynamically load a script file, than you can use the jQuery load function as mentioned in the comments, or use 3 lines of javascript:

var s = document.createElement("script"); // create an empty script tag
s.src = "demp.php"; // specify source
document.getElementsByTagName("head")[0].appendChild(s); // append it to the head tag

// Optionally you can delete the new script tag from the DOM, because the executed code will remain in the memory:
s.parentNode.removeChild(s);

Hope this helps.

Also, if you want to call PHP functions from javascript( because I read that too in comments ), than look for the PHP library: XAJAX

4 Comments

This is redirect, he needs load JS script.
The question is a bit ambigious to me, so I updated my answer, thanks for noting me.
"ambigious" > "ambigUous"
Sorry, mate, I'm hungarian, but I grew to love this word, since I first saw it in a MySQL error. But thanks for the info. :)
0

If you use jQuery you can just call

$.getScript('other_file.js', function() {
   // script loaded
});

without jQuery you can append script in head

var script = document.createElement('script');
script.setAttribute('src', 'other_file.js');
document.getElementsByTagName('head')[0].appendChild(script);

EDIT: if that file is not javascript inside php then to call it just use normal ajax. Using jQuery it will be:

$.get('script.php', function(result) {
   // do something with output from a script
});

1 Comment

please read the question! s/he is trying to call a serverside php script, not js!

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.