Unknown issue with my contact form in wordpress - php

I started working on a wordpress template and I am extremely stuck on this contact form, which I don't seem to find the problem. I must mention that I am somehow new to php, but I can understand pretty fast. This is what I am using for my contact page (there are also other elements but they do not interfere with the code I`m having trouble with):
<?php get_header();
$two2_option = two2_get_global_options();
global $post; setup_postdata($post); ?>
<?php if($two2_option['two2_contact_email']){ ?>
<form method="post" id="ajax_form" action="#">
<fieldset>
<ul class="form-item" id="field-name">
<li>
<label><?php _e("Name","jbirdie"); ?>:<span class="field-required" title="<?php _e("Required Field","jbirdie"); ?>">*</span></label>
<input type="text" maxlength="90" name="name" id="name" placeholder="<?php _e("name","jbirdie"); ?>" size="30" class="form-field required text_input clear" />
</li>
<li>
<label><?php _e("E-mail","jbirdie"); ?>:<span class="field-required" title="<?php _e("Required Field","jbirdie"); ?>">*</span></label>
<input type="email" maxlength="90" name="email" id="email" placeholder="<?php _e("e-mail","jbirdie"); ?>" size="30" class="form-field required email text_input clear" />
</li>
<li>
<label><?php _e("Phone","jbirdie"); ?></label>
<input type="number" maxlength="90" name="phone" id="phone" placeholder="<?php _e("phone number","jbirdie"); ?>" size="30" class="form-field required email text_input clear" />
</li>
<li class="message">
<label><?php _e("Comments","jbirdie"); ?> <span>Available for quotation</span></label>
<textarea cols="55" rows="" name="message" id="message" placeholder="<?php _e("Comments","jbirdie"); ?>" class="form-textarea text_area clear" ></textarea>
</li>
</ul>
<input type="submit" name="submit" id="submit" value="send" class="submit form-submit" />
<div id="result"></div>
</fieldset>
</form>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
</div>
<?php get_footer(); ?>
and the following in functions.php :
add_action('wp_ajax_nopriv_two2_send_contact_form', 'two2_send_contact_form');
add_action('wp_ajax_two2_send_contact_form', 'two2_send_contact_form');
function two2_send_contact_form(){
$two2_option = two2_get_global_options();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$subject = "Message from your portfolio";
$body = "From $name, \n\n$email, \n\n$phone, \n\n$message";
$to = $two2_option['two2_contact_email'];
$result = mail($to, $subject, $body);
if($result){
print "<h3>".__("Success","two2")."</h3><p>".__("Your email has been sent!","two2")."</p>";
} else {
print "<h3>".__("Error","two2")."</h3><p>".__("Try it again later, please.","two2")."</p>";
}
die();
}
...and it`s simply not working...can someone please give me a hint or sth to find the error?

Have you got a script to submit the form to the ajax handler?
e.g.
<script type="text/javascript" >
jQuery(document).ready(function($) {
$('#ajax_form').submit(function(evt){
var form_data = {
action: 'two2_send_contact_form',
name: jQuery('#name').val(),
email: jQuery('#email').val(),
phone: jQuery('#phone').val(),
message: jQuery('#message').val()
};
$.post('<?php echo admin_url('admin-ajax.php'); ?>', form_data, function(response) {
console.log(response);
});
return false;
});
});
</script>
http://codex.wordpress.org/AJAX_in_Plugins
Also there is an extra php closing brace <?php } ?> in your contact form.
Edit
Corrected jQuery selector & ajax url.

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 add simple image captcha with custom forms in wordpress

I am trying to add captcha with my custom form in WordPress. so I use the plugin Securimage-WP CAPTCHA for this. the plugin is working fine but in my page where I need this captcha, I used if(is_user_logged_in()) for displaying different forms for logged in or logged out users. it gives me an error Fatal error: Call to undefined function show_form(). please help me to out from this problem. thanks in advance. my current code is below:
<?PHP
/* Template Name: bbb */
get_header();
if(is_user_logged_in()){
//echo "<script> window.location.href='".site_url()."'; </script>";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$values = array();
$errors = array();
$values['name'] = #trim(stripslashes($_POST['contact_name']));
$values['email'] = #trim(stripslashes($_POST['email']));
$values['message'] = #trim(stripslashes(strip_tags($_POST['message'])));
if (empty($values['name'])) $errors['contact_name'] = 'Please enter your name';
if (!preg_match('/^(?:[\w\d-]+\.?)+#(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $values['email'])) $errors['email'] = 'The email address supplied is invalid';
if (strlen($values['message']) < 20) $errors['message'] = 'Please enter a message longer than 20 characters';
if (sizeof($errors) == 0) {
if (function_exists('siwp_check_captcha')) {
// make sure plugin is enabled before calling function
if (false == siwp_check_captcha($err)) {
$errors['captcha'] = $err;
}
}
}
if (sizeof($errors) > 0) {
show_form($errors, $values);
} else {
// form code goes here, no errors & captcha was correct
echo "<span style='font-size: 1.2em'><strong><em>Congrats, you win the captcha solving challenge!</em></strong>";
}
}//if condition end
else {
show_form();
}
?>
<?php function show_form($errors = array(), $values = array()) { ?>
<?php if (sizeof($errors) > 0): ?>
<p>There was a problem with your submission. Please correct the following errors:</p>
<ul>
<?php foreach($errors as $error): ?>
<li><?php echo $error ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
<div>
<label for="contact_name">Your Name:
<input type="text" name="contact_name" id="contact_name" class="input" value="<?php echo htmlspecialchars(stripslashes(#$values['name'])) ?>" size="20" /></label>
</div>
<br />
<div>
<label for="email">E-mail:
<input type="email" name="email" id="email" class="input" value="<?php echo htmlspecialchars(stripslashes(#$values['email'])) ?>" size="25" /></label>
</div>
<br />
<div>
<label for="message">Message:<br /><pre style="border: 0; margin: 0; padding: 0"><textarea name="message" id="message" class="input" rows="8" style="width: 100%"><?php echo htmlspecialchars(stripslashes(#$values['message'])) ?></textarea></pre></label>
</div>
<br />
<?php echo do_shortcode('[siwp_show_captcha]'); ?>
<p> </p>
<p>
<input type="submit" value="Send Message" />
</p>
</form>
<?php }}//user logged in if end
get_footer(); ?>
<?php
function show_form($errors = array(), $values = array()) { ?>
<?php if (sizeof($errors) > 0): ?>
<p>There was a problem with your submission. Please correct the following errors:</p>
<ul>
<?php foreach ($errors as $error): ?>
<li><?php echo $error ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
<div>
<label for="contact_name">Your Name:
<input type="text" name="contact_name" id="contact_name" class="input" value="<?php echo htmlspecialchars(stripslashes(#$values['name'])) ?>" size="20" /></label>
</div>
<br />
<div>
<label for="email">E-mail:
<input type="email" name="email" id="email" class="input" value="<?php echo htmlspecialchars(stripslashes(#$values['email'])) ?>" size="25" /></label>
</div>
<br />
<div>
<label for="message">Message:<br /><pre style="border: 0; margin: 0; padding: 0"><textarea name="message" id="message" class="input" rows="8" style="width: 100%"><?php echo htmlspecialchars(stripslashes(#$values['message'])) ?></textarea></pre></label>
</div>
<br />
<?php echo do_shortcode('[siwp_show_captcha]'); ?>
<p> </p>
<p>
<input type="submit" value="Send Message" />
</p>
</form>
<?php } ?>
Would you please move this functions in your current theme functions.php ? And after check it. I hope it's working fine for you.

Ajax not displaying validation messages

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

PHP form without opening a new window

Good day/evening. I have this sort of form in HTML:
<form action="scripts/form-testimonial.php" class="form" method="post">
<ul>
<li><label for="name">Name</label> <input class="required" id="namef3" name="name" placeholder="Name" type="text"></li>
<li><label for="email">E-mail</label> <input class="required" id="emailf3" name="email" placeholder="E-mail" type="text"></li>
<li><label for="company">Company</label> <input id="companyf3" name="company" placeholder="Company (if applicable)" type="text"></li>
<li><label for="msg">Message</label> <textarea class="required" cols="43" id="msgf3" name="msg" placeholder="Your feedback" rows="8"></textarea></li>
</ul>
<button class="submit" type="submit">Send your feedback</button>
</form>
and this sort of php sript for this form as a separate file:
<?php
$receip = "office#avehire.co.uk";
$subj = "Testimonial";
$bodymsg = "Name: ".$_POST['name']."\n"
."Company: ".$_POST['company']."\n"
."Email: ".$_POST['email']."\n"
."Message: ".$_POST['msg']."\n";
if(!$_POST['msg']){
header("Location: ../error.html");
exit;
}
$email = $_POST['email'];
if(mail($receip, $subj, $bodymsg, 'From: Contact <'.$email.'>')){
header("Location: ../msg_sent_feedback.html");
}
?>
I also have a validator for this form, but this is not important. What is more important here is the way how this form works. Basically - when you click submit button - it goes to a separate php file, process the form and then - from what is already there - goes to msg_sent_feedback.html file. What I want to do is to NOT opens a separate file but just display a message in the same basic html file where the actual form exists. A message e.g. "Message sent".
Another thing - if I already have a validation script - do I need to have the first "if" in this form? or can I remove it totally? the one which redirects to error.html. Or can I remove these 4 lines? That line - I guess - is responsible for errors in the form, but as I mentioned, I have a validation script. I don't really want to change html. is it possible to modify the php script to work within the html document, as instructed above? thanks
Here is an Jquery ajax example that will help you.
<?php
$receip = "office#avehire.co.uk";
$subj = "Testimonial";
$bodymsg = "Name: ".$_POST['name']."\n"
."Company: ".$_POST['company']."\n"
."Email: ".$_POST['email']."\n"
."Message: ".$_POST['msg']."\n";
if(!$_POST['msg']){
echo "<h1>Error sending e-mail";
exit;
}
$email = $_POST['email'];
if(mail($receip, $subj, $bodymsg, 'From: Contact <'.$email.'>')){
echo "<h1>Email sent successfully</h1>";
}
?>
Html :
<!Doctype html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function submitmyform(){
data=$('#myform').serialize();
$.ajax({
url: "myphpfile.php",
type:'POST',
data:data,
async:false,
dataType:'html',
success: function(msg){
$('#response_goes_here').html(msg);
}
});
}
</script>
<form action="" id="myform">
<ul>
<li> <label for="name">Name</label> <input id="namef3" name="name" type="text" placeholder="Name" class="required"/>
</li>
<li> <label for="email">E-mail</label> <input id="emailf3" name="email" type="text" placeholder="E-mail" class="required"/>
</li>
<li> <label for="company">Company</label> <input id="companyf3" name="company" type="text" placeholder="Company (if applicable)" />
</li>
<li> <label for="msg">Message</label> <textarea id="msgf3" name="msg" cols="43" rows="8" placeholder="Your feedback" class="required"></textarea>
</li>
</ul>
<button type="button" class="button" onclick="submitmyform();">Send your feedback</button>
</form>
<div id="response_goes_here"></div>

Trying to create a contact form with php in wordpress

I have written code to submit a form and email in php and placed it in my wordpress custom page. I have included the php to send the email, but for some reason nothing gets sent. I am not sure where to start for debugging this thing.
Here is my code for the custom page
<?php
/*
Template Name: page-pickup.php
*/
?>
<?php include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); ?>
<? include_once(ABSPATH. 'wp-content/themes/fiftyfityNew/contact-form.php'); ?>
<?php add_post_meta(7, 'fruit', 'banana', true) or update_post_meta(7, 'fruit', 'banana'); ?>
<?php
get_header(); ?>
<?php
echo do_shortcode( '[contact-form-7 id="23" title="Contact form 1"]' );
?>
<?php
if(isset($GLOBALS["mail_message"])) {
echo "<p>".$GLOBALS["mail_message"]."</p>";
}
if(isset($_SESSION["mail_message"])) {
echo "<p>".$_SESSION["mail_message"]."</p>";
unset($_SESSION["mail_message"]);
}
?>
<form method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<input type="hidden" name="form-name" value="contact" />
<div style="text-align:left; border: 1px solid #bbb; padding:10px;">
<div>
<label for="name">name</label>
<input type="text" name="name" id="name" />
</div>
<div>
<label for="email">email</label>
<input type="text" name="email" id="email" />
</div>
<div>
<label for="message">message</label>
<textarea name="message" id="message" style="width:250px; height:100px"></textarea>
</div>
<input type="submit" value="go" />
</div>
</form>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Here is my code for the included contact-form.php
<?
function send_message()
{
$subject = "Test email form WordPress";
$body = $_POST['message'];
$recipient = "anderskitson#gmail.com";
$success = #wp_mail($recipient, $subject, $body);
if($success) {
session_start();
$_SESSION["mail_message"] = "Thank you. your message has been sent.";
header( 'Location: '.$_SERVER['HTTP_REFERER'] ) ;
}
else {
$GLOBALS["mail_message"] = "Euston, we have a problem.";
}
}
?>
Why are you using CF7 and creating your own form at the same time? CF7 does all the processing for you.

Categories