Want to add comment at respective places - angularjs

Below is the structure of threaded comments in angular.js form.
<form>
<ul class="unstyled">
<!-- ng-class="{ '{{childcond}}': $index == selectedIndex }" -->
<li ng-repeat="comment in comments" class="{{childcond}}" ng-init=" childcond=checkChild(comment.slug); reply.parentSlug=comment.slug; idstring=slashRemove(comment.slug);" >
<strong>{{comment.author}}</strong><br>
<p>
{{comment.body}}
</p>
<a href="javascript:void(0)" class="replycomment" >reply</a>
<div id="{{idstring}}" style="display:none;" >
<input type="text" placeholder="Your Name" ng-model="reply.replyAuthor"><br>
<span style="display:none;">
<input type="text" class="hidevalue" ng-model="reply.parentSlug" ng-value="{{comment.slug}}" >
</span>
<textarea name="" id="" cols="30" rows="10" placeholder="Comment" ng-model="reply.replyBody"></textarea>
<input class="btn-primary" type="submit" value="reply" ng-click="replyComment(reply,$index)">
</div>
</li>
</ul>
When i replying to the comment i am getting my comment in last place.
This is my reply code in angular.js file.
$scope.replyComment = function(reply,$index) {
socket.emit('comment', { author: reply.replyAuthor,
body: reply.replyBody,
parent_slug:reply.parentSlug });
clearForm();
};
socket.on('comment', function(comment) {
$scope.$apply(function() {
console.log(comment);
$scope.comments.push(comment);
});
});
console.log(comment) gives following structure.
Any help would be much appreciated!!!

Related

I am performing ngclick show hide but can't get any output

i am using angular 1.6 and trying to perform ng-click hide show of profile data but anyhow i dont know where i am going wrong.
here the code
<div class="col-sm-12 col-lg-10" ng-controller="profile">
<div class="text-center">
<div class="col-sm-12">
<div class="pull-right">
<a ng-click="edit-form = ! edit-form"> <i class="fa fa-pencil fa-2x" style="cursor:pointer;color:#f7972f;"> </i> </a>
</div>
<div class="image-upload edit-picture">
<label for="file-input">
<i class="fa fa-camera fa-2x" style="cursor:pointer;color:#f7972f;"> </i>
</label>
<input id="file-input" type="file"/>
</div>
<img src="amit.jpg" class="img img-circle edit-image" style="width:150px;">
<h3> Amit Singh Chauhan </h3>
</div>
<div class="col-sm-12" ng-show="edit-form">
<div class="col-sm-1"></div>
<div class="col-sm-10" >
<form action="" novalidate="novalidate">
<p><span class="wpcf7-form-control-wrap first-name"><input type="text" name="first-name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="First Name"></span></p>
<p><span class="wpcf7-form-control-wrap last-name"><input type="text" name="last-name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Last Name"></span></p>
<p><span class="wpcf7-form-control-wrap email-address"><input type="email" name="email-address" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" aria-required="true" aria-invalid="false" placeholder="E-mail Address"></span></p>
<div><span class="wpcf7-form-control-wrap your-message"><textarea name="your-message" cols="40" rows="10" class="wpcf7-form-control wpcf7-textarea" aria-invalid="false" placeholder="Comment"></textarea></span></div>
<div><input type="submit" value="Save Profile" class="wpcf7-form-control wpcf7-submit mkd-contact3"><span class="ajax-loader"></span></div>
</form>
</div>
<div class="col-sm-1"></div>
</div>
</div>
<script>
var app = angular.module("myApp", []).controller ("profile", function ($scope){
});
</script>
Try changing edit-form to editForm.

How to insert array data into database using ajax in codeigniter

