1

This is my http get code, here console.log(data) is printing whole php code instead of echoed statement. Kindly help me guys i am not able to figure out whats happening. I am making an ionic angular app.

Angular Code

// define angular module/app
var formApp = angular.module('formApp', [])
// create angular controller and pass in $scope and $http
.controller('formController', function ($scope, $http) {
    // create a blank object to hold our form information
    // $scope will allow this to pass between controller and view
    $scope.formData = {};
    // process the form
    $scope.processForm = function() {
        $http({
            method  : 'GET',
            url     : 'process.php',
            data    : $.param($scope.formData),  // pass in data as strings
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
        })
            .success(function(data) {
                console.log(data);
                if (!data.success) {
                    // if not successful, bind errors to error variables
                    $scope.errorName = data.errors.name;
                    $scope.errorSuperhero = data.errors.superheroAlias;
                } else {
                    // if successful, bind success message to message
                    $scope.message = data.message;
                }
            });
    };
});

PHP Code

<?php
$errors         = array();      // array to hold validation errors
$data           = array();      // array to pass back data
// validate the variables ======================================================
if (empty($_POST['name']))
    $errors['name'] = 'Name is required.';
if (empty($_POST['superheroAlias']))
    $errors['superheroAlias'] = 'Superhero alias is required.';
// return a response ===========================================================
// response if there are errors
if ( ! empty($errors)) {
    // if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors']  = $errors;
} else {
    // if there are no errors, return a message
    $data['success'] = true;
    $data['message'] = 'Success!';
}
// return all our data to an AJAX call
echo json_encode($data);

Console Output/Ajax Response

<?php
$errors         = array();      // array to hold validation errors
$data           = array();      // array to pass back data
// validate the variables ======================================================
if (empty($_POST['name']))
    $errors['name'] = 'Name is required.';
if (empty($_POST['superheroAlias']))
    $errors['superheroAlias'] = 'Superhero alias is required.';
// return a response ===========================================================
// response if there are errors
if ( ! empty($errors)) {
    // if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors']  = $errors;
} else {
    // if there are no errors, return a message
    $data['success'] = true;
    $data['message'] = 'Success!';
}
// return all our data to an AJAX call
echo json_encode($data);

As you can see it is sending the whole php code instead of echoed statement.

t.niese
  • 39,256
  • 9
  • 74
  • 101
Anky
  • 33
  • 4
  • 1
    If the php code is send as-is and is not executed/evaluated, then it is clearly a problem on the server side, and not a js, ajax or angularjs problem. – t.niese Jan 14 '15 at 06:57
  • Ionic and angular are client side and unrelated to your problem. Also the content of your `php` file does not matter, as the php code I not evaluated at all. As I already said it is a problem on your server, which either does not have `php` installed or is configured the wrong way. – t.niese Jan 15 '15 at 11:55
  • soo how to configure it ?? As i am very new to php donno much about its configuration nd all – Anky Jan 16 '15 at 06:29
  • Aks the administrator of your server, who is responsible for configuration and support. – t.niese Jan 16 '15 at 06:41
  • I have tried this code on another system also , where there were no php configuration problems but their also the code was not working properly. – Anky Jan 19 '15 at 06:35
  • `also not working properly` is the worst _explanation_ of an error. Anyway if a new question/problem occures while a current one is solved you have to create a new question. – t.niese Jan 19 '15 at 06:50

1 Answers1

0

I know this thread is old, but try to check your .htaccess file, if you have written down a rule like:

Use PHP5.4 as default

AddHandler application/x-httpd-php54 .php

Get rid of it. I made this mistake.