Form field input empty than do not update - php

I have a form that updates a user's info. How can I tell the form to NOT update certain fields when left blank?
if(trim($email) == '') { /* don't update */ }

This is better:
if ( empty( trim( $email ) ) )
{
// do not update
}
else
{
// update
...
}

if(trim($email) == '') {}else{ YOUR UPDATE SCRIPT HERE }

Related

PHP How to make IF based on $ret value

I have a form in PHP and if user put correct coupon code something like that is defined:
$ret['status'] = 'success';
and next is:
if( $ret['status'] == 'success' ){
$ret['coupon-id'] = $query->post_id;
$ret['amount'] = $post_option['coupon-discount-amount'];
$ret['type'] = $post_option['coupon-discount-type'];
$discount_text = '';
if( $ret['type'] == 'percent' ){
$discount_text = $post_option['coupon-discount-amount'] . '%';
}else{
$discount_text = gdlr_lms_money_format($post_option['coupon-discount-amount']);
}
$ret['message'] = sprintf(__('You got %s discount', 'gdlr-lms'), $discount_text);
}
And the bottom of the form there is submit button with this conditions:
if( empty($_POST['first_name']) || empty($_POST['last_name']) || empty($_POST['email']) ){
$ret['status'] = 'failed';
$ret['message'] = __('Please fill all required fields.', 'gdlr-lms');
I need to add to this condition that correct coupon is also required. What I need to add to this code to have it done? I have been looking for answers for 5 hours and I tried everything what I have found but nothing work - please help me! :)
This is the website with form - after registration form there will be the entry form to the course I am ask about.
http://semcamp.university/course/starting-company-all-you-need-to-know-to-start-company-2-2-2-2/
You can login in using username: stackoverflow and password: stack1 and valid copuon code is 1234 or test1
Your question is not very clear about in which if statement should the coupon be validated. But, supposing that it needs to be validated in the last one (the if for the submit button), you could do it that way:
if( empty($_POST['first_name']) || empty($_POST['last_name']) || empty($_POST['email']) ){
$ret['status'] = 'failed';
$ret['message'] = __('Please fill all required fields.', 'gdlr-lms');
}
elseif( $ret['status'] != 'success') {
$ret['status'] = 'failed';
$ret['message'] = __('Please provide a valid coupon.', 'gdlr-lms');
}

Form validation server side in wordpress

I am new to wordpress. I have a custom contact form in frontend and i have to validate the data.
Will I have to make validation class or is there any hooks provided by wp.
Regardless of what you use WordPress for, there’s a range of common functions people need their site to perform that aren’t bundled with WordPress. This leaves you with two choices, installing a plugin or creating it yourself.
I am validating form data with my own class wrapper. following are some of the methods you can use :
function handleContactForm() {
if($this->isFormSubmitted() && $this->isNonceSet()) {
if($this->isFormValid()) {
$this->sendContactForm();
} else {
$this->displayContactForm();
}
} else {
$this->displayContactForm();
}
}
public function sendContactForm() {
}
function isNonceSet() {
if( isset( $_POST['nonce_field_for_submit_contact_form'] ) &&
wp_verify_nonce( $_POST['nonce_field_for_submit_contact_form'], 'submit_contact_form' ) ) return true;
else
return false;
}
function isFormValid() {
//Check all mandatory fields are present.
if ( trim( $_POST['contactname'] ) === '' ) {
$error = 'Please enter your name.';
$hasError = true;
} else if (!filter_var($_POST['contactemail'], FILTER_VALIDATE_EMAIL) ) {
$error = 'Please enter a valid email.';
$hasError = true;
} else if ( trim( $_POST['contactcontent'] ) === '' ) {
$error = 'Please enter the content.';
$hasError = true;
}
//Check if any error was detected in validation.
if($hasError == true) {
echo $error;
return false;
}
return true;
}
function isFormSubmitted() {
if( isset( $_POST['submitContactForm'] ) ) return true;
else return false;
}
if you try to use contact form 7 plugin, then you have validation plugin available for this,i.e Jquery Validation For Contact Form 7
or try
http://code-tricks.com/contact-form-7-custom-validation-in-wordpress/

I Have A Registration Form That Is Kicking Back Custom Made Errors Before Submission