How to insert array data into the database using ajax in CodeIgniter?
In my view I use code below, I have stack to find the solution because have many different structures
<form class="form-story" action="" onsubmit="return false" id="form-story">
<div class="tag-input form-group">
<div class="row">
<div class="col-xs-4 col-md-4 text-right">
<label for="crew" class="control-label">Crew #1</label>
</div>
<div class="col-xs-8 col-md-5">
<input type="text" name="crew_name[]" class="form-control" placeholder="NAME OF CREW MEMBER #1" style="margin-bottom: 5px">
<input type="text" name="crew_email[]" class="form-control" placeholder="EMAIL OF CREW MEMBER #1">
</div>
</div>
</div>
<div class="tag-input form-group">
<div class="row">
<div class="col-xs-4 col-md-4 text-right">
<label for="crew" class="control-label">Crew #2</label>
</div>
<div class="col-xs-8 col-md-5">
<input type="text" name="crew_name[]" class="form-control" placeholder="NAME OF CREW MEMBER #2" style="margin-bottom: 5px">
<input type="text" name="crew_email[]" class="form-control" placeholder="EMAIL OF CREW MEMBER #2">
</div>
</div>
</div>
<div class="tag-input form-group">
<div class="row">
<div class="col-xs-4 col-md-4 text-right">
<label for="crew" class="control-label">Crew #3</label>
</div>
<div class="col-xs-8 col-md-5">
<input type="text" name="crew_name[]" class="form-control" placeholder="NAME OF CREW MEMBER #3" style="margin-bottom: 5px">
<input type="text" name="crew_email[]" class="form-control" placeholder="EMAIL OF CREW MEMBER #3">
</div>
</div>
</div>
<div id="btn-add-more-wrapper" class="form-group text-center btn-add-more-wrapper">
<a href="javascript:void(0)" id="btn-add-more">
<i class="fa fa-plus"></i> <br> Add More
</a>
</div>
<div class="btn-groups-wrapper">
<div class="form-group">
<input type="submit" onClick="" value="SUBMIT" id="btnSubmit" class="btn btn-default is-disabled">
</div>
</div>
</form>
<script>
$('#form-story').submit(function() {
var dataString = $(this).serialize();
$.ajax({
type : 'post',
url : '<?php echo base_url()?>index.php/the_flow_crew/save_tag_member',
data : dataString,
dataType : 'json',
success : function(res) {
console.log(res);
}
});
return false;
});
</script>
Please help me to give the solution, Thank you
Make your post data into jsonencode and try to store in database. It will work.
For example
$crew_name = json_encode($_POST['crew_name']);
Above code will convert your array into json string. When you try to retrieve the data back in array format try to use json_decode($column_name) it help you to sort out your problem
If you want to store entire post into database pass your $_POST in json_encode($_POST)
Let me know if you need more guidelines on it. I'm happy to help you out.

how to error message on submitting of a from in angular js

