1

I have some inline jQuery mixed with PHP and i want to do something like this:

$("#someDiv").html("<?php include(page.php); ?>")

The problem is that ill get "Uncaught SyntaxError: Unexpected token ILLEGAL" errors due to new line characters and i think also double quote characters inside page.php.

My question is how can i go about doing an include() into a html() without worrying about illegal characters?

Thanls

7
  • Can't you just include inside #someDiv? Something like <div id="someDiv"><?php include (page.php);?></div>. Commented May 20, 2015 at 1:30
  • problem is that this code is in a partial view and someDiv is located in another partial view Commented May 20, 2015 at 1:31
  • Have you tried using AJAX to call the page? Commented May 20, 2015 at 1:32
  • I can do that, but im trying a way without having to make the extra AJAX call Commented May 20, 2015 at 1:33
  • 1
    Is there a reason for this? I don't think using include() inside javascript/jquery code like this is a good idea really. Commented May 20, 2015 at 1:36

1 Answer 1

2

The best way to do this is using AJAX. jQuery has something implemented called $.get, as well as $.post (which you can use for post data). Since you just want to include, we can use $.get which is 2 lines of code.

$.get("page.php", function(data){
    $("#someDiv").html(data);
})

Define the page in the first parameter, then a callback function as the second parameter. The variable inside the callback function will be the data that is returned. Then inside the callback function, simply set the html of the desired div, and if you want to do some kind of success message with alert() too, that's always an option.

I hope this helps; you avoid the long winded AJAX method as well as the insecurity of include()-ing inside javascript.

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

1 Comment

This will suffice. Thank you

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.