Form validation for multiselect dropdown list in codeigniter - php

i have tried using various methods for setting form validation using codeigniter for a multiselect dropdown list,but is unable to set the rules properly. under here is the code i am using,kindly help. p.s. i have already tried implementing codes suggested on stackoverflow but in vain.
<select class="" name="travel_cat[]" multiple="multiple" id="travel_lst" data-placeholder="Select Travel Categories" style="width: 100%;">
<option value="adventure">Adventure</option>
<option value="arts&culture">Arts & Culture</option>
<option value="events">Events</option>
<option value="backpacking">Backpacking</option>
<option value="beach_holidays">Beach Holidays</option>
<option value="budget_travel">Budget Travel</option>
<option value="city_travels">City Travels</option>
<option value="day_trips">Day Trips</option>
<option value="honeymoons">Honeymoons</option>
<option value="family_trips">Family Trips</option>
<option value="weekend_gateway">Weekend Gateway</option>
<option value="history&architecture">History & Architecture</option>
<option value="luxury">Luxury</option>
<option value="nature">Nature</option>
<option value="road_trips">Road Trips</option>
<option value="wildlife">Wildlife</option>
<option value="religious">Religious</option>
</select>
$choice = $this->input->post("travel_cat");
if(is_null($choice))
{
$choice = array();
}
$travel_cat = implode(',', $choice);
form validation:-
$this->form_validation->set_rules('travel_cat', 'Travel Category', 'callback_check_default');
function check_default($array)
{
foreach($array as $element)
{
if($element == '0')
{
return FALSE;
}
}
return TRUE;
}

i have replaced the callback function with the below mentioned code,and it worked fine.
function check_default()
{
$choice = $this->input->post("travel_cat");
if(is_null($choice))
{
$choice = array();
}
$travel_cat = implode(',', $choice);
if($travel_cat != '')
return true;
else
return false;
}

Related

Laravel Sort shoplist front end

Hi i'm doing a shop for a university project. This is my first course in web development and I don't understand how to sort a shoplist page by an html dropdown select in laravel.
Frontend Blade
<div class="toolbar-sorter">
<span>Sort By</span>
<select name="sorter" class="sorter-options" style="width:150px; "data-role="sorter">
<option selected="selected" value='comic_name'>Titolo: A-Z </option>
<option value='comic_name'> Titolo: Z-A </option>
<option value='price'> Prezzo: Crescente </option>
<option value='price'> Prezzo: Decrescente </option>
<option value='created_at'> Ultimi Arrivati </option>
</select>
</div>
Route.php (This is not the only route of shoplist)
Route::get('/shoplist', 'ComicController#shoplistBase');
ComicController
public function shoplistBase()
{
$genres = Genre::all();
$comics = Comic::paginate(9);
return view('shoplist')->with(compact('genres'))->with(compact('comics'));
}
Assuming that you are using the same route for your sorting:
Wrap your drop-down in a form
Make sure to use unique values for each options
<form action="/shoplist" method="GET">
<div class="toolbar-sorter">
<span>Sort By</span>
<select name="sorter" class="sorter-options" style="width:150px; " data-role="sorter">
<option selected="selected" value='comic_name_asc'>Titolo: A-Z</option>
<option value='comic_name_desc'> Titolo: Z-A</option>
<option value='price_asc'> Prezzo: Crescente</option>
<option value='price_desc'> Prezzo: Decrescente</option>
<option value='created_at'> Ultimi Arrivati</option>
</select>
</div>
<button type="submit">Filter</button>
</form>
Handle the filter request in your Controller
public function shoplistBase(Request $request)
{
$genres = Genre::all();
if ($request->has('sorter')){
switch($request->get('sorter')){
case `comic_name_asc`:
$comics = Comic::orderBy('name', 'desc')->paginate(9);
break;
case `comic_name_desc`:
//..
break;
}
} else {
$comics = Comic::paginate(9);
}
return view('shoplist')->with(compact('genres'))->with(compact('comics'));
}

Show or hide HTML code based on URL

I am working on a form and I've stumbled upon a problem. I have different users in which I want them to see different values for one of my SELECT tags; so far I am using PHP to get the URL and if $field == 'donor' then it takes off some of the options. Here's the code:
function defineUser($field){
if(isset($_GET['user']) && urldecode($_GET['user'])){
$field = urldecode($_GET['user']);
if($field == "donor"){
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>Select...</option>
<option value=''>Option 1</option>
<option value=''>Option 2</option>
<option value=''>Option 3</option>
<option value='Other'>Option 4</option>
</select>
</p>";
} else if ($field != "donor") {
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>...</option>
</select>
</p>";
} else {
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>...</option>
</select>
</p>";
}
}
}
This is my function, now on the page source itself I simply have,
<?php echo defineUser($_GET['user']); ?>
My question simply is how can I get each user to see a standard options set - inside my select tag - based on the URL the user has been sent from?
Your code could be simplified:
function defineUser() {
$user = isset($_GET['user']) ? urldecode($_GET['user']) : null;
switch ($user) {
case 'donor':
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>Select...</option>
<option value=''>Option 1</option>
<option value=''>Option 2</option>
<option value=''>Option 3</option>
<option value='Other'>Option 4</option>
</select>
</p>";
break;
default:
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>...</option>
</select>
</p>";
}
}
You don't need to pass it $_GET['user'] since that is globally accessible, unless you're worried about dependency. Second, you were essentially repeating the same block of HTML twice in your if else clause, using a switch here makes more sense.

