alert showing php code instead of returned value

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
brycekmartin
Forum Newbie
Posts: 6
Joined: Wed Jul 22, 2009 12:57 pm

alert showing php code instead of returned value

Post by brycekmartin »

Hello gracious people of the forum. I'm trying to get a proof of concept down so that I can expand on it and create some really great functionality in some web apps using PHP, JSON, and JQUERY. I have two small pieces of code that send a JSON string to a php script which receives it and then returns a plain text string. Later I want to return JSON from PHP but that is for another exercise.

The problem I have is that my alert box is coming up and displaying my php script instead of the echo'd variable results.

HTML/Javascript

Code: Select all

 
<html>
<head>
<title> json and php test</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript">
$(function(){
    var data = {"variable1":"data1"};
    $.post("getJSON.php",data,function(rdata){alert(rdata);},"text");
});
</script>
</head>
<body>
</body>
</html>
 

PHP Script

Code: Select all

 
<?php
 $res = json_decode($_POST['variable1'], true);
header("Content-type: text/plain");
 $ret = "done";
 echo $ret;
 
?>
 
Any Ideas?

Thanks
Bryce
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: alert showing php code instead of returned value

Post by kaszu »

Looks like you haven't installed php?

Code: Select all

$res = json_decode($_POST['variable1'], true);
This is useless, because $_POST['variable1'] content is "data1", which is not valid json. See documentation.
Last edited by kaszu on Wed Jul 22, 2009 1:34 pm, edited 2 times in total.
brycekmartin
Forum Newbie
Posts: 6
Joined: Wed Jul 22, 2009 12:57 pm

Re: alert showing php code instead of returned value

Post by brycekmartin »

Yep. I've got a XAMPP stack running. when I type 127.0.0.1 in the browser I get the test page, so its working.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: alert showing php code instead of returned value

Post by kaszu »

If you open getJSON.php page in your browser, what do you get?
brycekmartin
Forum Newbie
Posts: 6
Joined: Wed Jul 22, 2009 12:57 pm

Re: alert showing php code instead of returned value

Post by brycekmartin »

A blank page.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: alert showing php code instead of returned value

Post by kaszu »

Use "View -> Page Source", there you should see you php code <- if you see it, then PHP is not installed (configured)
brycekmartin
Forum Newbie
Posts: 6
Joined: Wed Jul 22, 2009 12:57 pm

Re: alert showing php code instead of returned value

Post by brycekmartin »

well I must have a configuration issue... Any ideas on where to start to fix it?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: alert showing php code instead of returned value

Post by kaszu »

You should create a new topic for that. Here on devnet is a forum called "Installation and Configuration"
brycekmartin
Forum Newbie
Posts: 6
Joined: Wed Jul 22, 2009 12:57 pm

Re: alert showing php code instead of returned value

Post by brycekmartin »

Thanks, will do.
brycekmartin
Forum Newbie
Posts: 6
Joined: Wed Jul 22, 2009 12:57 pm

Re: alert showing php code instead of returned value

Post by brycekmartin »

I was putting my stuff in the wrong htdocs folder. xampp has its own, but apache is pointing to its default one. PHP is installed.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: alert showing php code instead of returned value

Post by JAB Creations »

XAMPP is great at least for local WAMP development (I wouldn't use it live heh). In the Apache configuration file the htdocs directory is declared twice. If you declare two different paths it'll won't use the one you want. :wink: Also be sure to make a copy of a working configuration file! If you ever screw it up use a program like WinMerge to compare the working and broken copies. :)
Post Reply