ajax loading php with facebook-Java not working - javascript

I have some code on a main page (index.php) that calls a php script (access.php) with javascript, as seen below.
The php (access.php) also has javascript but when I load it into the current page (index.php) then the javascript content in (access.php) is not working. Maybe this can't be done. Any thoughts?
The javascript does fire when I load access.php in a browser by itself.
$.post("access.php",
{FullName : response.name,ID: response.id,Email:response.email,UD:userDevice},
function(data)
{
document.getElementById('Container').innerHTML = data;
});
Perhaps I should have stated this before but it's using the Facebook Java SDK. MY END GOAL: I want to SEND VARS to a php that also has more/new Facebook-Java script that I can run from within the index.php.

This method worked. :)
$( "#Container" ).load( "access.php",
{FullName : response.name,ID: response.id,
Email:response.email,UD:userDevice} );

Related

How to get previous defined variables with .load()

index.php
<script>
var example = 1;
$("#example-div").load("external.php");
</script>
<div id="example-div"></div>
external.php
<script>$("example-div").innerHTML("BlaBla " + example);</script>
Hi,
I am currently loading content from another file into my index.php with the load() function of JQuery to load images only then when I want it for example.
Now I am using a database connection which i define in my index.php. When I load the external content now, the code I load doesn't now all the variables I defined previously in the index.php, although it's part of index.php after loading them.
Is there a way to let the loaded content now all the previous stuff?
Thanks y'all!
You want to use session cookie to save data in php and for javascript you want to use cookie then use cookie and session any page
for php
https://www.w3schools.com/php/php_sessions.asp
https://www.w3schools.com/php/php_cookies.asp
for javascript
https://www.w3schools.com/js/js_cookies.asp
Jquery have some callback function executed when the load is finish, you can execute the script in external.php in this callback function, because at the moment your function will be execute, the content is in the DOM and it should work.
var example = 1;
$("#example-div").load("external.php", function() {
$("#example-div").innerHTML("BlaBla " + example);
});
It's a bit strange for me to use a script from the page you loaded to perform some task in the current document.
I don't know what you are trying to do. But your code is working, just small mistakes. Do it as follows -
index.php
<div id="example-div"></div>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
var example = 1;
$("#example-div").load("external.php");
</script>
external.php
<script>$("#example-div").html("BlaBla " + example);</script>
This gives the output
BlaBla 1

Javascript/Jquery execution order issue

I am using external JavaScript to show video player on web page like...
<script src='http://player.field59.com/v3/vp/...........'></script>
Here is my code that cut the HTML code which is generated by above script tag and paste into specific container like <div id='dynamic-video-container'></div>
$(document).ready(function(){
if( $("div.subscriber-hide div.html-content div.bimVideoPlayer").length ) {
var video_content = $('<span>').append($('div.subscriber-hide div.bimVideoPlayer').clone()[0]).remove().html();
}
$('div.subscriber-hide div.html-content').remove();
$("div#dynamic-video-container").html(video_content);
});
I am doing this because I am not able to put external code directly into "<div id='dynamic-video-container'></div>" container.
My issue is this sometimes play functionality of video player is not working may be due to loading sequence of external code and code snippet.
Is there any option by which code snippet will execute when external js becomes fully loaded? Can anyone suggest idea to how to do this?
One more thing external code "<script src='http://player.field59.com/v3/vp/...........'></script>" is added by CMS so I am unable to change this line.
Try this
window.onload = function() {
//your code comes here
}
As window.onload is called after all the content and resources of the page are loaded. Hope this helps.

How can I call a php / wordpress command from JavaScript?

I'm trying to create a lightbox effect for a self-made WordPress Theme. But I'm trying to include a WordPress page in the body through jQuery.
For example, in my js file.
$('.button').Click(function(){
$(body).append('`<div><?php get_template_part('content','thing'); ?></div>`');
});
I've tryed that and then my php file doesn't reproduce the php bit.
Thanks.
try
$('.button').Click(function(){
$(body).append("<div><?php get_template_part('content','thing'); ?></div>");
});
Remember to use this jquery in a php file not in JS file.
it might help you
You can call a ajax functions that returns your php result.
Like (Seems you're using jQuery):
$.get("templatepart.php", function(data) {
$(body).append("<div>"+ data +"</div>");
})
And in your php file ("templatepart.php"):
<?php
// after includes/requires/etc functions
get_template_part('content','thing');
?>

AJAX isn't loading the other js and css style

I wrote a short AJAX code which is getting Facebook contents from a PHP file (in the PHP file I have declared class="gallery-img-link" as <a> and class="img-responsive img-thumbnail" as <img>).
I have the gallery-img-link on the bottom of site as jQuery script, the problem is when I add those classes by hand its working fine, but when I try to load those by a AJAX get request it seems to doesn't work.
Should I use it in a document.ready function?
I have tried live as well, but that didn't helped at all.
$(document).ready(function(){
$(\'#loading-image\').show();
$.ajax({
method:\'get\',
url:\'ajax.php\',
success:function(data){
$("#facebook").html(data);
},
complete: function(){
$(\'#loading-image\').hide();
}
});
});
For all JavaScript DOM manipulations you Need the DOM to be fully loaded.
Without having your PHP and other Code around I can just correct your JS:
$(document).ready(function(){
$('#loading-image').show();
$.ajax({
method: get ,
url: ajax.php ,
success: function(data){
$("#facebook").html(data);
},
complete: function(){
$('#loading-image').hide();
}
});
});
UPDATE:
Go Into the f12 Developer Tools in your Browser, and go to the Network Tab, then have a llok o all loaded Files, on should be ajax.php. Have a look what the File of that COntent is
Basicly the problem was that the content has been loaded but the "photo java script file" wasnt waiting to get the page loaded, so this function couldnt work at all. Becasue those AJAX loaded after "reading JS function".

How to get a JavaScript file after you have loaded the page?

I want to load a AJAX form into my page with it's own validation.js attached to it. How can I do that?
I tried to load every one with its own $.ajax request, then output the HTML on the page successfully, but the validation.js doesn't work on the form.
Can I load a JavaScript file then a HTML file (with ajax) in my existing page, then get the HTML output on the page to respond to this JavaScript functions?
use the following javascript for adding a javascript file in async mode after the page has loaded.
(function() {
var d=document,
h=d.getElementsByTagName('head')[0],
s=d.createElement('script');
s.type='text/javascript';
s.async=true;
s.src='/js/myfile.js';
h.appendChild(s);
}());
if you have a number of files to load this way, you can convert this into a function call.
function asyncLoader(scriptName)
{
var d=document,
h=d.getElementsByTagName('head')[0],
s=d.createElement('script');
s.type='text/javascript';
s.async=true;
s.src = scriptName;
h.appendChild(s);
}
this method does not guarantee/notify when the js file will be loaded. for that you can put some event call in the file which is being loaded.
if you are loading some html from ajax and adding it to a your current html (as innerHtml) you can put the <script> tag inside that html and it will behave same as above code.
see this question and its answers for more details.
EDIT:
following is a JQuery way using $.getScript
$.getScript("path/my_script.js", function(){
console.log( "Load was performed." );
}); //JQuery can also be used instead of $
doing the same using $.ajax
$.ajax({
url: "path/my_script.js",
dataType: "script",
success: function(){
console.log( "Load was performed." );
}
});

Categories