I want to show the required error message once the from is submitted but not sure how to do it i am trying as shown below
<div class="controls">
<form name="formx" >
<ul class="form small">
<div class="tag">ID</div>
<input type="text" class="small" name="enrolmentId" ng-model="enrolmentDetail.Id" required="" onkeypress='return !(event.charCode == 32 )'>
<div class="error text-danger" style="position: absolute;margin-left: 120px;" ng-show="formx.enrolmentId.$error.required && (formx.$submitted || formx.enrolmentId.$touched)"><p>Id is required</p></div>
</li>
<li>
<div class="btn" ng-click="saveDetails(enrolmentDetail)" ng-show="formAction=='save'">SAVE</div>
</li>
</ul>
</form>
</div>
here now the error message is shown once we dirty the textbox but i want to show the error message once the form is submitted and if there is required error from should not be submitted
trying used ng-submit but not sure how to do it
Please help by creating a fiddle or posting a example to do it
This is an example
html
<body ng-app="myApp">
<div ng-controller="myCtrl as mc">
<form class="form" name="myForm" ng-submit="mc.submit(myForm)" novalidate>
<div class="form-group">
<label for="username">Username</label>
<input name="username" class="form-control" type="text" ng-model="mc.username" required/>
<p class="text-danger" ng-show="myForm.username.$error.required && mc.submitted">Username is required</p>
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" ng-model="mc.password" required/>
<p class="text-danger" ng-show="myForm.password.$error.required && mc.submitted">Password is required</p>
</div>
<button class="btn btn-success">submit</button>
</form>
<p class="text-success" ng-show="mc.sent && mc.submitted">Form sent</p>
js
angular.module("myApp",[])
.controller("myCtrl", function(){
var vm = this;
vm.submit = submit;
function submit(form){
vm.submitted = true;
if(form.$valid && vm.submitted === true){
//Send data logic
vm.sent = true;
}
}

AngularJS Form submit

Working on my first small AngularJS App I'm facing problems with a form submit. I worked trough the CodeSchool course and figured the most out by myself, but this form submit thingy... well I just don't get where I'm wrong so that's why it would be nice if you could show me the right solution, so I can go on.
Project: A simple Workout List where you can list all the training sessions you had. This is my HTML, Element 3 is the problem:
<header class="wob-masthead container-fluid">
<div class="row">
<div class="col-md-6" ng-init="tab = 1">
<ul class="nav nav-pills">
<li ng-class="{ active:tab === 1 }"><a href ng-click="tab = 1">Overview</a></li>
<li ng-class="{ active:tab === 2}"><a href ng-click="tab = 2">Stats</a></li>
<li ng-class="{ active:tab === 3 }"><a href ng-click="tab = 3">New</a></li>
</ul>
</div>
<div class="col-md-6">
<form class="navbar-form pull-right" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
</div>
</div>
</header>
<section class="wob-main mainlist container" id="headjump">
<!--- ==========================================
Element 1: Overview
============================================= -->
<div class="subsite" ng-show="tab === 1">
<div class="headico"><span class="glyphicon glyphicon-signal" aria-hidden="true"></span></div>
<h1>WorkoutBuddy</h1>
<div class="table-responsive" ng-controller="ListController as listing">
<table class="table table-hover">
<thead>
<tr>
<th class="col-md-2">Date</th>
<th class="col-md-8">Type</th>
<th class="col-md-1">Repeat</th>
<th class="col-md-1">Time</th>
</tr>
</thead>
<tbody ng-controller="ListController as listing">
<tr ng-repeat="wo in listing.sessions">
<td>{{wo.date | date:'dd/MM/yyyy'}} </td>
<td>{{wo.name}}</td>
<td>{{wo.repeat}}</td>
<td>{{wo.time}} Minutes</td>
</tr>
</tbody>
</table>
</div>
</div>
<!--- ==========================================
Element 2: Stats
============================================= -->
<div class="subsite" ng-show="tab === 2">
<div class="headico"><span class="glyphicon glyphicon-signal" aria-hidden="true"></span></div>
<h1>Stats</h1>
<!-- Ende Subsite -->
</div>
<!--- ==========================================
Element 3: New
============================================= -->
<div class="subsite" ng-show="tab === 3">
<div class="headico"><span class="glyphicon glyphicon-signal" aria-hidden="true"></span></div>
<h1>New</h1>
<div class="table-responsive" ng-controller="ListController as listing">
<table class="table table-hover">
<thead>
<tr>
<th class="col-md-2">Date</th>
<th class="col-md-8">Type</th>
<th class="col-md-1">Repeat</th>
<th class="col-md-1">Time</th>
</tr>
</thead>
<tbody ng-controller="ListController as listing">
<tr ng-repeat="wo in listing.sessions | limitTo:2">
<td>{{wo.date | date:'dd/MM/yyyy'}} </td>
<td>{{wo.name}}</td>
<td>{{wo.repeat}}</td>
<td>{{wo.time}} minutes</td>
</tr>
</tbody>
</table>
</div>
<form name="WorkoutForm" ng-controller="EntryController as entryCtrl">
<blockquote>
<h3>Last Workout:</h3>
<strong>{{entryCtrl.wo.name}}</strong><br>
<small>am: {{entryCtrl.wo.date}}</small><br>
{{entryCtrl.wo.repeat}} repeats in {{wo.time}} minutes.
</blockquote>
<input ng-model="entryCtrl.wo.date" type="date" placeholder="date" />
<input ng-model="entryCtrl.wo.name" type="name" placeholder="name" />
<input ng-model="entryCtrl.wo.repeat" type="repeat" placeholder="repeat" />
<input ng-model="entryCtrl.wo.time" type="time" placeholder="time" />
<input type="submit" value="Add" />
</form>
<!-- Ende Subsite -->
</div>
</section>
I styled it with Bootstrap and this is my app.js:
(function(){
var app = angular.module('wobuddy', [ ]);
app.controller('ListController', function(){
this.sessions = wos;
});
var wos = [
{
name: 'Squat',
date: '01.01.2015',
repeat: 50,
time: 10
},
{
name: 'Push Ups',
date: '01.01.2015',
repeat: 50,
time: 10
}
];
})();
Switching between the sections using the nav works pretty fine and also printing out the data-elements in the table, but when I push submit nothing happens - really hope you can help me to learn :-)
You need to make an EntryController that will add a new object to the end of the wos collection. Something like this:
app.controller('EntryController', function($scope) {
$scope.wo = {};
$scope.submit = function() {
wos.push($scope.wo);
$scope.wo = {}; // Clear the form fields
};
});
Then your HTML for that section could look something like this:
<form name="WorkoutForm" ng-controller="EntryController">
<blockquote>
<h3>Last Workout:</h3>
<strong>{{wo.name}}</strong><br>
<small>am: {{wo.date}}</small><br>
{{wo.repeat}} repeats in {{wo.time}} minutes.
</blockquote>
<input ng-model="wo.date" type="date" placeholder="date" />
<input ng-model="wo.name" type="name" placeholder="name" />
<input ng-model="wo.repeat" type="repeat" placeholder="repeat" />
<input ng-model="wo.time" type="time" placeholder="time" />
<button ng-click="submit()">Add</button>
</form>
Notice that it's more usual for a controller to communicate data to the template via the $scope object rather than via the controller object itself.
By looking at you form HTML, I think you missed the name attribute inside your form and also ng-submit directive is missing which will gets called after a submit form. Do check form validation inside controller using $valid() method and perform post else give alert to user.
HTML
<form name="workoutForm" ng-controller="ReviewController as reviewCtrl" ng-submit="submit(workoutForm, entryCtrl.wo)">
<blockquote>
<h3>Last Workout:</h3>
<strong>{{entryCtrl.wo.name}}</strong>
<br>
<small>am: {{entryCtrl.wo.date}}</small>
<br> {{entryCtrl.wo.repeat}} repeats in {{wo.time}} minutes.
</blockquote>
<input name="date" ng-model="entryCtrl.wo.date" type="date" placeholder="date" />
<input name="name" ng-model="entryCtrl.wo.name" type="name" placeholder="name" />
<input name="repeat" ng-model="entryCtrl.wo.repeat" type="repeat" placeholder="repeat" />
<input name="time" ng-model="entryCtrl.wo.time" type="time" placeholder="time" />
<input type="submit" value="Add" />
</form>
Controller
$scope.submit = function(workoutForm, item){
if(workoutForm.$valid)
//then make $http.post by sending item values
else
//show error
};
UPDATE
<html ng-app='demoApp'>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<form ng-controller="validationCtrl">
<input type="text" placeholder="Name...." ng-model="user.name"/>
<input type="text" placeholder="Password...." ng-model="user.pass"/>
<input type="text" placeholder="Mobile...." ng-model="user.mo"/>
<input type="submit" ng-click="alldata(user)"/>
</form>
<script>
//This is controller
var app = angular.module('demoApp', []);
app.controller('validationCtrl', function($scope) {
$scope.alldata=function(user)
{
alert(JSON.stringify(user));
}
});
</script>
</body>
</html>
You can also use this method, and
Your form shoud be like
<form method="post" name="sentMessage" id="my_contact" novalidate="novalidate">
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Name</label>
<input class="form-control" id="name" type="text" name="name" placeholder="Name" required="required" data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Email Address</label>
<input class="form-control" id="email" type="email" name="email" placeholder="Email Address" required="required" data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Phone Number</label>
<input class="form-control" id="phone" type="tel" name="phone" placeholder="Phone Number" required="required" data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Message</label>
<textarea class="form-control" id="message" rows="5" name="Message" placeholder="Message" required="required" data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="form-group">
Send
</div>
</form
import jquery as below
npm install jquery using CLI
import * as $ from 'jquery';
send_query() function will be
send_query() {
var data = $("#my_contact").serializeArray();
var indxarr = {};
$.each(data,function(i,v){
indxarr[v['name']] = v['value'];
});
data = JSON.parse(JSON.stringify(indxarr))
//POST YOUR DATA
this.httpClient.post('http://localhost/rajat/ajax/contact_query_submit/', data,httpOptions)
.subscribe(data => {
console.log(data);
});
}
Your backend code will be
public function contact_query_submit(){
if ($this->input->post()) {
$postdata = file_get_contents("php://input");
$_POST = json_decode($postdata,TRUE);
$addArr = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'message' => $this->input->post('message'),
'created' => time()
);
if($this->main_model->create('rjt_contact', $addArr)){
$arr[] = array('type' => 'success', 'msg' => '<p>Your query has been received. <br>We will get back to you soon.</p>');
echo json_encode($arr);
}else{
$arr[] = array('type' => 'warning', 'msg' => '<p>'.$this->db->last_query().'</p>');
echo json_encode($arr);
}
}else{
$arr[] = array('type' => 'warning', 'msg' => '<p>No data post yet</p>');
echo json_encode($arr);
}
}

