I'm having problems with a simple contact form.
The error I'm getting is:
Notice: Undefined index: terms in contact.php on line 29
Attention! You have to check the Privacy Policy box to accept our terms.
Code:
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$terms = $_POST['terms']; // <---- line 29
if(trim($terms) == '') {
echo '<div class="alert error"><div class="msg">Attention! You have to check
the Privacy Policy box to accept our terms.</div></div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="alert error"><div class="msg">Attention! Please enter your
message.</div></div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
The template file is below:
<form action="<?php echo $this->config->get('config_url').'ajax/contact.php' ?>" method="post" id="contact_form" enctype="multipart/form-data">
<table>
<tr>
<td>
<label for="name"><?php echo $entry_name; ?></label>
<input type="text" name="name" id="name" style="margin-right:20px" value="<?php echo $name; ?>" />
<?php if ($error_name) { ?>
<span class="error"><?php echo $error_name; ?></span>
<?php } ?>
</td>
<td>
<label for="email"><?php echo $entry_email; ?></label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
<?php if ($error_email) { ?>
<span class="error"><?php echo $error_email; ?></span>
<?php } ?>
</td>
</tr>
</table>
<label for="enquiry"><?php echo $entry_enquiry; ?></label>
<textarea name="enquiry" id="enquiry" cols="30" rows="5"><?php echo $enquiry; ?></textarea>
<?php if ($error_enquiry) { ?>
<span class="error"><?php echo $error_enquiry; ?></span>
<?php } ?>
<input type="checkbox" name="terms" value="<?php echo $terms; ?>" />
<label for="terms">Tick this box to confirm you comply with our Privacy Terms</label>
<input type="submit" id="submit" value="<?php echo $button_continue; ?>" class="button dark-bt" />
</form>
Try this:
$terms = isset($_POST['terms']) ? $_POST['terms'] : '';
You will get the checkbox field only if it is checked by the user in the form.
So, always check like this for checkbox and radio buttons.
Related
I have been trying to get this captacha to work for a while but i cant get it to work whatever i try to do.
When i changed !== to just != as someone suggested the form submission will go trough with a "TRUE" response when i press "submit" button for the form EVEN if press the recaptacha box or just ignore pressing it. The output for var_dump($responseKeys) always gives a me "NULL" value also so i cant figure out what is wrong.
can anyone see why it's not working?
**PHP**
// check captcha
if(isset($_POST)){
$captcha=$_POST['g-recaptcha-response'];
$ip = $_SERVER['REMOTE_ADDR'];
$secretkey = "SECRET KEY";
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretkey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) != 1) {
//echo 'TRUE';
//echo "<br>";
//echo var_dump($responseKeys)."<br>";
//exit;
} else {
//echo 'FALSE';
//echo "<br>";
//echo var_dump($responseKeys)."<br>";
// exit;
}
}
HTML
**<div class="span9 page_sidebar registerForm">
<?php
if (isErrors())
{
echo outputErrors();
}
?>
<div class="well">
<p>
<?php echo t('register_intro_text', 'Please enter your information below to register for an account. Your new account password will be sent to your email address.'); ?>
</p>
<form id="regerform" method="post" action="https://<?php echo _CONFIG_SITE_FULL_URL; ?>/register.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" class="form">
<div class="third">
<label for="title">
<?php echo t("title", "title"); ?>:
</label>
<select autofocus="autofocus" tabindex="1" id="title" name="title">
<option value="Mr"><?php echo t('title_mr', 'Mr'); ?></option>
<option value="Mrs"><?php echo t('title_mrs', 'Mrs'); ?></option>
<option value="Miss"><?php echo t('title_miss', 'Miss'); ?></option>
<option value="Dr"><?php echo t('title_dr', 'Dr'); ?></option>
<option value="Pro"><?php echo t('title_pro', 'Pro'); ?></option>
</select>
</div>
<div class="third">
<label for="firstname">
<?php echo t("firstname", "firstname"); ?>:
</label>
<input type="text" tabindex="1" value="<?php echo isset($firstname) ? safeOutputToScreen($firstname) : ''; ?>" id="firstname" name="firstname">
</div>
<div class="thirdLast">
<label for="lastname">
<?php echo t("lastname", "lastname"); ?>:
</label>
<input type="text" tabindex="1" value="<?php echo isset($lastname) ? safeOutputToScreen($lastname) : ''; ?>" id="lastname" name="lastname">
</div>
<div class="clear"></div>
<div>
<label for="emailAddress">
<?php echo t("email_address", "email address"); ?>:
</label>
<input type="text" tabindex="1" value="<?php echo isset($emailAddress) ? safeOutputToScreen($emailAddress) : ''; ?>" id="emailAddress" name="emailAddress">
</div>
<div>
<label for="emailAddressConfirm">
<?php echo t("email_address_confirm", "Email Confirm"); ?>:
</label>
<input type="text" tabindex="2" value="<?php echo isset($emailAddressConfirm) ? safeOutputToScreen($emailAddressConfirm) : ''; ?>" id="emailAddressConfirm" name="emailAddressConfirm">
</div>
<div class="thirdLast">
<label for="username">
<?php echo t("username", "username"); ?>:
</label>
<input type="text" tabindex="3" value="<?php echo isset($username) ? safeOutputToScreen($username) : ''; ?>" id="username" name="username">
</div>
<div class="clear"></div>
<?php if(SITE_CONFIG_REGISTER_FORM_SHOW_CAPTCHA == 'yes'): ?>
<div class="g-recaptcha" data-sitekey="PRIVATE KEY"></div>
<?php endif; ?>
<div class="buttonWrapper">
<button type="submit" name="submit" class="btn btn-primary" tabindex="99"><?php echo t("register", "register"); ?></button>
</div>
<input type="hidden" value="1" name="submitme"/>
</form>
<div class="disclaimer">
<?php echo t('by_clicking_register_you_agree_to_our_terms', 'By clicking \'register\', you agree to our terms.', array('SITE_CONFIG_PAGE_EXTENSION'=>SITE_CONFIG_PAGE_EXTENSION)); ?>
</div>
<div class="clear"></div>
</div>
**
The method of verifying users is a bit different.
g-recaptcha-response is sent through a POST request to your server-side script,
and along with your reCAPTCHA key that you get when you sign up,
you pass that along to Google to tell if the user is a bot or not.
The response is a JSON response so we will be using file_get_contents() and json_decode() to fetch the response and decide what to do from there with our script.
This example will give you an idea to write your own code.
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
//verify your site
$data = array(
'secret' => 'YOUR_SECRET',
'response' => $_POST["g-recaptcha-response"]
);
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "<p>You are a bot! Go away!</p>";
} else if ($captcha_success->success==true) {
echo "<p>You are not not a bot!</p>";
}
Here i have numerous input fields in a form, which are working perfectly. And now, i want to add new input boxes and their labels according to property_type , Which is already exist on this page.
Now, Kindly Suggest me How can i add new fields in that form.
Here is my Form Code
<div class="dashboard_price_left">
<h3>
<?php
if ($this->lang->line('BasePrice') != '') {
echo stripslashes($this->lang->line('BasePrice'));
} else
echo "Base Price";
?>
</h3>
<p>
<?php
if ($this->lang->line('Atitleandsummary') != '') {
echo stripslashes($this->lang->line('Atitleandsummary'));
} else
echo "Set weekly or monthly price guests will see for your listing.";
?>
</p>
</div>
<?php
if ($listDetail->row()->home_type == 'Office') {
} else if ($listDetail->row()->home_type == 'House') {
}
?>
<form id="pricelist" name="pricelist" action="site/product/savePriceList" method="post">
<div class="dashboard_price_right">
<label><?php
if ($this->lang->line('Pernight') != '') {
echo stripslashes($this->lang->line('Pernight'));
} else
echo "Per night";
?>
</label>
<div class="amoutnt-container">
<?php if ($currentCurrency == '') { ?>
<span class="WebRupee"><?php echo $currencyDetail->row()->currency_symbols; ?></span>
<?php } else { ?>
<span class="WebRupee"><?php echo $currentCurrency; ?></span>
<?php } ?>
<input type="text" id="price" value="<?php
if ($listDetail->row()->price != '0.00') {
echo intval($listDetail->row()->price);
}
?>" class="per_amount_scroll" name="price" onkeypress="return onlyNumbersWithDot(event);" onchange="javascript:Detailview(this,<?php echo $listDetail->row()->id; ?>, 'price');" />
<input type="hidden" id="id" name="id" value="<?php echo $listDetail->row()->id; ?>" />
</div>
<div class="dashboard_currency">
<label><?php
if ($this->lang->line('Currency') != '') {
echo stripslashes($this->lang->line('Currency'));
} else
echo "Currency";
?>
</label>
<div class="select select-large select-block">
<select name="currency" id="currency" onchange="javascript:Detailview(this,<?php echo $listDetail->row()->id; ?>, 'currency');get_currency_symbol(this)" >
<!--<option value="">select</option>-->
<?php foreach ($currencyDetail->result() as $currency) { ?>
<option value="<?php echo $currency->currency_type; ?>" <?php if ($listDetail->row()->currency == $currency->currency_type) echo 'selected="selected"'; ?>><?php echo $currency->currency_type; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
</form>
Here is a pic of my Property prices form
Note: There are two types of property that are following:
1. Office
2. House
on the basis of these property, I want to add more fields in my form.
Please suggest me, Thanks in Advance :)
you can change it base on conditon and pass hidden field which indicates you office or house.
<?php
if ($listDetail->row()->home_type == 'Office')
{
<input type="hidden" name="home_type" value="Office" />
<input type="text" name="per_day" value="" />
<input type="text" name="half_day" value="" />
<input type="text" name="per_hour" value="" />
}
else if ($listDetail->row()->home_type == 'House')
{
<input type="hidden" name="home_type" value="House" />
<input type="text" name="per_night" value="" />
<input type="text" name="per_day" value="" />
<input type="text" name="per_hour" value="" />
}
?>
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've got a slight problem with my CI2.0.2 application.
From what I can tell, it is not getting any post data from a form and so is failing to run an update function, though most pages work fine.
The form is as follows:
<?php echo form_open('job/update/', array('id' => 'update', 'name' => 'update')); ?>
<div class="images">
<?php echo img('application/images/updateJob.png');?>
</div>
<h1>Update Job</h1>
<br><br><br><br>
<div class="fieldset">
<?php
if (isset($error))
{
echo '<div id ="error">';
echo '<h2>Error:</h2>';
echo '<p>'.$error.'</p>';
echo '</div>';
}
?>
<input type="hidden" name="c" id='c' value="<?php if(!empty($view[0])) { echo $view[0]; } else { echo "0"; } ?>">
<div class="left">
<h4>Job Details:</h4>
<div>
<label for="job_number">Job No:</label>
<input type="text" name="job_number" id="job_number" value="<?php echo $job['jobno']; ?>" disabled="disabled">
</div>
<div>
<label for="date">Date:</label>
<input type="text" name="date" id="date" value="<?php echo $job['datetime']->format('d-M-Y'); ?>">
</div>
<div>
<label for="council">Council:</label>
<input type="text" name="council" id="council" value="<?php echo htmlspecialchars($job['council']); ?>"> *
</div>
<div>
<label for="dano">DA No:</label>
<input type="text" name="dano" id="dano" value="<?php echo htmlspecialchars($job['dano']); ?>">
</div>
<div>
<label for="client">Client:</label>
<input type="text" name="client" id="client" value="<?php echo $job['clientid']; ?>" disabled="disabled">
</div>
</div>
<div class="right" style="margin-left: 390px;">
<h4>Location:</h4>
<label for="street_no">Street No:</label>
<input type="text" name="street_no" id="street_no" value="<?php echo $address['streetno']; ?>"> *
<br>
<label for="street_name">Street Name:</label>
<input type="text" name="street_name" id="street_name" value="<?php echo $address['streetname']; ?>"> *
<br>
<label for="street_type">Street Type:</label>
<select name="street_type" id="street_type">
<?php
foreach ($street_types as $type)
{
echo '<option';
if ($type == $address['streettype']) echo ' selected="selected"';
echo '>'.$type.'</option>';
}
?>
</select> *
<br>
<label for="suburb">Suburb:</label>
<input type="text" name="suburb" id="suburb" value="<?php echo $address['suburb']; ?>"> *
<br>
<label for="postcode">Postcode:</label>
<input type="text" name="postcode" id="postcode" value="<?php echo $address['postcode']; ?>"> *
<br>
<label for="state">State:</label>
<input type="text" name="state" id="state" value="<?php echo $address['state']; ?>"> *
<br>
<label for="country">Country:</label>
<input type="text" name="country" id="country" value="<?php echo $address['country']; ?>">
<br>
<label for="dp">DP:</label>
<input type="text" name="dp" id="dp" value="<?php echo $address['dp']; ?>">
<br>
<label for="lot_no">Lot No:</label>
<input type="text" name="lot_no" id="lot_no" value="<?php echo $address['lotno']; ?>">
<br>
<label for="po_box">PO Box:</label>
<input type="text" name="po_box" id="po_box" value="<?php echo $address['pobox']; ?>">
</div>
<div>
<input type="submit" id="submit" name="submit" value="Submit" class="button">
<p>* = Required fields</p>
</div>
</div>
<?php echo form_close();?>
And the update section of the controller is as follows:
/**
* Update an existing job.
* #param int $job_number The number of the job to update.
*/
public function update($job_number=0)
{
$this->load->model('Job_model');
$job = array();
$address = array();
// Get post data.
$job['joblocation'] = '';
$job['jobno'] = $this->input->post('job_number');
$job['datetime'] = new DateTime($this->input->post('date'));
$job['dano'] = $this->input->post('dano');
$job['council'] = $this->input->post('council');
echo $job['jobno'];
echo $job['dano'];
echo $job['council'];
$address['streetno'] = $this->input->post('street_no');
$address['streetname'] = $this->input->post('street_name');
$address['suburb'] = $this->input->post('suburb');
$address['country'] = $this->input->post('country');
$address['postcode'] = $this->input->post('postcode');
$address['state'] = $this->input->post('state');
$address['dp'] = $this->input->post('dp');
$address['lotno'] = $this->input->post('lot_no');
$address['pobox'] = $this->input->post('po_box');
$address['streettype'] = $this->input->post('street_type');
echo "here2";
if (isset($_POST['submit']))
{
$this->Job_model->update($job);
echo "here";
redirect('job/');
}
// Otherwise, get the data from the database.
else
{
$job = $this->Job_model->search($job_number);
$job = $job[0];
$job['datetime'] = new DateTime($job['datetime']);
$address = $this->Job_model->get_address($job['joblocation']);
$address = $address[0];
}
// Get the street types.
$street_types = array();
$this->load->model('staff_model');
$streets = $this->staff_model->get_street_types();
foreach ($streets as $street)
{
$street_types[$street['streettype']] = $street['streettype'];
}
// Load the client list.
$clients = array();
$this->load->model('client_model');
$people = $this->client_model->get_client_person_list();
$companies = $this->client_model->get_client_company_list();
// Allocate view data.
$viewdata = array();
$viewdata['job'] = $job;
$viewdata['street_types'] = $street_types;
$viewdata['address'] = $address;
$viewdata['people'] = $people;
$viewdata['companies'] = $companies;
$this->layout->view('job/update', $viewdata);
}
Any ideas why this might be happening?
Thanks in advance,
James
Try changing the quotes you're using on this line:
<input type="hidden" name="c" id='c' value="<?php if(!empty($view[0])) { echo $view[0]; } else { echo '0'; } ?>" />
The way you had it with the double quotes around the 0 you were breaking that php because the first one would be closing the opening value quote.