AJAX PHP echoes js code, which doesn't work [duplicate] - php

This question already has answers here:
Executing <script> injected by innerHTML after AJAX call
(12 answers)
Closed 9 years ago.
I'm writing an AJAX page with php, which will echo a block of JS code when it finishes.
Although the JS code is embedded into HTML page, it doesn't work. How can I make it work?

Browsers generally don't execute js in ajax sections, for security reasons.
You'll want to provide the final javascript to be executed in a callback to the ajax load functionality instead.
Even better, just include the javascript from the target page in an external initialization function (e.g. function finalizeProfilePage()) in some siteName.js file and then load that upon completion of the ajax load and where-ever else you use that page content.

I'm right now working on something similar.
To execute the code loaded through ajax i just need to call the function like this:
jQuery(function() {
loadScript();
});
(yes i'm using jquery )

Related

jQuery: add PHP with jQuery [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I have simple jQuery code where Im trying to like in PHP echo in jQuery to my website. But when I do it like this:
$( ".doc-list" ).html( "include 'documents.php';" );
My code echo it on site like:
include 'documents.php';
So my problem is when I make code like upwards jQuery echo it like plain text not like PHP and I need to include to my index.php documents.php when AJAX is done.
To load data from server use .load():
$( ".doc-list" ).load( "documents.php" );
Also, learn about client side and server side differences. Javascript (client-side) does not know anything about php code or any other code on your server side. Helpful question is here.

jQuery AJAX vs PHP in HTML attribute

If I want to do some PHP on an event(e.g. onchange) should I use jQuery ajax like:
$("#elm").on("change", function(){
//ajax code
}
, should I use the PHP in the HTML attribute like:
<element onchange="<?php //stuff to do ?>"></element>
You seem to be conflating two different issues.
JS bound events vs intrinsic event attributes.
Bind your event handlers with JS.
Follow the principles of Progressive Enhancement and Unobtrusive JavaScript.
Ajax vs Putting PHP in a JS function
If you put PHP in a JS function then it will run when the PHP outputs the JS function to the browser, not when the JS function is called.
If you want to run PHP in response to an event, then you have to make an HTTP request to the server to run the PHP.
If you want to insert content from the load of page and leave it static, you should use only PHP.
If you want to insert content dynamically (changing with users interactions) you should use AJAX.
I can't found out what are you trying to achieve with your example, so not very sure what you should do there.
taking your code it would give this :
$("#elm").on("change", function(){
//ajax code
$.get('url', {data:'tosend'}, function(data){
// here you have the response of the php script in the data object
// it can be json for exemple
});
}
You must realise two things, your php code will be render when the page is loaded in the
browser so the second code you gave us
means that your "onchange" event is already present in your page.
If you want to request something (data, html, etc) to server from a loaded page, then do an ajax.
In that case below code is correct.
$("#elm").on("change", function(){
//ajax code
}
You cannot execute a piece of php code from client side. But you can assign values from php to javascript and then do operations on client side.

Dynamically passing PHP variables [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I'm trying to accomplish a JavaScript form that will load the result from a PHP request on an external website, after a text box is filled out.
Right now, I have
<script type="text/javascript"/>
function sendrequest() {
document.location="myphpsite.com/submit.php?tb="+document.getElementById("tb").value;
}
</script>
<form>
<input type="text" id="tb" onChange="sendrequest()" />
</form>
This code is going nowhere, because from my current knowledge, you can only load PHP variables on startup, by using "script src=... ", and by having the php page return some javascript code.
What I'm looking for is a way to request/receive php variables dynamically, something that would work like this:
<script type="text/javascript">
function getdata() {
return document.get("myphpsite.com/submit.php?tb=hi");
}
</script>
In other words, is there a function that dynamically "reads" a website, and returns the raw text?
http://w3schools.com/ajax/default.asp
You should go read this and then read it again, AJAX is what you need.
best example with PHP and AJAX -
http://www.w3schools.com/php/php_ajax_database.asp

Variables from Javascript to PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
I have a javascript function I would like to run on a page, that when executed will send a variable to a PHP script but not open the PHP page just stay on the current page.
What I am trying to do is add this function to pages on a website and for it to return information about the page it was run on, such as URL and user IP.
Everything I have tried so far has not done the job, so any info is appreciated.
Many thanks
Thanks Quentin, but I have tried to javascript forms, but they all open the url the form is being posted to. My most recent example:
var form = new Element('form',
{method: 'post', action: 'http://www.site.com/data.php'});
form.insert(new Element('input',
{name: 'u', value: ""+u, type: 'hidden'}));
$(document.body).insert(form);
form.submit();
I also tried:
document.location.href='http://www.site.com/data.php?u='+u;
but again this open the page :S
For those that have suggested AJAX, thank you but it wont work because that needs a script link in the header and I need to be able to plug this into a page without editing the header. I did try to dynamically insert it via the javascript function, it went in OK but none of the AJAX functions would execute.
What you are looking for is called AJAX (Asynchronous Javascript and XML). These type of calls in Javascript are made trivial with things like jQuery.
This is what jQuery's website says about AJAX: The jQuery library has a full suite of AJAX capabilities. The functions and methods therein allow us to load data from the server without a browser page refresh.
And an example jQuery POST:
$.post('ajax/test.html', function(data) {
$('.result').html(data);
});
Using jQuery
for simplicity do something like this
$.ajax('foo.php',function(d){ alert(d); });

about combine ajax ,php and javascript

I have a main html file and I am using ajax to call another php file in same directory.
Inside that php file, I call some external Javascript functions. But my javascripts functions are not working. Is it not possible?
I see generated source in my web browser and it is normal. If i call those functions with using php file (without using ajax), then my functions are working and the generated source same as the previous case. Please help me.
In my html file I am using ajax as follows:
xmlhttp.open("GET","end_location_drop_down.php?q="+str,true);
xmlhttp.send();
in my php file functions as follow,
<?php
echo '<script type="text/javascript" src="../js/pointing.js"></script>'; //external script
$btn8="'btn8'";
$q=$_GET["q"];
echo '<script type="text/javascript">
nextpoint('.$at_id.'); //$at_id mean a variable,nextpoint() is my java script function
</script>';
?>
in my JavaScript function have some image swapping functions.they can call by nextpoint(). But it didn't work.
Javascript functions are executed only when you load the page directly in a browser, as your browser is the one who interprets and executes the javascript code. However when the javascript code is in a different page and that page is being called through AJAX, there is no browser to execute the javascript code before the ajax response is sent back to you. SO it won't work.
Javascript must be executed in browser, so you must insert that script in DOM to current document. Try to insert result of ajax call to DOM.

Categories