Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is it possible to parse an external php file via XMLHttpResquest? If not, how would I exectute an external php file on a page, or load the script into the document?
You would have to make a CURL request from your PHP to a server which is geared to serve raw PHP code rather than execute it. Normally servers don't print the server-side code so you might have some issue there.
You can then use the eval() function to execute raw PHP code
http://php.net/manual/en/function.eval.php
Also, I would recommend finding an alternative to whatever it is you are trying to achieve because, unless you know exactly what you are doing... it sounds wrong.
According to same origin policy you cant . XMLHttpResquest can only call scripts from same server.
But there is a hook for this. I dont know how exactly you do this in core javascript, but in jQuery you can do something like this:
var aURL="SOME_EXTERNAL_URL";
$.ajax({
url: aURL+"&callback=?",
data: "message="+commentdata,
type: 'POST',
success: function (resp) {
alert(resp);
},
error: function(e) {
alert('Error: '+e);
}
});
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
From a PHP page (http://example.com/test.php), I would like to call another PHP file located on another web site (http://newsite.example.net/version.php) and passing parameters (var1=123, var2=789). Version.php should do something and return true or false, so I can test the result in test.php (if ($result) ...)
Any short exemple how to proceed and what should be the content of the fileversion.php ?
Regards,
David
Executing a php script in such a way is equivalent to performing an http query. As such, the only result you can expect from the url you call is a http response (plain text, image, json - whatever mime type the page can output).
It can be done for example with file_get_contents() (provided your php configuration allows it) :
$contents = file_get_contents('http://somesite.com/somepage.php?someparameter=somevalue');
You could then process the resulting content text however you like. But you won't ever get direct boolean output. numbers (0 or 1) could be suitable replacements for your needs, however.
Edit:
The version.php might look like this :
<?php if($condition) { echo 1; } else { echo 0; }
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Below is a code snippet of my PHP file - I am trying to call a PHP function on click of a button. Simple echo is not working - appreciate any help
/* This function saves Add or Make of Model to Database
*/
$('#addmake-btn').live('click',function(){
<?php
echo "Not Working";
?>
});
</script>
first you need to know about how client side and server side language works.
server side languages like PHP will execute in server, you cant execute them in client side unlike javascript or some other client side language.
$('#addmake-btn').live('click',function(){
var printVal = "<?php echo "Not Working"; ?>";
document.write(printVal);
});
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the problem this code:
$window_width= '<script>screen.availWidth</script>';
$window_height= '<script>screen.availHeight</script>';
any idea?
Nothing wrong in your code, but you can't get any values in PHP variable, because PHP is Server side scripting language.
You need JavaScript, not PHP.
var screenWidth = window.screen.width,
var screenHeight = window.screen.height;
You can then send it to the server via Ajax (with an XmlHttpRequest).
You seem to be misunderstanding the basic concepts of how PHP and HTML/JavaScript works together. PHP is a server-side language and JavaScript is a client-side language.
PHP generates the output first and then it's transferred to the client where it's being executed. You're trying to do it the other way around.
What you need to do is first, generate a page using PHP, have it sent to the client for client-side execution, and from there have JavaScript perform a call that sends the information back to PHP using a GET/POST request.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
This is probably terribly simple. I have done something similar to this when the information was being passed to a different page.
Basically... I have a div with a dynamically generated list of objects.
<li><?= $array['City'] ?></li>
All of the code already exists to load the zip codes for a given city, so in PHP (in this case)
$locations = new ProjectsLocationsArrays();
$zipcodes = $locations->array_zips($array['City']);
At present, everything runs off of query strings... ?city=aaa&zip=123, seems sloppy to me. I would like to use AJAX. Once the cities are loaded, how do I load the zips into an existing div on the page, fired by a click on one of the city anchors.
TIA
If you have jQuery something like this should work
function loadZipCodes(city){
$.ajax({
url: "pagethatgivesarrayofzipcodes.php",
method: "POST",
data : {'city': city}
}).done(function(data) {
$(.zip-div).html(data);
});
}
Where pagethatgivesarrayofzipcodes.php takes a $_POST['city'] containing the city info and returns either HTML or a a Javascript array of ZIP codes.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I've seen sites where instead of form buttons, they use links like that
onclick="USER._like('161', this);"
And when I click on it, it dynamically inserts the data into the database.
What is this called? And are there any tutorials around here?
Thanks.
That is a javascript event handler. Presumablly the function USER.like ( or to be exact the method like on the object USER) makes an ajax call and sends the value 161 to the server which is then somehow recorded.
That said its much better to do it in an unobtrusive manner, meaning that you would not write function call into an html attribute but rather attach an even handler pragmatically from javascript... using assuming this html:
<a class="like-button" href="#" data-id="161">Like</a>
Achieving the same thing in an unobtrusive manner with jquery would look like:
$(function (){
$('.like-button').on('click', function (e) {
e.preventDefault();
USER.like($(this).attr('data-id'), this);
});
});