Ajax not displaying validation messages - php

I have got a PHP contact form where I'm trying to implement Ajax to it to make it more user friendly because the contact form is a pop-up form using javascript/jQuery, so everytime the form is submitted it refreshes the page and you have to go back in the form to view the validation message.
Could you check what am I missing here please? I've been on it for sometime now and it still doesn't work. Please let me know if you want me to add the PHP script.
Ajax code:
jQuery(document).ready(function ($) {
$("#ajax-contact-form").submit(function () {
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "demoform.php",
data: str,
success: function (msg) {
if (msg == 'OK') {
result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
$("#fields").hide();
} else {
result = msg;
}
$("#note").html(result);
}
});
return false;
});
});
HTML FORM
<center>
<p>Please complete the form below <br />to request a <br/> <span id="free_demo">FREE DEMO</span></p>
<div id="note"></div>
<div id="fields">
<form id="ajax-contact-form" enctype="multipart/form-data">
<div><input type="text" placeholder="Name" name="fullname" id="vpbfullname" value="<?php echo strip_tags($_POST["fullname"]); ?>" class="vpb_input_fields"> <?php echo $submission_status1; ?></div>
<div><input type="text" placeholder="E-Mail" name="email" id="email" value="<?php echo strip_tags($_POST["email"]); ?>" class="vpb_input_fields"><?php echo $submission_status2; ?></div>
<div><input type="text" placeholder="Company" name="company" id="company" value="<?php echo strip_tags($_POST["company"]); ?>" class="vpb_input_fields"><?php echo $submission_status3; ?></div>
<div><input type="text" placeholder="Telephone" name="phone" id="telephone" value="<?php echo strip_tags($_POST["phone"]); ?>" class="vpb_input_fields"><?php echo $submission_status4; ?></div>
<div class="vpb_captcha_wrappers"><input type="text" placeholder="Security Code" id="vpb_captcha_code" name="vpb_captcha_code" class="vpb_input_fields"> </div>
<br/><br/><br/>
Refresh Security Code</font>
<br/><br/>
<div class="vpb_captcha_wrapper"><img src="vasplusCaptcha.php?rand=<?php echo rand(); ?>" id='captchaimg' ></div><br clear="all"><br/>
<br clear="all">
<div><?php echo $submission_status; ?></div><!-- Display success or error messages -->
<div> </div>
<input type="hidden" id="submitted" name="submitted" value="1">
<input type="submit" class="vpb_general_button" value="Submit"></form> </div>
<br clear="all">
</center>
Any help would be great.. Thanks

