Jump to content

Check if multiple variables are set


esoteric

Recommended Posts

I have a form with multiple options to fill out (its a admin form) i want to make sure only one of the of the options are set, so im checking if the variable is empty with

 

if ($variable1 != NULL) {

$new_variable = "yep, this option was filled out";

}

 

so if the form sent anything from the form with the value 'variable1' it then sets a new variable ($new_variable)

 

Problem is i have multiple fields so i need to make sure if it finds a a variable is not empty, then too check if the others have anything in them too. If it does find another (meaning more than one has been filled out) echo an error (echo "you must only complete one option..")

 

What is the best way to check if more than one variable exists? Sorry if i explained it complicated.

Thank you

Link to comment
Share on other sites

well the value has to be a set value, the value will change and is used elsewhere in the script, im trying the following but is it the best way to do things?

 

what  its doing is checking if the field 'offerValue' has been set

 

 

if ($offerValue != NULL) {

if (($offerPercent == NULL) && ($offerPrice == NULL) && ($ShippingPrice == NULL)) {

$offer = "OFFER_VALUE";

}elseif (!isset($offer))

{

echo "You must only use one type of offer!" ;

}

}

 

Link to comment
Share on other sites

As titan21 stated you should be using a Radio Group. This requires the user to select one, and only one, value. No matter which one is selected, the value is passed as the same field name. But, you would still need to validate that the value passed is one of the "approved" values. But, that is a trivial exercise. You can compare against an array of valid values or do a DB query.

 

You are making this much harder than it needs to be.

Link to comment
Share on other sites

Here is a rough example. Probably not the best implementation for your needs because I don't know enough about your current code or implementation

 

Radio group options

Select an offer:<br>
<input type="radio" name="offer" value="OFFER_VALUE">Offer Value<br>
<input type="radio" name="offer" value="OFFER_PERCENT">Offer Percent<br>
<input type="radio" name="offer" value="OFFER_PRICE">Offer Price<br>
<input type="radio" name="offer" value="SHIPPING_PRICE">Shipping Price<br>

 

PHP code to validate

$valid_offers = array('OFFER_VALUE', 'OFFER_PERCENT', 'OFFER_PRICE', 'SHIPPING_PRICE');

if(isset($_POST['offer']) && in_array($_POST['offer'], $valid_offers))
{
    $offer = $_POST['offer'];
}
else
{
    echo "You must select a valid offer.";
}

Link to comment
Share on other sites

Thank you for your example. The problem with that is the fields will contain different values. This script is too add special offers on my website, so for example i have 4 fields,

discount percentage

discount cash value

set cash value

shipping value

 

So depending on the offer one of these fields will be filled out. Lets say i want to make a special offer on a item and make it cost £10. I would then type 10.00 in the box labelled 'set cash value'

 

Now the variable for 'set cash value' would be set too '10.00' but i want to make sure the others are blank, if there not tell the user not too fill in more than one option.

Link to comment
Share on other sites

In fact it could be even simpler than that, Just add a textbox and compare the value from "offer" and use that to determine how to use "details". A small addition to mjdamato's code:

 

Select an offer:<br>
<input type="radio" name="offer" value="OFFER_VALUE">Offer Value<br>
<input type="radio" name="offer" value="OFFER_PERCENT">Offer Percent<br>
<input type="radio" name="offer" value="OFFER_PRICE">Offer Price<br>
<input type="radio" name="offer" value="SHIPPING_PRICE">Shipping Price<br>

<input type="text" name="details"/>

 

and the PHP side:

 

$details = $_POST['details'];

switch($_POST['offer'])
{
   case "OFFER_VALUE":
   //do something with $details
   break;

   case "OFFER_PERCENT":
   //do something with $details
   break;

   case "OFFER_PRICE":
   //do something with $details
   break;

   case "SHIPPING_PRICE":
   //do something with $details
   break;
}

 

 

Link to comment
Share on other sites

I think you are still making this more difficult than it needs to be. If I understand you correct you have four different options of which the user will enter an amount, correct? Then just give the user a radio button group to select the offer and one field to enter the amount or have the four radio button options with a text field for each one. Then you only need to check the text field associated with the offer selected.

Capture.jpg

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.