PHP form with limit some value from html post

my html code :
<form action="myfile.php" method="POST">
Data
<select id="data" name="data" class="form-control" required>
<option value="data1">Data 1</option>
<option value="data2">Data 2</option>
</select>
Value
<select id="value" name="value" class="form-control" required>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</form>
This my php code :
<?php
$data = $_POST['data'];
$value = $_POST['value'];
if code {
} else {
HERE MY PROCCESS
}
?>
How if Data 1 is selected and value is must more than 3,if not it will get notice "Value of Data 1 is must more than 3" and if not error it will return to my proccess
Check:
if($data == "data1" && $value == 1) {
// notice
} else {
// process
}
Try this
if($data == "data1" && $value > 3) {
//everything is fine
} else {
// through notice
}

How I explode this kind of string in PHP?

I have a dropdown to display price ranges for searching.
HTML for dropdown:
<select name="price">
<option value="">All (-)</option>
<option value="">Rs.400 to Rs.1,000 (3)</option>
<option value="">Rs.1,000 to Rs.2,000 (6)</option>
<option value="">Rs.2,000 to Rs.4,000 (1)</option>
<option value="">Rs.4,000+ (1)</option>
</select>
Using this option values, now I need to separate first and second price from user selection.
Eg. If someone select Rs.400 to Rs.1,000 (3), then I need to get 400 and 1,000.
Can anybody tell me how can I do this in php. I tired it with php explode. but I could not figure this out correctly.
Hope somebody may help me out.
Thank you.
What about something like this:
HTML
<select name="price">
<option value="all">All (-)</option>
<option value="400|1000">Rs.400 to Rs.1,000 (3)</option>
<option value="1000|2000">Rs.1,000 to Rs.2,000 (6)</option>
<option value="2000|4000">Rs.2,000 to Rs.4,000 (1)</option>
<option value="Over">Rs.4,000+ (1)</option>
</select>
PHP
<?php
//Get the data from the form
$price = $_POST['price'];
$prices = explode("|", $price);
?>
For example, if the user selects "Rs.400 to Rs.1,000", the value of $prices will be:
$price[0] = "400";
$price[1] = "1000";
I hope that helps. If it doesn't, I suppose I wasn't clear on what you were asking.
Inspired by a previous answer. I'd suggest.
HTML
<select name="price">
<option value="|">All (-)</option>
<option value="400|1000">Rs.400 to Rs.1,000 (3)</option>
<option value="1000|2000">Rs.1,000 to Rs.2,000 (6)</option>
<option value="2000|4000">Rs.2,000 to Rs.4,000 (1)</option>
<option value="4000|">Rs.4,000+ (1)</option>
</select>
PHP
<?php
function get_numeric($val) {
if (is_numeric($val)) {
return $val + 0;
}
return false;
}
$price_selected = isset($_REQUEST['price']) && trim($_REQUEST['price'])!="" ? $_REQUEST['price'] : "|"; // sets a default value
list($lower, $upper) = array_map('get_numeric',explode('|', $price_selected, 2));
var_dump($lower); // false when there's no lower limit
var_dump($upper); // false when there's no upper limit
?>

html select value 0 is in php validation always empty

I try to post the selected value and check if the variable is empty.
html:
<select id="monitors-old" class="form-control" name="monitors-old">
<option value="">Auswählen...</option>
<option value="0" <?php if ($personData["cmo_mon"] == "0"){echo 'selected';}?>>0</option>
<option value="1" <?php if ($personData["cmo_mon"] == "1"){echo 'selected';}?>>1</option>
<option value="2" <?php if ($personData["cmo_mon"] == "2"){echo 'selected';}?>>2</option>
<option value="3" <?php if ($personData["cmo_mon"] == "3"){echo 'selected';}?>>3</option>
<option value="4" <?php if ($personData["cmo_mon"] == "4"){echo 'selected';}?>>4</option>
</select>
Result html:
<select id="monitors-old" class="form-control" name="monitors-old">
<option value="">Auswählen...</option>
<option value="0" selected="">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
POST Check:
if (empty($_POST["monitors-old"])) {
$errors[] = "Alt-Monitore is required.";
die;
} else {
$monitors_old = validateInput($_POST["monitors-old"]);
}
the value 0 is always empty and the script fired the die, all other values are working.
Is the value 0 like ""?
Also tried:
<select id="monitors-old" class="form-control" name="monitors-old">
<option>Auswählen...</option>
<option selected="">0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
This is also working, but the same issue. And another question, why is the selected allocated to ="" ? I thought it is only a tag for html, that this value is the selected?
validateInput:
function validateInput($value) {
$value = trim($value);
$value = stripslashes($value);
$value = htmlspecialchars($value);
return $value;
}
The empty() function will return TRUE if the value is even 0.
So you should use the isset() function and != operation for checking
if(isset($_POST["monitors-old"]) and $_POST["monitors-old"]!=''){
// code here
}
else{
// code here
}

Categories