Use preventDefault();
$("#ajax-contact-form").submit(function (e) {
e.preventDefault();
//....
};
This is preventing the form submit, but ajax call will happens.

you have to write onsubmit="return false" in your form tag like

Related

Submit to same page with an anchor

I am trying to understand how forms work , so far I understood that I could submit the form and refresh to the same page through
> action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
and then catch the errors or not have blank inputs.
However, let's say I have anchors in the page (sections in other words like #ContactUs) how could I refresh the page using action to get to that specific place instead of going back to top of page?
Thanks for all in advance
Here is part of the code:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<h2>SEND US A MESSAGE!</h2>
<span>We'd be happy to hear from you.</span>
<input name="contactname" placeholder="Name" type="text" value="<?php echo $contactname;?>"/> <span class="error"> <?php echo $contactnameErr;?></span>
<input name="contactemail" placeholder="Email" type="text" value="<?php echo $contactemail;?>" /><span class="error"> <?php echo $contactemailErr;?></span>
<input name="contactphone" placeholder="Phone #" type="text" value="<?php echo $contactphone;?>" /><span class="error"> <?php echo $contactphoneErr;?></span>
<input name="contactsubject" placeholder="Subject" type="text" value="<?php echo $contactsubject;?>" /><span class="error"> <?php echo $contactsubjectErr;?></span>
<textarea name="contactmessage" placeholder="Message"><?php echo $contactmessage;?></textarea><span class="error"> <?php echo $contactmessageErr;?></span>
<input type="submit" name="submit" value="Send" class="contact-button" />
</form>
You should have a validation for it for example
<?php
if(isset($_POST['submit'])){
//then do something
header("Location: #contactus");
}
?>
So this way even if you load, it does not give you empty and does not ask you to resubmit
You can Use jQuery as i given below
<?php
if(isset($_POST['submit']))
{
//
//sql query and display
//
?>
<script>
$(function() {
$('html, body').animate({
scrollTop: $("#contactus").offset().top
}, 2000);
});
</script>
<?php
}
?>

How to send two different actions in a form, with one submit button [duplicate]

This question already has answers here:
JavaScript: send multiple submitions
(4 answers)
Closed 8 years ago.
I have one actions that sends info to the journals.php and the second actions sends info to the uploads.php file. How can i do this with one submit button.
If you could show an example that would be awesome :)
<form id="login" action="journal.php?journal=journals&id=<?php echo $opened['id']; ?>" method="post" role="form">
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" name="title" id="title" value="<?php echo $opened['title']; ?>" placeholder="Title">
</div>
<div class="form-group">
<label for="body">body</label>
<textarea class="form-control" name="body" id="body" rows="14" placeholder="body"><?php echo $opened['body']; ?></textarea>
</div>
<button type="submit" id="loginSubmit" class="btn btn-default">Save</button>
<input type="hidden" name="submitted" value="1">
<?php if(isset($opened['id'])) { ?>
<input type="hidden" name="id" value="<?php echo $opened['id']; ?>">
<?php } ?>
</form>
<form action="uploads.php" enctype="multipart/form-data">
<input type="file" name="file">
</form>
I have now tried to make this script.
<script type="text/javascript">
$(document).ready(function() {
$("#Login").click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "uploads.php",
datatype: "html",
data: $("#loginSubmit").serialize(),
success: function(data) {
}
});
$("#loginSubmit").submit();
});
});
</script>
You can submit the text form with AJAX, wait for it to come back successfully, and then submit the file form regularly.
OR
You can make a single action file and include journal.php and uploads.php in it.

How can I show PHP echo in DIV through jQuery without page refresh

I would like to make the PHP echo I have appear in the div "preview". I want this because I don't want the page to refresh but to make the div "formbox" disappear and to show the echo in the "preview" div.
Does someone know what I am doing wrong?
HTML
<div id="preview"> </div>
<div id="formbox">
<form id="form" method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="City">City:</label>
<input type="text" name="City" id="City" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
</div>
jQuery
$('document').ready(function(){
$('#form').ajaxForm( {
target: '#preview',
success: function() {
$('#formbox').slideUp('fast');
}
});
});
PHP
if ($success){
echo "<h1>Thank You!";
}
Just use the result.
$('document').ready(function(){
$('#form').ajaxForm( {
target: '#preview',
success: function( result ) {
$('#formbox').slideUp('fast');
$('#preview').html(result);
}
});
});

PHP page not saving form and parsing output

