I would like to have a form in my Meteor html template and on submit insert that data to my MongoDB list. My questions are:
Use the <noscript></noscript> tags to define a "normal" submit-button when javascript is disabled. Show activity on this post. You could maybe use the <noscript> tag and encapsulate the above code with the button type as submit. If the client has js, the code inside the noscript will be ignored.
It is actually possible to "submit" a "form" without form-tags. what you need is an ajax-function, e.g. with jquery, which will get the values by id. But there is no reason not to use a form tho. you should use form tag.
Yes, structurally the submit button needs to be inside a form element for the document to be valid X/HTML.
Essentially, javascript sends the form data (see below examples!) to the specified PHP file, which processes the data and echo 's back a response. You receive that response, break it up into variables, and change the page accordingly.
JQuery is included in meteor & very simplifying, why would you want to avoid it? Its quite long to traverse the dom manually with js
You could use javascript to submit your forms ajax style:
So you could just use normal form html in your template like usual:
// HTML
<form id="myform">
<input type="text" name="firstname"/>
<input type="text" name="lastname"/>
<input type="text" name="email"/>
<input type="submit" id="submit"/>
</form>
// Client JS
function submitme() {
form = {};
$.each($('#myform').serializeArray(), function() {
form[this.name] = this.value;
});
// Do validation on
// form = {
// firstname: 'first name',
// lastname: 'last name',
// email: '[email protected]'
// }
MyCollection.insert(form, function(err) {
if(!err) {
alert("Submitted!");
$('#myform')[0].reset();
} else {
alert("Something is wrong");
console.log(err);
}
});
}
You could bind the select button to insert the data when clicked:
Event map binding:
Template.templatename.events({
'submit' : function(event) {
event.preventDefault(); //prevent page refresh
submitme();
}
});
If you HATE jQuery & cant use it at all
// HTML
<form id="myform">
<input id="firstname" type="text" name="firstname"/>
<input id="lastname" type="text" name="lastname"/>
<input id="email" type="text" name="email"/>
<input type="submit" id="submit"/>
</form>
// Client JS
function submitme() {
form = {
firstname: firstname.value,
lastname: lastname.value,
email: email.value
};
// Do validation on
// form = {
// firstname: 'first name',
// lastname: 'last name',
// email: '[email protected]'
// }
MyCollection.insert(form, function(err) {
if (!err) {
alert("Submitted!");
// Empty field values
email.value = "";
firstname.value = "";
lastname.value = "";
} else {
alert("Something is wrong");
console.log(err);
}
});
}
Just for reference, there is a slightly cleaner way to do forms in Meteor without all the global jQuery references, which I find to be messy. It makes code a lot less error prone by scoping to the template, which is provided in the event handler. Here's an example using the form described in the other answer:
Template code:
<template name="foo">
<form id="myform">
<input type="text" name="firstname"/>
<input type="text" name="lastname"/>
<input type="text" name="email"/>
<input type="submit" id="submit"/>
</form>
</template>
Event handler:
Template.foo.events({'submit form' : function(event, template) {
event.preventDefault();
firstname = template.find("input[name=firstname]");
lastname = template.find("input[name=lastname]");
email = template.find("input[name=email]");
// Do form validation
var data = {
firstname: firstname.value,
lastname: lastname.value,
email: email.value
};
email.value="";
firstname.value="";
lastname.value="";
MyCollection.insert(data, function(err) { /* handle error */ });
}});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With