I'm working on a registration for my classifieds (via a tutorial) but I'm having problems. For some reason, just visiting this page: http://classifieds.your-adrenaline-fix.com/register.php
will generate the 2 custom errors you'll see in a red box above the registration form BUT the form hasn't even been submitted yet so I don't know why this is happening. If anyone could shed some light on this I'd be most appreciative and I thank you all in advance!!
(I've been staring at it for hours)
Here's the code that validates and submits the form data;
<?php
if(empty($_POST) === false) {
$VisitorsFirstName = $_POST['First_Name'];
$VisitorsLastName = $_POST['Last_Name'];
$VisitorsEmail = $_POST['E_mail'];
$VisitorsPassword = $_POST['Pass'];
$RequiredFlds = array('First_Name', 'Last_Name', 'E_mail', 'Pass', 'PassAgain');
foreach($_POST as $key=>$value) {
if(empty($value) && in_array($key, $RequiredFlds) === true) {
$Err[] = 'All Fields Are Required';
break 1;
}
}
if(empty($Err) === true) {
if(email_exists($VisitorsEmail) === true) {
$Err[] = 'The Email Address \''. $VisitorsEmail. '\' Is Already In Use.';
}
if(strlen($VisitorsPassword) < 4) {
$Err[] = 'Please Select A Password of At Least 4 Characters.';
}
if($_POST['Pass'] !== $_POST['PassAgain']) {
$Err[] = 'Passwords Do Not Match.';
}
if(filter_var($VisitorsEmail, FILTER_VALIDATE_EMAIL) === false) {
$Err[] = 'A Valid Email Address is Required';
}
}
}
if(isset($_GET['success']) && empty($_GET['success'])) {
echo 'You Have Now Been Registered and Can Proceed to Creating Your First Ad<br>(Use the Email and Password That You Registered With to Login)';
} else {
if(empty($_POST) === false && empty($Err) === true) {
$register_data = array (
'VisitorsFirstName' => $_POST['First_Name'],
'VisitorsLastName' => $_POST['Last_Name'],
'VisitorsPassword' => $_POST['Pass'],
'VisitorsEmail' => $_POST['E_mail'],
'Notify' => $_POST['Notify']
);
register_func($register_data);
header('Location: register.php?success');
exit();
} else if(empty($Err) === false) {
echo output_error($Err);
}
}
?>
Upon putting just the code you provided on my own server by itself, it works as designed. It isn't running the top block of code, because the $_POST variable is empty. Try outputting the contents of $_POST at the top of the file so you can figure out why it isn't empty.
print_r($_POST);
Try this:
if(!empty($_POST['First_Name']) && !empty($_POST['Last_Name']) && !empty($_POST['E_mail']) && !empty($_POST['Pass'])){
....
}
OR
try isset($_POST['First_Name'])......
This works fine for me!
You could instead check if the submit button was pressed. Like this:
if (isset($_POST['submit']) {
// get the post values
}
This way you could eliminate the script launching before the form was actually submitted. Right now it seems to run as soon as I visit the page.

PHP- Validate on certain fields

I've a form,in the form only few fields are mandatory.When a fields is not mandatory,it should not check for empty data validation.If the non mandatory field contain data then it shoudl check the data,validation should happen only when data present.
My below code check for all fields.For eg:Say phone is not mandatory in below code,how to change the code.
$validate = array(
array($x, '/^[a-z\d ]{4,20}$/i', "Please enter valid name."),
array($y, '/^[a-z\d ]{4,20}$/i', "Please enter a real category."),
array($phone, '/^\(?[0-9]{3}\)?|[0-9]{3}[-. ]? [0-9]{3}[-. ]?[0-9]{4}$/' , "Please enter a valid phone number")
);
$error = '';
foreach ($validate as $validation)
{
if (!preg_match($validation[1],$validation[0]))
{
$error .= $validation[2];
}
}
if($error != '')
{
echo $error;
exit;
}
Comment on this post,if it is not clear.
Thanks in advance!
If you want to check if at least required fields have been submitted, you can check whether they have been set and have a truthy value using the isset and empty functions.
For example:
if ( isset($_POST['username'], $_POST['password']) &&
! empty($_POST['username']) && ! empty($_POST['password']) ) {
// validation here
}
That would check if the username and password fields were submitted and have a value, whereas for example an email field wouldn't necessarily have been filled in for the above condition to return true.
If you know the mandatory fields before hand I'll suggest you group them in an array and test for based on the current key. If a key is in not mandatory but holds value you can test otherwise do your regular check.
$error = '';
$mandatoryFields = array('key1', 'key2' 'key3');
foreach ($validate as $validation)
{
if (!in_array($validation[0], $mandatoryFields) && strlen(trim($validation[0])) > 0)
{
// There is data in a non mandatory field.
// Perform your test.
}
else {
// Do your regular test.
if (!preg_match($validation[1],$validation[0]))
{
$error .= $validation[2];
}
}
}
You can give this a try.

PHP conditional on query string with id

I have this code:
if('open' == $post->comment_status || !isset($_GET['wp_accept_favor']))
{
// show comment form
}
else
{
// dont show comment form
}
domain.com/?wp_accept_favor then show comment form
domain.com/?wp_accept_favor=2 then dont show comment form
How do I do that? Thanks
if('open' == $post->comment_status ||
(isset($_GET['wp_accept_favor']) && empty($_GET['wp_accept_favor'])) )
{
// show comment form
}
else if(isset($_GET['wp_accept_favor']) && !empty($_GET['wp_accept_favor']))
{
// do not show comment form
}

Categories