I have this PHP page that grabs the response's from a form (well i hope it does) and then inputs the data into a table. I then echo the response from the table on the same page. Im using ajax on the form page to send over the form values and on success of the ajax call load the data into a div. this is all done without a refresh, however no information is being sent and it just refresh's the page
my php page -
<?php
$comment_name = $_POST["name"];
$comment_body = $_POST["comment"];
$film_no = $_POST["hidden"];
echo $comment_name;
echo $comment_body;
echo $film_no;
// Connects to your Database
mysql_connect("localhost", "****", "****") or die(mysql_error());
mysql_select_db("ignitet1_CheckFilm") or die(mysql_error());
$query1 = "INSERT INTO film_comments (comments_id,film_no,name,comment)
VALUES ('','$film_no', '$comment_name','$comment_body')";
$runquery1 = mysql_query($query1)or die(mysql_error());
$getComments = mysql_query("SELECT * FROM film_comments where film_no = '$film_no'")
or die(mysql_error());
while($info = mysql_fetch_array($getComments))
{
echo "<p>";
echo $info['name'];
echo ":</p>";
echo "<p>";
echo $info['comment'];
echo "</p>";
echo "<p>Comment posted on:";
echo $info['timestamp'];
echo "</p>";
echo "</div>";
?>
my form and javascript code
<form id="ContactForm2" onsubmit="return submitForm()" >
<div> <input type="hidden" name="hidden" id="hidden" value="2">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" />
<label for="body">Comment Body</label>
<textarea name="comment" id="comment" cols="20" rows="5"></textarea>
<input type="submit" id="comment" class="button" value="Submit" />
<div class="form_result"> </div>
</form> </div>
<script>
function submitForm() {
$.ajax({type:'POST', url: 'comment.php', data:$('#ContactForm2').serialize(), success: function(response) {
$('#ContactForm2').find('.form_result').html(response);
}});
}
Everything i try seems to not work. help would be much appreciated! :)
Try this with your php its working fine for me.
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#sub').click(function(){
$.ajax({type:'POST', url: 'comment.php', data:$('#ContactForm2').serialize(), success: function(response) {
$('#ContactForm2').find('.form_result').html(response);
}});
});
})
</script>
<body>
<div>
<form id="ContactForm2"> > <input type="hidden" name="hidden" id="hidden" value="2">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" />
<label for="comment">Comment Body</label>
<textarea name="comment" id="comment" cols="20" rows="5"></textarea>
<input type="button" id="sub" class="button" value="Submit" />
<div class="form_result"> </div>
</form> </div>
</body>
</html>

Delay wordpress comment form post with jQuery to allow analytics to send tracking.

I am trying to put a custom event on my Wordpress comments form and would like to track an event every time someone clicks the submit button. The problem I am having is the form submits and creates a page request and the Google analytics code doesn't have time to work.
I need to delay the the form submit so the tracking has time to work. I have looked up similar problems and tried various solutions but cant seem to get them to work. Any help would be greatly appreciated.
My code is as follows, with my attempt to delay the form unsuccessfully.
<form id="commentsForm" action="<?php echo get_option('siteurl'); ?>/wp-comments- post.php" method="post" id="commentform">
<!-- Delay form submit to allow GA code to send -->
<script type="text/javascript">
$(document).ready(function(){
$('#commentsForm').submit(function (e) {
var form = this;
e.preventDefault();
setTimeout(function () {
form.submit();
}, 1000); // in milliseconds
});
});
</script>
<?php if ( is_user_logged_in() ) : ?>
<p>Logged in as <? php echo $user_identity; ?>. Log out ยป</p>
<?php else : ?>
<p class="textfield">
<input name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" size="22" tabindex="1" type="text" <?php if ($req) echo "aria-required='true'"; ?>>
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label> </p>
<p class="textfield"><input type="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?>>
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>
<p class="textfield"><input type="text" name="url" id="url" value="<?php echo esc_attr($comment_author_url); ?>" size="22" tabindex="3">
<label for="url"><small>Website</small></label></p>
<?php endif; ?>
<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->
<p class="text-area"><textarea name="comment" id="comment" cols="58" rows="10" tabindex="4"></textarea></p>
//Submit button
<p><input class="submitButton" onClick="_gaq.push(['_trackEvent','Comment','Submit', URL]);" name="submit" type="submit" id="submit" tabindex="5" value="" />
<?php comment_id_fields(); ?>
</p>
<?php do_action('comment_form', $post->ID); ?>
</form>
I think it might not work as the form is a PHP form, but I'm not sure the best way to get it working.
Thanks for any help or thoughts.
You may be having issues of the order in which the submit onclick is firing and the form.submit() event fires. Could you try pulling the click event off of your submit button and just placing it in your submit function?
Something like this:
$(document).ready(function(){
$('#commentsForm').submit(function (e) {
var form = this;
e.preventDefault();
_gaq.push(['_trackEvent','Comment','Submit', URL]);
setTimeout(function () {
form.submit();
}, 1000); // in milliseconds
});
});
Also, maybe I missed it in your example, but what is URL?

Categories