Webdesigner seeking deeper understanding of forms...
Hello!
I have two similar contact forms on the same page which are sent out through ajax.
Each contact form is working fine on its own, but as soon as I put them both together on the page, one of them stops working...
I know there are issues having two forms on one page, so I gave them different ids (#contactForm1, #contactForm2), which is working, but I am sure there has to be a smarter way to do this...
What advice would you give me to make this working the best way?
My html as followed:
<form role="form" id="contactForm" class="contact-form" data-toggle="validator">
<section id="content">
<!-- NAME -->
<div class="form-group">
<div class="controls">
<input autocomplete="off" type="text" id="name" class="form-control" placeholder="Your Name" required data-error="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
<!-- SUBJECT -->
<div class="form-group">
<div class="controls">
<input autocomplete="off" type="text" id="msg_subject" class="form-control" placeholder="Company Name" required data-error="Please enter your message subject">
<div class="help-block with-errors"></div>
</div>
</div>
<!-- MESSAGE -->
<div class="form-group">
<div class="controls">
<textarea id="message" rows="7" placeholder="short description" class="form-control" required data-error="Write your message"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<!-- EMAIL -->
<div class="form-group">
<div class="controls">
<input autocomplete="off" type="email" class="email form-control" id="email" placeholder="Email Adress" required data-error="Please enter your email">
<div class="help-block with-errors"></div>
</div>
</div>
<button type="submit" id="submit" class="btn btn-success"></i> SEND</button>
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="clearfix"></div>
</section>
</form>
Here my JS:
$("#contactForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
formError();
submitMSG(false, "got everything right?");
} else {
event.preventDefault();
submitForm();
}
});
function submitForm(){
var name = $("#name").val();
var email = $("#email").val();
var msg_subject = $("#msg_subject").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "assets/php/form-process.php",
data: "name=" + name + "&email=" + email + "&msg_subject=" + msg_subject + "&message=" + message,
success : function(text){
if (text == "success"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
Use class="contactForm" instead of id="contactForm" and in js:
(".contactForm").validator().on("submit", function (event) {...}
Related
When I try and submit a form here (php page) - http://portsdownhb.ogdigital.co.uk/ the page throws the error 'Failed to load resource: the server responded with a status of 404 (Not Found)' when pressing submit, despite the exact same working on a HTML page.
The error suggests the page http://portsdownhb.ogdigital.co.uk/contact-us/contact.php is trying to be found but it should be http://portsdownhb.ogdigital.co.uk/contact.php
$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "contact.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();
}
}
});
return false;
}
})
});
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="First Name" required="required" data-error="Name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input id="form_email" type="email" name="email" class="form-control" placeholder="Email" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea id="form_message" name="message" class="form-control" placeholder="Message" rows="4" required="required" data-error="Please, leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="g-recaptcha" data-sitekey="****"></div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
</div>
</form>
I'm working on a site and the goal was to submit a contact form without reloading the whole page. I tried different paths, but nothing seems to help. It looks like the "on submit" part doesn't work at all I'm still leaving site to contact_form.php. I would be very thankful for any help, thanks in advance
This is js part:
$("#contact-form").on('submit', function(){
var name = $("#form_name").val();
var lastname = $("#form_lastname").val();
var email = $("#form_email").val();
var subject = $("#form_subject").val();
var message = $("#form_message").val();
var dataString = 'name=' + name + '&lastname=' + lastname + '&email=' + email +'&subject'+subject + '&message=' + message;
$.ajax({
type: "POST",
url: "contact_form.php",
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
return false;
});
and here html:
<form id="contact-form" method="post" action="contact_form.php" role="form">
<div class="messages"></div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="Name" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class ="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Last name" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_email" type="text" name="mail" class="form-control" placeholder="Email" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_subject" type="text" name="subject" class="form-control" placeholder="Subject" required="required" data-invalid-message="Please enter your name">
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<textarea id="form_message" name="message" class="form-control" placeholder="Message" rows="4" required="required" data-invalid-message="Please enter your name"></textarea>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success" value="Send message">
</div>
</div>
</form>
You can start by passing the event and stopping the default behaviour, which would be to hit the action of the form.
$("#contact-form").on('submit', function(e){
e.preventDefault();
// rest of code... ( do not return true in the end of this handler though )
I want to create a submit form to enter MySQL db data and image. I want to make do this using same form through jquery ajax. I tried my best with following code. I couldn't be success. When I enter id I want auto fill email and name field auto. I did it through ajax successfully. I couldn't submit both file and data through ajax.
<?php
print_r($_POST);
print_r($_FILES);
?>
<form class="form-horizontal" action="postvaccode2.php" method="post" id="data" enctype="multipart/form-data">
<fieldset>
<!-- Form Name -->
<legend>Post Vacancy</legend>
<!-- Select Basic -->
<div class="form-group">
<div class="col-md-5">
<select id="cato" name="cato" class="form-control" id="cato">
<option value="IT">IT</option>
<option value="Finance">Finance</option>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-6">
<input id="comid" name="comid" type="text" placeholder="comid" class="form-control input-md" required="" >
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-6">
<input id="loc" name="loc" type="text" placeholder="city or town" class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-8">
<input id="qul" name="qul" type="text" placeholder="qulification" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-8">
<input id="indate" name="indate" type="date" placeholder="indate" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-4">
<input id="expdate" name="expdate" type="date" placeholder="expdate" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="gpa" name="gpa" type="text" placeholder="gpa" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="des" name="des" type="text" placeholder="description" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="title" name="title" type="text" placeholder="title" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="comname" name="comname" type="text" placeholder="name" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="email" name="email" type="text" placeholder="email" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="image" name="image" type="file" class="form-control input-md">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input id="vacpost" name="vacpost" type="button" class="form-control input-md">
</div>
</div>
</fieldset>
</form>
<script>
$('#comid').on('input',function(e){
$.ajax({
type: "POST",
url: "postvaccode.php",
data: {id:$('#comid').val()},
success: function (response) {
var partsOfStr = response.split(',');
$('#comname').val(partsOfStr[0]);
$('#email').val(partsOfStr[1]);
}
});
});
</script>
<script>
$("form#data").submit(function(){
var formData = new FormData($(this)[0]);
$.ajax({
url: "postvaccode3.php",
type: 'POST',
data: formData,
async: false,
success: function (data) {
alert(data)
},
cache: false,
contentType: false,
processData: false
});
return false;
});
</script>
postvaccode3.php
<?php
include('dbconnection.php');
if(isset($_POST['comid'])) {
$comid = $_POST['comid'];
$cato = $_POST['cato'];
$loc = $_POST['loc'];
$qul = $_POST['qul'];
$indate = $_POST['indate'];
$expdate = $_POST['expdate'];
$gpa = $_POST['gpa'];
$des = $_POST['des'];
$title = $_POST['title'];
$comname = $_POST['comname'];
$email=$_POST['email'];
$sql="insert into vacancy(companyid,catogary,location,qulification,indate,expectgpa,description,title,expdate,company_name,email,image) values($comid,'$cato','$loc','$qul','$indate','$gpa','$des','$title','$expdate','$comname','$email')";
if(mysqli_query($conn,$sql)){
echo "great";
}
else{
echo "not great";
}
}
?>
So I have this form:
<form method="post" id="login-nav">
<div class="form-group">
<label class="sr-only" for="exampleInputEmail2">Email address</label>
<input type="email" id="email" name="email" class="form-control" id="exampleInputEmail2" placeholder="Email address" required>
</div>
<div class="form-group">
<label class="sr-only" for="exampleInputPassword2">Password</label>
<input type="password" id="PassWord" name="PassWord" class="form-control" id="exampleInputPassword2" placeholder="Password" required>
<div class="help-block text-right">Zaboravili ste password?</div>
</div>
<button id="submitit" onclick="GetUser();" class="btn btn-primary btn-block">Sign in</button>
<div class="checkbox">
<label>
<input type="checkbox"> keep me logged-in
</label>
</div>
</form>
And when I press on the Sign in button, I send an post and I have my HTML updated correctly for a brief second, and then the page reloads. This is not what I want. I dont want the page to reload, but stay as it is after my ajax call. Here is the javascript/jquery code:
function GetUser() {
var email = $("#email").val();
var password = $("#PassWord").val();
$.post('../phpdatabase/getlogin.php',{ email: email , password : password }, function(data){
$('#LogNav').html(data);
});
}
The default behaviour of a submit button is to submit the form, so you have to prevent it.
onclick="GetUser(event);"
This will pass the event to the function callback. Insde your function you have to use:
function GetUser(e) {
e.preventDefault();
I would suggest you to change the event from the button to the form submit event:
$("#login-nav").on("submit", function(e) {
var email = $("#email").val();
var password = $("#PassWord").val();
$.post('../phpdatabase/getlogin.php',{ email: email , password : password }, function(data){
$('#LogNav').html(data);
});
e.preventDefault();
});
Avoid using inline events into your HTML elements e.g. onclick, onfocus etc...
add type of the button which is <button type="button"></button>. Because by default it will assume button type submit if it's under form tag.
<form method="post" id="login-nav">
<div class="form-group">
<label class="sr-only" for="exampleInputEmail2">Email address</label>
<input type="email" id="email" name="email" class="form-control" id="exampleInputEmail2" placeholder="Email address" required>
</div>
<div class="form-group">
<label class="sr-only" for="exampleInputPassword2">Password</label>
<input type="password" id="PassWord" name="PassWord" class="form-control" id="exampleInputPassword2" placeholder="Password" required>
<div class="help-block text-right">Zaboravili ste password?</div>
</div>
<button type="button" id="submitit" onclick="GetUser(e);" class="btn btn-primary btn-block">Sign in</button>
<div class="checkbox">
<label>
<input type="checkbox"> keep me logged-in
</label>
</div>
</form>
Also you can prevent it by passing event into the function and preventing it in the javascript function. like this
function GetUser(e) {
e.preventDefault();
I know that there has been a couple of threads like that, I have looked through them and I can't seem to be able to find an answer. So I have a HTML form with a PHP file action and method POST. Here is the form:
<div class="col-md-4 col-sm-12">
<div class="contact-form bottom">
<h2>Send a message</h2>
<form id="main-contact-form" name="contact-form" method="POST" action='http://creobox.ie/site/sendemail.php'>
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
</div>
</div>
Please note, I tried using action ="sendemail.php" with no effect.
Then I am trying to GET the data in the PHP file, like so:
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
The rest of the code is just composing the e-mail etc - this part works fine.
Am I missing something very simple? The code looks to be fine! I did echo on $name, $from, $subject and $message and all I got was
Details : <br/>
No errors showing up or anything... Thanks for any help!
Ok, so I tried to go with the AJAX... I modified my form to be
<div class="contact-form bottom">
<h2>Send a message</h2>
<form id="main-contact-form" name="contact-form">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit" href="javascript: void(0);" onclick = "sendMail();">
</div>
</form>
</div>
and created a js function
function sendMail()
{
var name = document.getElementById('name');
var email = document.getElementById('email');
var message = document.getElementById('message');
$.ajax({
type: 'POST',
url: 'sendemail.php',
data: {
'name' : name,
'email' : email,
'message' : message
},
success: function(json) {
if (json.status == 'S')
alert('Duplicates removed!');
},
error: function() {}
});
}
But that doesn't work either... I just get a 304 - Not Modified error with a cached index page. This is starting to burn my brain! Could I be using an old version of jquery, hence all the problems?!