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.
Related
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.
I want to check form fields on PHP. I have tried but I don't get any message. I am not sure why.
I use mongodb database, and I'm new in PHP.
My PHP code so far:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post">
<h3> Name :</h3>
<p>
<input type="text" name="username" id="title/">
</p>
<h3>Title :</h3>
<p>
<input type="text" name="title" id="title/">
</p>
<h3>Content :</h3>
<textarea id="content" name="content" rows="20" cols="100"></textarea>
<p>
<br/>
<input type="submit" name="btn_submit" value="Save"/>
</p></form>
</center>
<?php
if (isset($_POST['btn_submit']))
{
$title = $_POST['title'];
$content = $_POST['content'];
if (!isset($title) || empty($title))
{
echo " <h2> check title ! </h2>";
}
if (!isset($content) || empty($content))
{
echo " <h2> check content ! </h2>";
}
}
?>
<?php
else: ?>
<p>
success. _id:<?php echo $article['_id']; ?>.
<a href="blogpost.php">
write other articles ?</a>
</p>
<?php endif; ?>
Just reviewed your code. I think it has a few problems, but the main one is in the if-else sequence.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post">
<h3> Name :</h3>
<p>
<input type="text" name="username" id="title/">
</p>
<h3>Title :</h3>
<p>
<input type="text" name="title" id="title/">
</p>
<h3>Content :</h3>
<textarea id="content" name="content" rows="20" cols="100"></textarea>
<p>
<br/>
<input type="submit" name="btn_submit" value="Save"/>
</p></form>
</center>
<?php
if (isset($_POST['btn_submit']))
{
$title = $_POST['title'];
$content = $_POST['content'];
if (!isset($title) || empty($title))
{
echo " <h2> check title ! </h2>";
}
if (!isset($content) || empty($content))
{
echo " <h2> check content ! </h2>";
}
?>
<?php } else { ?>
<p>
<?php echo $article['_id']; ?>.
<a href="blogpost.php">
write other articles ?</a>
</p>
<?php } ?>
You have syntax error in if else statement and also there is undefined varibale $article['_id']. Where do you get that value from? Other than that the code is fine. Here is the updated code:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post">
<h3> Name :</h3>
<p>
<input type="text" name="username" id="title/">
</p>
<h3>Title :</h3>
<p>
<input type="text" name="title" id="title/">
</p>
<h3>Content :</h3>
<textarea id="content" name="content" rows="20" cols="100"></textarea>
<p>
<br/>
<input type="submit" name="btn_submit" value="Save"/>
</p></form>
</center>
<?php
if (isset($_POST['btn_submit']))
{
$title = $_POST['title'];
$content = $_POST['content'];
if (!isset($title) || empty($title))
{
echo " <h2> check title ! </h2>";
}
else if (!isset($content) || empty($content))
{
echo " <h2> check content ! </h2>";
}
else
{
echo "<p>Success!!! <a href='blogpost.php'>
write other articles ?</a> </p>";
}
}
?>
i have a cms which creates pages in the form of index.php?p=PAGENAME
for example:
index.php?p=01Home
index.php?p=news
index.php?p=about
or whatever pagename
now what i want to do is to get the PAGENAME as a variable
for example
$page="01home";
$page="news";
$page="about";
i tried to get the pagename via $_GET[p] from my cms code, but that doesnt work, any other ideas? The code must be independent from the cms so i can use the pagename in a plugin
PLUGIN code where i want o use the pagename
<?php
$amount= $cfr ;
$page = $_GET['p'];
if (!isset($_GET["action"])) {
}
elseif ($_GET["action"] == "write") // etc.
{
header('location: succes.php');
$data = ''.PHP_EOL;
$file=fopen("plugins/comments/comments/$page/comments.txt",'a');
$message=str_replace("\r",'',$message);
$message=str_replace("\n",'{{',$_GET['message']);
fwrite($file,$_GET['name'].'||'.date('d-m-Y').'||'.$message.$data);
fclose($file);
}
$start=(isset($_GET['start'])?$_GET['start']:0);
$gastenboek=Array();
$gastenboek=file("plugins/comments/comments/$page/comments.txt");
?>
<br>
<fieldset class="comments">
<legend class="pm">Reacties</legend>
<div style="float:right;">Aantal reacties: <b><?php echo min($start+$amount,sizeof($gastenboek)); ?></b></div>
<TABLE class="comments" width="100%">
<?PHP
$gastenboek=array_reverse($gastenboek);
for ($i=$start;$i<$start+$amount && $i<sizeof($gastenboek);$i++) {
list($name,$date,$message)=explode('||',$gastenboek[$i]);
$message=str_replace('{{',"\n",$message);
echo '<TR><TD class="comments"><div style="float:left;">
<B>'.$name.'</B></div><div style="float:right;">
(<i>'.$date.'</i>)</div><br><p>'.str_replace("\n",'<BR>',htmlspecialchars($message)).'</p> </TD></TR>'."\n";
}
?>
</TABLE>
</fieldset>
<br>
<fieldset class="pm">
<legend class="pm">Plaats een reactie</legend>
<FORM action="index.php" method="GET" onSubmit="return validate(this);" name="comments" id="comments">
<INPUT type="hidden" name="action" value="write">
<p class="pm">Naam: </p>
<INPUT type="text" name="name" size="30" style="width:300px;" required><br>
<p class="pm2">Reactie: </p>
<TEXTAREA class="message" name="message" id="message" cols="46" rows="5" required></TEXTAREA><br>
<INPUT type="submit" value="Reactie toevoegen">
</FORM>
</fieldset>
<script type="text/javascript">
$(function(){
$('#comments').ebcaptchaword();
});
</script>
The url being - index.php?p=01Home
Try this -
$page = $_GET['p'];
If it says ?p=pagename that should mean you should use $_GET['p']... or I'm not getting this problem right.
Script1 is based on CSS and html design(with some texture effect) I tried to setup php form action script to a html/css form however it keeps refreshing to the home page.. according to script if successful then it will refresh to thankyou.php, but it is not success.
However script2, I don't use any css, it is very basic html but it is working fine! but since my whole website design have css effect, I want the form also needs to have some css work.
Could someone please help me with script1? if it not possible then could you please suggest me an action script for below form, currently I don't have any action.php script, I tried few in online, unfortunately without success.
CSS form script:
<div class="row add-bottom-main">
<form name="myform" id="contactForm" action="" method="post">
<article class="span6">
<textarea id="msg" rows="3" cols="40" name="message" placeholder="Message">Message</textarea>
</article>
<article class="span6">
<input size="100" type="text" name="name" id="name" placeholder="Name">
<input type="text" size="30" id="email" name="email" placeholder="email">
<button type="submit" name="submit" id="submit" class="btn btn-renova-alt add-top-half">Send Message</button>
</article>
</form>
</div>
Script 1
<div class="row add-bottom-main">
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="myform" id="contactForm" action="" enctype="multipart/form-data" method="post">
<article class="span6">
<textarea id="msg" rows="3" cols="40" name="message" placeholder="Message">Message</textarea>
</article>
<article class="span6">
<input size="100" type="text" name="name" id="name" placeholder="Name">
<input type="text" size="30" id="email" name="email" placeholder="email">
<button type="submit" name="submit" id="submit" class="btn btn-renova-alt add-top-half">Send Message</button>
</article>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill again.";
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("niranjan.thampu#gmail.com", $subject, $message, $from);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=thankyou.php">';
exit;
}
}
?>
</div>
SCRIPT 2
<div class="row add-bottom-main">
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="myform" id="contactForm" action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill again.";
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("niranjan.thampu#capital-placement.com", $subject, $message, $from);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=thankyou.php">';
exit;
}
}
?>
</div>
Your "css form" as posted has no action set on the form and no PHP to process the page in the event that it's submitted. Your two other forms both have PHP that is testing to see if the form has been submitted and process it when it has. The PHP on those two pages is doing the work of processing the form. Without it, they would also not do anything when submit.
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.