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 )
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 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";
}
}
?>
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?!
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) {...}
Ok So I have a basic login page with a form:
<form name="register" class="form-horizontal" id="signup" onsubmit="return validateForm(this);" action="check.php">
<div class="control-group input-append">
<label for="text" class="control-label">Name</label>
<div class="controls">
<input id="name" name="name" type="name" placeholder="Enter your name"/>
</div>
</div>
<br>
<div class="control-group input-append">
<label for="passwd" class="control-label">Password</label>
<div class="controls">
<input id="pass" name="pass" type="pass" placeholder="Enter your password"/>
</div>
</div>
<br>
<div class="control-group">
<div class="controls">
<div class="left1">
<input type="submit" name="submit" class="btn btn-success"/>
</div>
</div>
</div>
<br>
</form>
On clicking submit I need to post the name and password to check.php. I do not want the browser to refresh the page to check.php.
It should get the resulting JSON array, parse it and based on that redirect if the login is successful.
Right now i can either post or get but not both. And I don't want the browser to refresh the page to check.php.
UPDATE:
I'm still not getting this to work. Have a look please.
<form class="form-horizontal" id="login-form">
<div class="control-group input-append">
<label for="text" class="control-label">Email</label>
<div class="controls">
<input name="email" id="email" type="email" value="" placeholder="Enter your email"/>
</div>
</div>
<br>
<div class="control-group input-append">
<label for="passwd" class="control-label">Password</label>
<div class="controls">
<input name="passwd" id="passwd" type="password" value="" placeholder="Enter your password"/>
</div>
</div>
<br>
<div class="control-group">
<div class="controls">
<div class="left1">
<input type="button" id="login" value="Login" class="btn btn-success"/>
</div>
</div>
</div>
<br>
</form>
</div>
</div>
</body>
<script>
$('#login').click(function(){
$.ajax({
url:"login.php",
type:'POST',
data: $("#login-form").serialize()
}).done(function(data){
alert(JSON.stringify(data));
});
}
});
</script>
TRY THIS ONE
add jquery-1.9.1.js
<form class="form-horizontal" id="login-form">
<div class="control-group input-append">
<label for="text" class="control-label">Email</label>
<div class="controls">
<input name="email" id="email" type="email" value="" placeholder="Enter your email"/>
</div>
</div>
<br>
<div class="control-group input-append">
<label for="passwd" class="control-label">Password</label>
<div class="controls">
<input name="passwd" id="passwd" type="password" value="" placeholder="Enter your password"/>
</div>
</div>
<br>
<div class="control-group">
<div class="controls">
<div class="left1">
<input type="button" id="login" value="Login" class="btn btn-success"/>
</div>
</div>
</div>
</form>
<script>
$('#login').click(function(){
$.ajax({
url:"login.php",
type:'POST',
dataType:"json",
data: $("#login-form").serialize()
}).done(function(data){
var json_text = JSON.stringify(data, null, 2);
obj = JSON.parse(json_text);
if(obj.status == 'done')
alert('you are logged in');
});
});
</script>
In the login page use like this
<?php
$login = $_POST['email'];
$pass = $_POST['passwd'];
// LOGIN CHECKING
$data['status'] = 'done';
echo json_encode($data);
?>
Since you dont want the page to reload, i would go for ajax posting this to the php-script that handles the login.
<form id="login-form">
<input type="text" name="user_name" value=""/>
<input type="password" name="password" value=""/>
<input type="button" id="login" value="Login"/>
</form>
Then i would suggest you use jquery to make the ajax-handling easier. And it should look something like this:
$('#login').click(function(){
$.ajax({
url: PHP_PAGE_THAT_HANDLES_LOGIN.php,
type:'POST',
data: $("#login-form").serialize()
}).done(function(data){
/* Here you will recieve the data that
* has been returned from the php page.
* so if its a success you can redirect
* here with a window.location = "www.site.com/welcome.php";
*/
});
}
});
The attributes added in the ajax call under the param data will be recieved in the php page as $_POST["user_name"] and $_POST["password"]