0

I'm trying to make a form that sends an url of the type /:id/:option where I know the id before entering the form but the option-attribute is the one that the user selects from the radio-buttons. After submitting the form I should move to the page /:id/:option. I'm also using angulasJS with my application.

So how can I send the url with the POST-method?

html

<form method="POST">
    <div class="voteOptions" ng-repeat="item in id.data.options">
      <label class="radioButtons">{{item.title}} votes:{{item.votes}}
        <input type="radio" name="option" value={{item.option}}>
        <span class="radioSelector"></span>
      </label>
    </div>
    <input type="submit" id="voteSubmit" value="Vote!">
  </form>

.post-call

app.post('/:id/:option', (req, res) => {
  var poll = polls[req.params.id-1];
  poll.options[req.params.option-1].votes++;
  res.json(poll);
});
6
  • can you add the code. Commented Feb 28, 2018 at 7:01
  • I don't think it helps, but I added the html. Commented Feb 28, 2018 at 7:14
  • controller code, $http call Commented Feb 28, 2018 at 7:15
  • There's the post-call. I don't have anything (for this part of the code) in the controller yet. Commented Feb 28, 2018 at 7:25
  • I agree It's post-call but where you have mention URL in form. Commented Feb 28, 2018 at 7:26

1 Answer 1

1

Create Controller and make Http call from there.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<form method="POST" ng-submit="submit()">
    <div class="voteOptions" ng-repeat="item in options">
      <label class="radioButtons">{{item.title}} votes:{{item.votes}}
        <input type="radio" name="option" value={{item.option}} ng-model="option.val">
        <span class="radioSelector"></span>
      </label>
    </div>
    <input type="submit" id="voteSubmit" value="Vote!">
  </form>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$http) {
    var id = 12345;
	$scope.option = {val:0};
	$scope.options= [{
    	title : 'option-1',
        votes : 5,
        option : 1
    },{
    	title : 'option-2',
        votes : 5,
        option : 2
    }];
    
    $scope.submit = function(){
    	console.log('Url', `<domain>/${id}/${$scope.option.val}`);
        // Generate url and call http post
         // $http.post(url, body, config)
        //  .success(function (data, status, headers, config) {
        //  })
        //     .error(function (data, status, header, config) {
        //     });
    }
});
</script>

</body>
</html>

Sign up to request clarification or add additional context in comments.

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.