Functions in Angular controller not activated on button click - javascript

I am trying to make a contact me from with Angular. Right now, I have some input fields, a button, and an angular app as a script in the page to try and make things as simple as possible. I was trying to make sure things are talking to each other so I just put a console.log in my angular controller which I think should print to the console when I click the button. However, it just reloads the page every time.
Below is the code that is showing this issue
HTML:
<body ng-app="contactApp">
<div class="container">
<div class="row">
<div class="col-sm-8 blog-main">
<!-- Input Fields for contact form -->
<form ng-controller="ContactCtrl" class="form-horizontal" role="form" ng-submit="submit(name, email, message)">
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-4">
<input class="form-control" type="text" placeholder="Name" ng-model="info.name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Email</label>
<div class="col-sm-4">
<input class="form-control" type="email" placeholder="Email" ng-model="info.email">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Message</label>
<div class="col-sm-4">
<input class="form-control" type="text" placeholder="Place Message Here" ng-model="info.text">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-success" type="submit">Contact Me</button>
</div>
</div>
</form>
</div><!-- /.blog-main -->
</div><!-- /.row -->
</div><!-- /.container -->
JS:
var contactApp = angular.module('contactApp', [])
.controller('ContactCtrl', function ($scope, $http){
$scope.submit = function (name, email, message){
console.log('contact with controller')
}
})
Most code is just creating the HTML form, see angular app at the bottom.

ooo so close ;) you've got a little typo in there ng-controller="contactCtrl" != .controller('ContactCtrl') check out your first letter.

I was using Firefox as my browser and I was looking for the output in their developer console. It was appearing in the firebug console. I still don't know why it doesn't appear in the regular console, but I will check firebug as well in the future. I accepted btm1's answer because he pointed out my typo and that made everything work when I started up firebug.

Related

Form submit does not close the form and reload to the homepage