Set a form input to dirty in angularjs?

I have a form that I auto input the current date into an input field. I want to set the input field to dirty because when it tries to sync with firebase, that input field is not included unless you interact with it.
I have tried the following in many different forms...and I am at a loss.
var now = new Date();
$scope.load = function () {
if (!$rootScope.auth) {
$location.path('/a/');
} else {
$scope.logs = Logs.list($scope);
$scope.newDate = now;
$scope.logForm.$setDirty(true);
}
};
<form name="logForm" novalidate class="css-form">
<div class="form-group input-group">
<input class="form-control" name="action" placeholder="Action" type="text" value="{{newAction}}" ng-model="newAction" required />
<div class="input-group-addon">
<i class="fa fa-rocket"></i>
</div>
</div>
<div class="form-group input-group">
<input class="form-control" name="quantity" placeholder="Quantity" type="number" value="{{newQuantity}}" ng-model="newQuantity" ng-pattern="/^[0-9]*$/" />
<div class="input-group-addon">
<i class="fa fa-plus-square"></i>
</div>
</div>
<div class="form-group input-group">
<input class="form-control" name="location" placeholder="Location" type="text" value="{{newLocation}}" ng-model="newLocation" />
<div class="input-group-addon">
<i class="fa fa-map-marker"></i>
</div>
</div>
<div class="form-group input-group">
<input class="form-control" name="date" placeholder="Date" type="datetime" value="{{newDate}}" ng-model="newDate" />
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
</div>
<button class="btn btn-info btn-block" ng-click="create()">Add log</button>
</form>
The form tag creates its own scope, so to have a reliable scope structure, we shouldn't use primitives for the ng-model values.
To ensure, that we have access to the form controller, we have to delay the form initialization into the next digest cycle. This is achieved with the use of the $timeout service.
http://jsbin.com/OvIvIHO/3/
jsbin contains the updated version

Resources