0

I came across this earlier thread while searching for help on this issue, but there are no responses so I'm at a loss.

I am trying to submit a form and receive a php variable in return. I have no problems running the test code on its own, but when put into an angular app with routing enabled, I get no result. Clicking submit while on the routed page leads to nothing happening. Is there anything I can do to fix this?

Here is my code:

HTML + PHP

<form method="post">
    <input type="submit" value="Submit" name="submit">
</form>

<?php if (isset($_POST['submit'])) {
    echo "IF GOOD :)";
} else {
    echo "ELSE BAD :(";
}?>

HTML

<div ng-app="app">
    <div ng-view></div>
</div>

JS

var app = angular.module('app', ['ngRoute']);

app.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'pages/begin.html',
            controller: 'mainController',
            activePage: 'begin'
        });
});
5
  • Why you need to do that using php ?? you can do that thing using angular as your using it . use php only for data insert update delete. else handle everything using angular Commented Apr 5, 2015 at 5:16
  • I'm using php to make a call to a zillow api and return information on houses. Sorry that it wasn't clear in this, I just wanted to make the test work first before I moved on to anything else. Commented Apr 5, 2015 at 5:21
  • You can do that using angular too Commented Apr 5, 2015 at 5:23
  • Could you give me an example of how to use angular to access the zillow api? Everything I've read mentions needing php or another server-side language to do it. If I could do it in angular alone that would save me so much hassle. Commented Apr 5, 2015 at 5:29
  • Give me the path of your api Commented Apr 5, 2015 at 5:30

2 Answers 2

1

Use php only for data insert/update/delete. else handle everything using angular

Try like this

bengin.html

<form >
    <input type="submit" value="Submit" name="submit" ng-click="submit()">
</form>

mainController

$scope.submit=function(){
  $http.post('msg.php', {submit:'hello word!'}).
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  });
}

create file name msg.php

msg.php

<?php if (isset($_POST['submit'])) {
    echo "IF GOOD :)";
} else {
    echo "ELSE BAD :(";
}?>
Sign up to request clarification or add additional context in comments.

Comments

0

You have to supply <form> with action="" attribute to make your code work.

From the manual:

Since the role of forms in client-side Angular applications is different than in classical roundtrip apps, it is desirable for the browser not to translate the form submission into a full page reload that sends the data to the server. Instead some javascript logic should be triggered to handle the form submission in an application-specific way.

For this reason, Angular prevents the default action (form submission to the server) unless the element has an action attribute specified.

However, Angular encourages you to use ngSubmit and ajax requests that cause no page reloads.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.