I have a problem setting up a form on my website.
I want to add two separate forms on my website, which i did, using some HTML & css.
Thoses forms need to send informations to a webhook -> it also works (since it's collected)
BUT as soon as i press submit, it redirect to the main page of the website, so no conversion (which is a shame) and a very bad input from me.
Please, lend me your intelligence, since i must be the best idiot there is.
<form name='myForm' data-form-id='IdFromWebhook' action='WebhookLink' method='post' onsubmit="return false">
<div class="row">
<div class="col-lg-12">
<div class="form-group text_box">
<label for='EMAIL_ADDRESS'>E-mail: </label><input id='insightly_email' name='email' type='text' required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group text_box">
<label for='FIRST_NAME'>First name: </label><input id='insightly_firstName' name='firstName' type='text' required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group text_box">
<label for='LAST_NAME'>Last Name: </label><input id='insightly_lastName' name='lastName' type='text' required>
</div>
</div>
<div class="col-lg-4">
<div class="form-group text_box">
<label for='Language__c'>Choose a language: </label>
<select id='insightly_language' name='language' required><option value='EN'>EN</option><option value='FR'>FR</option><option value='DE'>DE</option></select>
</div>
</div>
</div>
<!--Bouton d'envoi-->
<div style="display:flex;justify-content:center"><input class="btn_three" type='submit' value='Submit'></div></form>
Use event.preventDefault() this will stops redirect:
formReference.onsubmit = (event) => {
event.preventDefualt()
}
I stupidly found the right answer, i had to specify URLs for the success or error pages.
It was an insightly form.

After form submit redirect back to page and show a modal

I'll try to explain this as best as possible.
I have a form that on submit actions on email.php:
<form data-toggle="validator" role="form" method="POST" action="email.php">
<div class="row">
<!-- Full Name -->
<div class="form-group col-lg-4">
<label for="inputName" class="control-label">Name</label>
<input type="text" class="form-control" name="inputName" id="inputName" required>
</div>
<!-- email -->
<div class="form-group col-lg-4">
<label for="inputEmail" class="control-label">Email</label>
<input type="email" class="form-control" name="inputEmail" id="inputEmail" data-error="Incorrect Email Format" required>
</div>
<!-- phone number -->
<div class="form-group col-lg-4">
<label for="inputTel" class="control-label">Phone Number</label>
<input type="text" pattern="(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}" name="inputTel" id="inputTel" data-minlength="10" maxlength="15" class="form-control" placeholder="123-456-7890" data-error="(123)-456-7890 | 1234567890 | 123-456-7890">
</div>
<!-- message -->
<div class="form-group col-lg-12">
<label for="content" class="control-label">Message</label>
<textarea name="content" id="content" class="form-control" rows="6" data-minlength="5" data-error="Message Must be longer"></textarea>
</div>
<!-- button -->
<div class="form-group col-lg-12">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
After the form is successfully processed in email.php, I want it to go back to the page the form is located on and show the "thank you" modal I created
currently I have in email.php (after validation):
echo "<script>
$(window).load(function(){
$('#myModal').modal('show');
});
window.location.href="Websitename";
</script>";
This does not work. What happens is the page just gets redirected but does not display the modal.
Can anyone please provide me any assistance?
I think the problem is in
window.location.href="Websitename";
When browser see this line , she will automatically locate the browser which means new request immediately made.
Let me explain :
Your form send request to email.php -----> sends response as a javascript --> javascript request to "website name".
In order to correct this issue , you should put window.location.href inside timeout function and that timeout triggered after window.load .
Please check :
echo "<script>
function deferredFunction(){
window.location.href="Websitename";
}
$(window).load(function(){
$('#myModal').modal('show');
setTimeout(deferredFunction,5000);
});
</script>";
Hope Helps!!

AngularJS: Required attribute working for some fields but not others?

So I have a form with nested forms within. When I click submit, and if there are any required fields that are not filled, I want the boxes to highlight and for there to be an alert displayed.
What I have now works for all the fields within the Company Information field - the alert displays properly, the fields are highlighted, and the form does not get submitted. However, this does not work for the fields within Product Information. Even if there are unfilled required forms, the form continues to get submitted, and there is no invalid highlight.
I did notice, however, that if I had invalid fields in Company Information and invalid fields in Product information, all fields have invalid highlights and it does not proceed to submit. However the case where only Product Information has invalid fields does not work. Also, if I remove ng-class="{failedSubmit:model.failedSubmit}" from the product form attribute, then even in the case where both Company and Product have invalid fields, invalid Product fields do not highlight at all - so I'm puzzled as to why totalForm does not cover all the forms its wrapping.
Anyone have any insight? Thanks!
So here is my HTML:
<form name="totalForm" ng-class="{failedSubmit:model.failedSubmit}">
<div id="collapse3" class="row">
<h1 class="no-margin">Company Information</h1>
<form class="form-horizontal" role="form" name="company">
<div class="container">
<div class="col-md-12">
<div class="col-md-12">
<label for="companyName-md" class="control-label required">Company Name</label>
</div>
<input class="form-control" id="companyName-md" name="companyName"
ng-show=type="text"
ng-model="model.companyName"
required/>
</div>
</div>
<div class="col-md-12">
<div class="col-md-12">
<label for="companyDesc-md" class="control-label text-area-label required">Company
Description</label>
</div>
<textarea class="form-control" id="companyDesc-md" name="companyDesc"
ng-model="model.companyDesc" rows="5" required> </textarea>
</div>
</div>
</div>
</form>
</div>
<div id="collapse4" class="row email-pref">
<h1 class="no-margin">Product Information</h1>
<form class="form-horizontal" name="product" ng-class="{failedSubmit:model.failedSubmit}">
<div class="container">
<div class="col-md-12">
<div class="col-md-12">
<label for="productType-md" class="control-label text-area-label required">Product
Type</label>
</div>
<select class="form-control" id="productType-md" name="productType"
ng-options="item for item in productTypeList" rows="3"
ng-model="model.productType" required></select>
</div>
</div>
<div class="col-md-12">
<div class="col-md-12">
<label for="productDesc-md" class="control-label text-area-label required">Product
Description</label>
</div>
<textarea class="form-control" id="productDesc-md" name="productDesc"
ng-model="model.productDesc" rows="5" required></textarea>
</div>
</div>
</div>
</form>
</div>
</form>
And here is my CSS class:
form.failedSubmit .ng-invalid
{
border:1px solid #f00;
}
And here is the relevant javascript code - this is fired upon pressing a submit button:
if (!$scope.totalForm.$valid || !$scope.product.$valid){
$scope.showAlerts.push($scope.alerts[0]);
window.scrollTo(0, 0);
$timeout(function(){
$scope.showAlerts.pop();
}, 3000);
$scope.model.failedSubmit=true;
}
I'm answering without testing it so let me know if you need example... first, my recommendation is to use one form, with different divs that you can toggle with ng-show. is there a special reason you want nested forms? second, labels don't need to have the required field and you also wrote twice few lines with the col-md-12. third, where are you submitting your form? I didn't see it in your code. let me know if you solved it.

Bootstrap and placeholder

so I'm having a weird problem that I haven't encountered before. My code is the following:
<div class="row">
<div class="col-md-12">
<h4>Send message to seller</h4>
<div class="panel panel-default">
<div class="panel-body">
<form action="#" method="POST">
<div class="form-group">
<label for="InputEmail">Email address</label>
<input type="email" class="form-control" id="InputEmail" placeholder="Enter your email" >
</div>
<div class="form-group">
<label for="InputText">Your text</label>
<textarea class="form-control" id="InputText" name="message" placeholder="Type in your message" rows="5" style="margin-bottom:10px;"></textarea>
</div>
<button class="btn btn-info" type="submit">Send</button>
</form>
</div>
</div>
</div>
</div>
When I run it on the server, this is the result, and it is really annoying:
<div class="form-group">
<label for="InputEmail">Email address</label>
<div class="has-placeholder form-control"><label>Enter your email</label><input type="email" class="form-control" id="InputEmail"></div>
For the second code, I omitted the rest of the code. As you can see, it adds a new and a new with a class of place-holer.
I have 2 versions of this file. Full HTML, and one with PHP etc.. The HTML version is just fine, but this one, for some reason, has this result. Can anyone explain to my why this is happening.
I have looked in the js files that are being included and cannot find anything regarding that added class and what not.
Thanks.
Your this html code is just perfect. Probably you missed some tag in php file. Can you please post your php code so that we can sorted it out.
Thanks

How is user input submitted in bootstrap 3 form?

I'm using bootstrap 3, which includes jquery 1.11.2.min.js. I'm updating a search webpage with two textfields: keyword and city,state,zip. I have a script for the search results that needs to pull the user input that's typed into these two search textfields for finding the desired search results.
Visual explanation:
My search bar with placeholders and search button:
My search bar with my user input (searching for an engineering job in Boston):
In the HTML, there is no unique value (value="Engineer" or value="Boston") created for my engineering job in Boston search:
Without value="Engineer" and value="Boston" I cannot tell my script to display search results of engineering jobs in Boston. Where do I find/create this to pull into my search script?
Is this located in the bootstrap.js file anywhere? I'm having trouble figuring out where/how this form is pulling the user input value (without having to use value="" for each input).
Your help is much appreciated - my form is below.
<div id="somebanner" class="somebanner-bgsearch-red hero1 hidden-xs">
<div class="container-fluid">
<div class="top-section">
<div class="container">
<form class="form-inline no-margin center-block" action="http://www.somesite.com/unknown" method="POST" role="form">
<div class="row">
<center>
<fieldset>
<!-- Search input-->
<div class="form-group center-block">
<div class="col-sm-4">
<input id="keyword" name="keyword" type="search" placeholder="Keyword" class="form-control input-lg">
</div>
</div>
<!-- Search input-->
<div class="form-group center-block">
<div class="col-sm-4">
<input id="location" name="location" type="search" placeholder="City, State, or Zip Code" class="form-control input-lg">
</div>
</div>
<input type="hidden" name="unknown" value="unknown">
<!-- Button -->
<div class="form-group center-block">
<div class="col-sm-2">
<button type="submit" id="btn-search" name="btn-search" class="btn btn-primary btn-lg btn-tusaj">Search</button>
</div>
</div>
</center>
</fieldset>
</div>
</form>
</div>
</div>
</div>
</div>
Here's the link to the bootstrap.js code: http://getbootstrap.com/dist/js/bootstrap.js
Herer's the link to the jquery code:
https://code.jquery.com/jquery-1.11.2.js
You're trying to use Bootstrap for modifying HTML, but Bootstrap is a CSS framework used for responsive design. It's incapable of doing such HTML modification. You need to use jQuery:
$(document).ready(function(){
$("#btn-search").click(function(e){
e.preventDefault();
var x = $("#keyword").val();
var y = $("#location").val();
$("#keyword").attr("value", x);
$("#location").attr("value", y);
});
});
That should do the work. If you have further questions, let me know in the comments.

Categories