PHP How to make IF based on $ret value - php

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');
}

Related

Gravity Forms regex for US ZIP code

I'm trying to set up some form validation for a Gravity Form that I've created. One of the fields that I need to validate is a US ZIP code. I want to pass ZIPs that follow the nnnnn and nnnnn-nnnn patterns. Here's my code:
if ( $field->type == 'address' ) {
$zip = rgar( $value, $field->id . '.5' );
if ( preg_match( "(^(?!0{5})(\d{5})(?!-?0{4})(|-\d{4})?$)", $zip ) && ! $field->get_input_property( '5', 'isHidden' )
) {
$result['is_valid'] = false;
$result['message'] = empty( $field->errorMessage ) ? __( 'Please enter a valid ZIP code (ie. 00000 or 00000-0000).', 'gravityforms' ) : $field->errorMessage;
} else {
$result['is_valid'] = true;
$result['message'] = '';
}
}
My form continues to fail validation and I can't figure out why. I've double checked that .5 is the correct input field number of the ZIP code. Any suggestions?
My form can be found at http://marcusjones.wpengine.com/
shouldn't be easier to use:
/(^\d{5}$)|(^\d{5}-\d{4}$)/
or other function fe:
function isValidPostalCode(postalCode, countryCode) {
switch (countryCode) {
case "US":
postalCodeRegex = /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/;
break;
default:
postalCodeRegex = /^(?:[A-Z0-9]+([- ]?[A-Z0-9]+)*)?$/;
}
return postalCodeRegex.test(postalCode);
}
and "if" you'll add quite simple.

IF statements with OR operator not working

if($action == "send"){
$_POST['name'] = $name ;
$_POST['email'] = $email ;
$_POST['phone'] = $phone ;
if(!empty($name) || !empty($email) || !empty($phone)){
.....
} else {
$msg = 'All fields required';
}
//whatever I do only shows $msg.
//already tried that too
if(!empty($_POST['name']) || !empty($_POST['email']) || !empty($_POST['phone'])){
....
}
What Im trying to do is a form that email me the data, and I want all fields to be filled so maybe Im writing the if statement the wrong way.
sorry if I didnt explained well before.
Your code reads:
If name is not empty, or email is not empty, or phone is not empty
This means that as long as at least one of them are non-empty, then you're good!
Pretty sure that's not what you meant. You want:
If name is not empty, AND email is not empty, AND phone is not empty
Use && instead of || and it should just work!
I think you're getting confused by all the negatives involved here. I suspect what you're after is:
if (!(empty($name) || empty($email) || empty($phone))) {
...
} else {
$msg = 'All fields required';
}
Which would be better written (in my opinion) as:
if (empty($name) || empty($email) || empty($phone)) {
$msg = 'All fields required';
} else {
...
}
if($name=='' || $email=='' || $phone=='')
{
$msg='All fields required';
}
else
{
..............
}

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.

Form field input empty than do not update

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 }

Categories