This is really simple code I have, it works on my site (if link to php is just "/phpcode.php", but fails to retrieve data if I put the javascript request on the other site and call full link.
I am using godaddy.
I am beginner and have no freaking idea where to look for solution or troubleshoot it.
This is PHP side (full code of phpcode.php):
<?php
$val = 'apple';
echo json_encode($val);
?>
And here is javascript request:
$.post(
"http://website.com/phpcode.php",function(data){
alert(data);
},
"json");
And here is full code of the javascript page
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post(
"http://website.com/phpcode.php",function(data){
alert(data);
},
"json");
});
});
</script>
</head>
<body>
<button>TEST POST</button>
</body>
</html>
Thanks for your help.
This could be something causing the error. Add the line
header("Access-Control-Allow-Origin: *");
after <?php in phpcode.php, this would make your code:
<?php
header("Access-Control-Allow-Origin: *");
$val = 'apple';
echo json_encode($val);
?>
In future, you'll be more likely to get a better answer from your question if you include any error messages / warnings that can be seen. You can often see these in the javascript console. [ctrl] + [shift] + [i] in chrome and then go to the console tab.
ok guys, it is solved.
1st issue was that php code was put into http: site and calls were made from https: sites.
2nd - the particular site could not do alert(data), but response was there. Thank you #stefan for reminding to check responses. Solved it with an extra variable.
Thank you for your help!
Related
I have a site https://one.com/go.php that (for example) contains :
session_start();
$_SESSION['id_ad'] = 123456;
header('Location: https://two.com/index.php');
and on https://two.com/index.php user click and go to https://three.com/index.php that contains :
<script type="text/javascript" src="https://one.com/2.php"></script>
and finally 2.php is:
<?php
session_start();
header("Content-Type: application/javascript");
?>
alert("<?=$_SESSION['id_ad']?>");
can i access $_SESSION['id_ad'] in 2.php ?
I tested only on firefox works.
////////EDIT//////
thank you to #Dimash to mention my mistake, but the main question still exists
It works, you just return javascript wrongly. Firefox for some reason works, but other browsers fails.
Here is working code:
<?php
session_start();
header("Content-Type: application/javascript");
?>
alert("<?=$_SESSION['id_ad']?>");
Please note, that code was tested.
expect javascript file, not php with tag, you need return reall js file, so you need add header content type, telling that it is js file + remove script tag, as it is not html but regular js file.
I am converting my webapp from PHP / HTML to JS/HTML so that it can work with phonegap. I am keeping the php server side files separate and on the server. I am having issues with executing AJAX calls from my local HTML file. When i check the console on chrome, it doesn't show any errors, but the ajax call doesn't return any value either. Ive simplified the issue so that people can easily understand what the problem is.
My JS code is
<head>
</head>
<body>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
$.ajax({
url: "http://www.betsmart.org/betsmart/models/test.php",
method: "get",
dataType: "json",
crossDomain: true,
success: function(result){
console.log(result);
$("#text").html(result);
}
});
</script>
<h1 id="text"></h1>
</body>
My PHP code in test.php is
<?php
header("Access-Control-Allow-Origin: *");
echo "test";
?>
The success callback isn't executing. I understand the same origin policy will probably come into play here but i thought that doesn't matter with Phonegap.
Please do let me know where i'm going wrong or an alternate way to do this. If i have to use jsonp, what will be the client side and server side code?
Thanks
Gagan
So basically i want to html() a php file but i seem to fail, maybe it isn't possible but i want to make sure.
This is an example code (not the real one):
page.php:
<? //some code generating html ?>
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<? include('php/content.php'); ?>");
</script>
php/content.php:
<div class='realContent'>Awesome Stuff Here</div>
Note: I know there is .load(), $.post and many others. I just want to know if this practic is possible, .html()-ing a php file's contents.
Thank you in advance, awesome community!
d3nm4k.
The code in my example does what you want.
Maybe you got the problem with the short opening tags that is < ? ? >.
The file I'm trying to load in my example is located in app/View/Tests/def.php and works just fine.
Hope it helped you!
<div class='content'></div>
<?php
$fileLocation = APP . 'View/Tests/def.php';
// Ensure the file exists
echo "FILE EXISTS: <b>" . (file_exists($fileLocation) ? "TRUE" : "FALSE") . "</b><br /><br />";
?>
<?php
// Alternatively, you can try this one
//include $fileLocation;
?>
<script type='text/javascript'>
$('.content').html("<?php include($fileLocation); ?>");
</script>
You can do it, the code you gave i tried it just by adding ?php
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<?php include('php/content.php'); ?>");
</script>
When the php is executed on server it replaces content with actual html inside html() function. And on client side javascript plays its role to render it.
I'm working on figuring how to make this code work but just can't seem to crack it.
<?php
header("Location: http://domain-2.com?subid=<?php echo $_GET["subid"]; ?>" /><?php
die();
?>
I understand that it has something to do with the php echo inside the php, is there a way to fix this?
If none, are the below mentioned worthy contenders or is there something else that I haven't seen that would be the best choice.
The purpose of this is to redirect users to a different page.
The final URL would look like this:
http://domain-1.com/index.php?subid=subid
What this will do is that it will get the sub-id from the final URL and show stats on a tracker app.
By doing so, there will no longer be a need to change the sub-ids within the .php file.
I can simply add what I want to track outside the .php file & simply FTP just one file to any domain.
I know for sure this code works & is ideally what I'm looking for but,
<html>
<head>
<meta http-equiv="refresh" content="0;url=http://example.com?subid=<?php echo $_GET["subid"]; ?>" />
</head>
</html>
I researched that meta refresh doesn't work on all browsers, which is not good, & that this code:
<html>
<head>
<script>setTimeout('top.location = \'http://example.com?subid=<?php echo $_GET["subid"]; ?>\'', 0000);</script>
</head>
</html>
Is one that I'm not sure will provide a great alternative "open for suggestions though",
it does work the way it's intended to be, but I'm not totally convinced this would be the next best option.
Much appreciated,
J.c.
You have syntax error correct code will be
<?php
header("Location: http://domain-2.com?subid=".$_GET["subid"]);
die();
?>
It should have to be like this
<?php
header("Location: http://domain-2.com?subid=".$_GET["subid"]." ");
die();
?>
I have a website that I'm sending traffic to but then geo-redirecting with the Maxmind javascript code.
When the user first arrives at the page there are tracking variables appended to the url... &c1=, &c2=, &c3=, etc...
Can someone help me grab the tracking info from the url and then php echo it in the redirect? Not sure exactly how this is done and I would appreciate the help. Here's what I have so far:
<script src="http://j.maxmind.com/app/geoip.js" charset="ISO-8859-1"
type="text/javascript"></script>
<script language="JavaScript">
var country= geoip_country_code();
if(country == "US")
{
<!--
window.location = "http://example.com/redirect/usa/index.htm?&c1=PHP ECHO HERE&c2=AND HERE
//-->
}
else
{
<!--
location.href
//-->
}
</script>
Pretty close, just need to open php
window.location = "http://example.com/redirect/usa/index.htm?&c1=<?php echo $_GET['c1']; ?>&c2=<?php echo $_GET['c2'];?>
You can also you the short hand for php echo <?= ?>
to get the variable from the url use the super global $_GET so one of the variable might be $_GET['c1'] check the manual for more information
If it's param in URL you can see :
$_SERVER['QUERY_STRING']
parse_url() can also be useful