Im tying to implament a preloader for swf based on this Post, but im getting trouble passing the javascript value to a php variable.
<script type="text/javascript">
var swfJSPreLoaderConfig = {
'assets':['swf/test.swf'],
'assetLoaded': function( asset){
alert(asset);
},}
</script>
The alert in the above code shows only when the swf file test.swf is fully downloaded. the value that im trying to get in php is asset ( this value contain the swf file path, in this example is swf/test.swf. the alert fires always when the swf files is fully dowloaded and it works very good.)
i tried something like this to get it in a php variable. but no lucky.
$filename = $_REQUEST['asset'];
also i tired using ajax but nothing.
<script type="text/javascript">
var swfJSPreLoaderConfig = {
'assets':['swf/test.swf'],
'assetLoaded': function( asset){
$.ajax ({
type: "post",
url: "index.php",
data: { 'asset': asset },
success: function()
{
alert(asset);
}
});
},}
</script>
and then
$filename = $_REQUEST['asset'];
Whats worng?
Try this,
data: { 'asset': asset },
success: function(response)
{
alert(response);
}
instead of,
data: { 'asset': asset },
success: function()
{
alert(asset);
}
});
PHP:
echo $filename = $_REQUEST['asset'];
Change from
url: "index.php"
to
url: "index.php?asset="+asset
Then in PHP, use:
echo $_GET['asset'];
to confirm that you are sending the correct value
Related
<?php
if (defined('STDIN')) {
$path= $argv[1];
} else {
$path = $_GET['path'];
}
if( isset($path) && ($path!==null) ) {
$local='/home/www/site/';
$fullpath = $local . $path;
var_dump(is_dir($fullpath));
}
?>
If I call it from console , it works as as expected: bool(true).
Called via ajax, it fails:
$.ajax({
url: '../inc/is_dir.php',
data: { path: "a/b" },
dataType: 'text',
success: function (data) {
alert(data);
}
});
Since you use a relative path, check the network tab in the devtools where ../inc/is_dir.php is actually pointing to. There you will also be able to see what kind of error you are getting (e.g. 404, 500, etc.)
Consider using an absolute path, e.g. /inc/is_dir.php. You don't want your ajax calls to break if you use the script on a different page.
I know that there is a lot of questions like this out there, but I have been surfing them and other website for like 4 hours trying to figure this out. I am trying to get main.js to post the data via ajax and then the php should echo that data. If it does not work, it will echo "null". It keeps echoing "null" instead of "John". I know that the jquery and main.js links work, I have tested them. Here is main.js:
$(document).ready(function(){
$.post("index.php", { test: "John"} );
});
And here is the php part of index.php:
<?php
$var = "null";
if(isset($_POST['test'])) {
$var = $_POST['test'];
}
echo $var;
?>
I hope you can solve my problem, and thank you in advance.
You are missing the callback function with the response from the server.
$.post( "index.php",{ test: "John"}, function( data ) {
alert(data);
});
Or you can do something like this:
$.post( "index.php",{ test: "John"})
.done(function( data ) {
alert( "Data Loaded: " + data );
});
Please check the documentation Documentation
Give this a shot
jQuery
var available agent = 1;
jQuery.ajax({
type: "POST",
url: "your-url",
data: available_agent,
dataType: 'json',
cache: false,
contentType: false,
processData: false,
success: function(data){
owner = data['round_robin_agent'];
},
error : function () {
alert("error");
}
});
PHP Script
public function round_robin() {
//Do your work
$round_robin_agent = 'rob';
$results_array = array(
'round_robin_agent' => $round_robin_agent
);
return json_encode($results_array);
}
Download HTTP Trace chrome extension, traceback ajax call and share a screenshot.
https://chrome.google.com/webstore/detail/http-trace/idladlllljmbcnfninpljlkaoklggknp
I searched my problem but I cannot find any solution.
I guess it's really simple and I missing something really easy...but I'm stucked.
I have an html page with a php generated list of links, like that:
<?php
$val = "'"my file with spaces.txt"'";
echo 'click me';
?>
Where $val is different each time, depending on a SQL query.
And the file name is showed correctly, since I hover the mouse on the link and I can read on the bottom-left of the browser:
javascript:post_filename('my file with spaces.txt');
Clicking this link will call this javascript function:
<script type="text/javascript">
function post_filename(val){
$.ajax
({
url: 'myurl.php',
data: { filename: val },
type: 'POST'
});
}
</script>
But clicking on it, does nothing! No errors, just nothing happens.
With the dev tools (F12 on Chrome) I see that it's all ok... just the mypage.php doesn't show... is a page with graphics, part of the site!
I'm stucked... please help me!
Thanks so much
Your AJAX call was invalid, data should be either a String, Array or Object. Object is probably what you're after here from the start you've made
<script type="text/javascript">
function post_filename(val){
$.ajax
({
url: 'myurl.php',
data: {
filename: val
},
type: 'POST';
});
}
</script>
Try making data an object:
<script type="text/javascript">
function post_filename(val){
$.ajax
({
url: 'myurl.php',
data: { filename: val },
type: 'POST';
});
}
</script>
I tried your code, but I made some some revisions for it to work ( I just notice you had a semicolon after type, I believe that should be a comma, that the reason why you have unexpected token) my codes works using this:
<?php
$val = "'hello.txt'";
echo 'click me';
?>
on javascript
$('.click').click(function() {
$.ajax
({
url: 'myurl.php',
data: { filename: $(this).attr('file') },
type: 'POST'
})
.done(function (data) {
//check the output from your url
console.log(data);
})
.fail(function (jqXHR, textStatus) {
console.log("Request failed: " + textStatus);
});
});
I have an ajax request like :
$.ajax({
type: "GET",
url: "services/Test.class.php",
data: "call=getTest",
success: function (data) {
alert(data);
},
error: function (response) {
alert("Error getting php file");
}
});
So, in my class ( Test.class.php ), I have a getTest() function and a require_once('OtherPHP'),
When I test this code, I have an error in require once :
No such file or directory
in my alert(data)
how can I fix it?
It seems like you've included a wrong path of OtherPHP file in Test.class.php. Use file_exists() function to make sure that given path really exists before including/requiring in Test.class.php
if (file_exists(`OtherPHP.php`)) {
require_once(`OtherPHP.php`)
} else {
echo "The file `OtherPHP.php` does not exist";
}
You cant able to call the class directly from ajax,
create new php file say test.php
in test.php
include("Test.class.php");
$test = new Test();
$test ->getTest(); //print the getTest outpout here
Javascript:
$.ajax({
type: "GET",
url: "test.php",
.....
User fills input texts, presses the button Submit. The data sends to the server to be stored and result returned back. Fancybox window with result appears. My question is: how to display the result $res1 in the fancybox?
$.ajax({
type:"POST",
url:"index/success",
async:false,
data:{ name:name, password:password},
success:function ()
{
var html='$res1 from the server should be here instead of this string';//var html=$res1.
$.fancybox(
{
content: html,//fancybox with content will be displayed on success resul of ajax
padding:15,
}
);
}
});
=========================
OK, still doesn't work (returns in the fancybox the whole page+ the word "hello" on top instead of the message "hello"). Below is my update regarding the answers below, that doesn't work properly:
PHP:
<?php
$res1="hello";... // handle logic here
echo $res1; // print $res1 value. I want to display "hello" in the fancybox.
?>
AJAX
$.ajax({
type: "POST",
url: "index/success",
async: false,
data: {
name: name,
password: password
},
success: function (html) {
$.fancybox(
{
content: html,//returns hello+page in the fancybox
//if I use the string below instead of the upper one, the fancybox shows "The requested content cannot be loaded. Please try again later."
// content: console.log(html)
padding:15,
}
});
=============
New update:
Fixed!!! The problem was the data ( "hello" in the example above) was sent to the template in the framework, and template was displayed.
That's why.
Fixed.
Thank you.
Everybody.
Assuming you're using PHP:
PHP:
<?php
... // handle logic here
echo $res1; // print $res1 value
?>
AJAX:
$.ajax({
type: "POST",
url: "index/success",
async: false,
data: {
name: name,
password: password
},
success: function (html) {
// given that you print $res1 in the backend file,
// html will contain $res1, so use var html to handle
// your fancybox operation
console.log(html);
}
});
Enjoy and good luck!
$.ajax({
type:"POST",
url:"index/success",
async:false,
data:{ name:name, password:password},
success:function(html){ // <------- you need an argument for this function
// html will contain all data returned from your backend.
console.log(html);
}
});