esoteric Posted October 13, 2011 Share Posted October 13, 2011 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 Quote Link to comment Share on other sites More sharing options...
titan21 Posted October 13, 2011 Share Posted October 13, 2011 We can't see the form - but sounds like you may want to consider posting the data via radio buttons? This forces the user to only select one option. Quote Link to comment Share on other sites More sharing options...
esoteric Posted October 13, 2011 Author Share Posted October 13, 2011 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!" ; } } Quote Link to comment Share on other sites More sharing options...
titan21 Posted October 13, 2011 Share Posted October 13, 2011 try [m=http://uk3.php.net/manual/en/function.isset.php]isset()[/m]? Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 13, 2011 Share Posted October 13, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 13, 2011 Share Posted October 13, 2011 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."; } Quote Link to comment Share on other sites More sharing options...
esoteric Posted October 13, 2011 Author Share Posted October 13, 2011 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. Quote Link to comment Share on other sites More sharing options...
titan21 Posted October 13, 2011 Share Posted October 13, 2011 You could combine the radio buttons with the input fields. Use the radio buttons to determine whether a field is enabled or not, that way a user is only able to fill out one field and you'll know which field that is by the value of the radio button. Quote Link to comment Share on other sites More sharing options...
titan21 Posted October 13, 2011 Share Posted October 13, 2011 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; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 13, 2011 Share Posted October 13, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.