I am using AngularJS. I would like to send multiple data in a HTTP post through AngularJS.I am using laravel php as backend and tinymce as my html text editor.The problem is that tinymce comes up with a model and i am unable to figure it out on how to pass data that is dealing with multiple models.
Here is my partial:
<div ng-controller="controller">
<div class="row">
<div class="container">
<div class="col-lg-8">
<form role="form" method="post" >
<div class="form-group" >
<input type="text" class="form-control" name="" ng-model="post.title" placeholder="Type the title here">
</div>
<div action="" class="form-group" ng-controller="TinyMceController">
<textarea ui-tinymce="tinymceOptions" ng-model="tinymceModel" style="height:300px">
</textarea>
</div>
<div class="form-group">
<input type="text" class="form-control" id="" ng-model="post.tags" placeholder="Atleast one related tag">
</div>
<button type="submit" class="btn btn-primary" ng-click="postsave()">Submit</button>
<button class="btn btn-default">Discard</button>
</form>
</div>
<div class="col-lg-4">
RULES:
</div>
</div>
</div>
my controller:
app.controller('controller',['$scope','$rootScope','$http',function($scope,$rootScope,$http){
$scope.post={
title:"",
};
$scope.postsave= function(){
$http({
method:"POST",
url:'http://localhost/../../...',
data: //I can pass title by using $scope.title but i aslo
//want to pass $scope.tinymcemodel
})
.success(function(result){
alert(result);
})
}
Any help would be great.Also suggest me if there is a better way of doing it.
Thank you.
Just like your post.tags is structured, you should do the same. Put all your children under a parent and submit the parent. Let's look at that:
$scope.formData = {}; //parent
Then your textarea/title should be altered accordingly:
ng-model="formData.title" //child
ng-model="formData.tinymcemodel" //child
And then when you pass your data over, you can just pass this.formData.
$scope.postsave= function(){
$http({
method:"POST",
url:'http://localhost/../../...',
data: this.formData
})
.success(function(result){
alert(result);
})
};
This will properly send over the object to the server, which will have the key:value pairs as your title and tinymcemodel. Note that post.tags won't be included in this submission given that it belongs to another parent.
Related
I am pretty new in angular.
I'm working on an internal tool where I can get the user details like location, work profile and mobile number etc.
So i have a form with location, work profile dropdowns and mobile number input field. Here for the above fields im already getting data from database through ajax get request. If i want to change data like mobileNumber, how can i do that and how to save that to database which should override the before mobile number. thanks in advance.
here is my html code:
<form name="userForm">
<div class="page-heading fs-xs-1-5">
Update Profile
</div>
<div class= "second-row">
<div class="row">
<div class="col-lg-4">
<label for="Location"> Location</label>
<select class="form-control" placeholder="location" ng-model="user.location" required>
<option value="">Select Location</option>
<option>Hyderabad</option>
<option>Ahmadabad</option>
<option>New Jersey</option>
<option>California</option>
<option>Jaipur</option>
</select>
</div>
<div class="col-lg-4">
<label for="WorkProfile"> Work Profile</label>
<select class="form-control" placeholder="workprofile" ng-model="user.workProfile" required>
<option value="">Select Profile</option>
<option>UI Developer</option>
<option>Tester</option>
</select>
</div>
<div class="col-lg-4">
<label for="ContactNumber"> Contact Number</label>
<input class="form-width contact-height" type="text" ng-model="user.mobileNumber" maxlength="20" required></input>
</div>
</div>
</div>
<div class="third-row">
<div class="row">
<div class="col-lg-4">
<label for="SkillSet"> Skill Set</label>
<textarea id="SkillSet" class="form-width" placeholder="Enter Your skill set here..." row="4" col="100" ng-model="user.skillset" maxlength="250" required></textarea>
</div>
</div>
</div>
<button class="btn btn-primary setng-btn" type="submit" ng-click="addnewdata">Save</button>
</form>
js:`
(function () {
angular
.module('app.login')
.factory('userprofileService', userprofileService);
userprofileService.$inject = ['config', '$http'];
function userprofileService (config, $http) {
var userprofileService = this;
userprofileService.getUserDetails = function(){
return $http({
method: 'GET',
url: config.apiURL+'/v2/user',
headers: { 'Content-Type': 'application/json' }
}).then(function(response){
return response.data;
});
}
`
That will be too much to make clear.
for short, you need REST service as backend to call to save data.
AngularJS project is only front-end, you need backend ( to develop with Nodejs, JSP or asp.net/C# ) to serve this purpose.
I have a form and in that from there are some text boxes. Textbox are generated at run time. Like user can add or remove text boxes.
Here is the form.
<form name="formprofile3" method="post" id="formprofile3" role="form" ng-submit="formprofile3()">
<div class="container">
<div class="row">
<div ng-app="angularjs-starter" ng-controller="MainCtrl">
<fieldset data-ng-repeat="choice in choices">
<div class="col-xs-12">
<div class="row">
<div class="col-xs-10">
<textarea name="exp_details" ng-model="choice.name" class="form-control textbox1" id="exp_details" placeholder="Experience" required="required" rows="3"></textarea></div>
<div class="col-xs-2">
<button class="remove" ng-show="$last" ng-click="removeChoice()">-</button>
</div>
</fieldset>
<button class="addfields" ng-click="addNewChoice()">+</button>
<div id="choicesDisplay">
{{ choices }}
</div>
</div>
</div>
</div>
<center><button type="submit" class="btn btn-home" name="btn-save1" id="btn-save1" required="required">Save & Finish <i class="icon ion-arrow-right-a"></i></button></center>
</form>
here is the angular code to submit the form.
formApp.controller('formProfile3', function($scope,$http){
$scope.formData = {};
$scope.formprofile3 = function() {
var allData={'formData': $scope.formData, 'uid': uid}
$http({
method : 'POST',
url : 'add_profile.php',
data : allData,
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.success(function(data) {
if (!data.success) {
$scope.message = data.errors.message;
}else{
alert('Your details has been updated.');
}
});
};
})
Problem is when i submit the form. It is not getting submit. no data is getting in post.
Any advise what am i doing wrong.
Edit: i can see the data in {{ choices }}.
Choices are not coming in formdata.
Here is the data in firebug.
{"formData":{},"uid":"75"}:""
Move ng-app & ng-controller directives to body/html tag, so that form & ng-submit directive should get compile
<body ng-app="angularjs-starter" ng-controller="MainCtrl">
<form name="formprofile3" method="post" id="formprofile3" role="form" ng-submit="formprofile3()">
....
</form>
</body>
Also I can't see any formData(as ng-model) two way binding is present there on page. I guess you should look for choices. Additionally add name attribute to each textarea field to it part of form object for validation purpose.
I am trying to save surveys that contain multiples questions. One survey has many questions. Likewise there must be many surveys. Each time I enter a question and the answers and click the saveQuestion button. I am using Angular and want to save that in an array (in front end). After I save all the questions of a survey and click the saveSurvey button, I want to save the survey to the mongoose schema.
Can I use two submit buttons for a form? If not what is the solution?
I had asked about this issue in my previous question.
How to save array of inputs to a child schema in a nested mongoose schema?
But I do not know how to Collect form submits in an array in front end and send to post route at once.
I searched a lot and may be there is something to do with 'ng-repeat'.
MyForm
<form class="form-horizontal" action="/CreateSurvey" method="post">
<div class="form-group">
<div>
<div class="col-sm-9">
<input type="text" name="que" placeholder="Click to enter question" style="width: 100%;">
</div>
</div>
<div>
<div class="col-sm-9">
<input type="text" name="ans1" placeholder="Click to enter answer" style="width: 100%;">
</div>
</div>
<div>
<div class="col-sm-9">
<input type="text" name="ans2" placeholder="Click to enter answer" style="width: 100%;">
</div>
</div>
<div>
<div class="col-sm-9">
<input type="text" name="ans3" placeholder="Click to enter answer" style="width: 100%;">
</div>
</div>
<div>
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-default">Save Question</button>
</div>
</div>
<div>
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-default">Save Survey</button>
</div>
</div>
</div>
Mongoose Model
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var SurveySchema = new Schema({
surveyname: String,
question : [{
que: String,
ans1: String,
ans2: String,
ans3: String,
ans4: String
}]
});
module.exports=mongoose.model('Survey',SurveySchema);
How I post to the mongoose schema
var express = require('express');
var router = express.Router();
var Survey = require('../models/QBank');
router.post('/', function(req, res, next){
new Survey({
surveyname: req.body.surveyname,
question : [{
que: req.body.que,
ans1:req.body.ans1,
ans2: req.body.ans2,
ans3: req.body.ans3,
ans4:req.body.ans4
}]
}).save(function(err, doc){
if(err) res.json(err);
else
req.flash('success_msg', 'User registered to Database');
res.redirect("/CreateSurvey");
});
});
module.exports = router;
do you have multiple form or no? if you have one form you can use input type button instead of type submit to save question in front end.. you can use ng-click on input type button and save it to an array in that controller.
if you're question is dynamic you can use type [Mixed] in you're model..
but in my idea you can send your data with jquery ajax and you don't need to use form element
You can push multiple question in an array and finally submit to api with multiple question.
in the same form add a button to push question in an array and another button to submit survey information to api.
HTML:
<form class="form-horizontal" ng-submit="addSurvay()">
<div class="form-group">
<label for="s" class="col-sm-2 control-label">S Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="s" ng-model="surveyname" placeholder="Survay name">
</div>
</div>
<div class="form-group">
<label for="q" class="col-sm-2 control-label">Question</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="q" ng-model="questionObj.que" placeholder="Question">
</div>
</div>
<div class="form-group">
<label for="a1" class="col-sm-2 control-label">Answer 1</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="a1" ng-model="questionObj.ans1" placeholder="Answer 1">
</div>
</div>
// ans 2,3,4 div
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-default" ng-click="addQuestion()">Add question</button>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Add survey</button>
</div>
</div>
</form>
Controller Code:
$scope.addQuestion = function() {
$scope.question.push($scope.questionObj);
$scope.questionObj ={};
};
$scope.addSurvey = function() {
$scope.data ={surveyname:$scope.surveyname, question: $scope.question};
$http.post('/path', $scope.data);
};
For more details see PLUNKER and server part in another question answer
I'm building a small web app (blog post alike) using AngularJs and Firebase. i implemented the addPost controller earlier and it worked good. After i wanted to add an input file inside the form which already implemented to upload images to firebase on form submit using ng-file-upload directive . As a new in angular i knew that firebase can save images as base64 but i couldn't figur out how to make it work.
Maybe some will say that its repeated question, but believe me i searched allover here i couldent find answer.
Here is my form file :
<div class="container" ng-controller="AddPostCtrl">
<form class="form-horizontal" ng-submit="AddPost(files)">
<fieldset>
<!-- Form Name -->
<legend>Create Post</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="txtTitle">Title</label>
<div class="col-md-4">
<input id="txtTitle" name="txtTitle" ng-model="article.title" type="text" placeholder="placeholder" class="form-control input-md">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="txtPost">Post</label>
<div class="col-md-4">
<textarea class="form-control" id="txtPost" ng-model="article.post" name="txtPost"></textarea>
</div>
</div>
<!-- Images -->
<div class="form-group">
<label class="col-md-4 control-label" for="pictures">Add Pictures</label>
<div class="col-md-4">
<input id="pictures" type="file" ngf-select ng-model="files" name="file"
accept="image/*" ngf-max-size="2MB" ngf-multiple="true" ngf-keep="true" ngf-keep-distinct="true" class="btn btn-primary"/>
<!-- Show image thumb and remove option -->
<span ng-repeat="file in files">
<img ngf-thumbnail="!file.$error && file" class="thumb"> <button class="btn btn-danger" ng-click="file = null" ng-show="file">Remove</button>
</span>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<input id="singlebutton" ng-disabled="!article.title || !article.post" name="singlebutton" class="btn btn-primary" type="submit" value="Publish" />
</div>
</div>
</fieldset>
</form>
</div>
here is my controller :
angular.module('myApp.addPost', ['ngRoute'])
.controller('AddPostCtrl', ['$scope','CommonProp','$firebase','$location','Upload','$timeout', function($scope,CommonProp,$firebase,$location,Upload,$timeout) {
if(!CommonProp.getUser()){
$location.path('/main');
}
/***** Add data to firebase *****/
$scope.AddPost = function(files) {
var fb = new Firebase("https://hotelboard.firebaseio.com/Articles/");
var title = $scope.article.title;
var post = $scope.article.post;
var user = CommonProp.getUser();
var images = Upload.base64DataUrl(files).then(function(base64Urls){
fb.push({
title: title,
post: post,
emailId: user,
images : base64Urls,
'.priority': user
},function(error) {
if (error) {
console.log("Error:",error);
} else {
console.log("Post set successfully!");
console.log(images);
$location.path('/home');
$scope.$apply();
}
});
});
}
}]);
and here is my GitHub for full project files
https://github.com/SaidThaher/HotelApp
If i get help with this issue , it will be more question regards to the result.
PLEASE HELP
Update :
#danialfarid updated the directive and made my day :)
Upgrade to 8.0.6 and move your fb push here:
Upload.base64DataUrl(files).then(function(base64Urls) {
fb.push({...
images : base64Urls,
},...
});
I updated the code too.
Move your fb push here:
Upload.base64DataUrl(files).then(function(base64Urls) {
fb.push({...
images : base64Urls,
},...
});
Upload.base64DataUrl will convert the files to base64 data url which then can be added to the json to be sent to the server.
I am trying to learn angularJS and have problem figuring out how to solve this issue. I am trying to prepend content of the form when user submit the form. This is my code but it's not working. Can anyone help me to figure out where the problem is? Thanks!
angular.module("displayText", [])
.controller("submitText", function($scope) {
$scope.outputArr = [];
$scope.print = function() {
$scope.outputArr.push($scope.inputText);
$scope.inputText = '';
};
};
});
<body ng-app="displayText">
<div ng-controller="submitText">
<form method="get" action="#">
<input type="text" name="input" ng-model="inputText" required/>
<button ng-click="print(inputText)">Submit</button>
</form>
</div>
<div>
<p ng-repeat="text in outputArr">{{text}}</p>
</div>
</body>
put this piece of code inside the submitText controller div
<div>
<p ng-repeat="text in outputArr">{{text}}</p>
</div>
then you whole code should be like
<div ng-controller="submitText">
<form method="get" action="#">
<input type="text" name="input" ng-model="inputText" required/>
<button ng-click="print(inputText)">Submit</button>
</form>
<div>
<p ng-repeat="text in outputArr">{{text}}</p>
</div>
</div>
here is the Plunker
in your case the ng-repeat="text in outputArr" is out of the controller that means scope of the submitText controller not accessible for the ng-repeat that's why its not print anything