Laravel Search not working with error massage - php

What i need is use this search which has a selectable dropdown.see the following screenshot.
These selected data are comes under trainee_division which is in registerdetails data table.here is the controller function i`m developing.
$query = $request->search;
$queryType = $request->institute;
$items = DB::table('registerdetails');
if($queryType == 'Operation' || $queryType == 'operation' ){
$items = $items->where('Operation', '=',"%$queryType%");
}
$items = $items->get();
return view('registeredusers.admindivisiondetails')->with('items',$items);
Relevant view is like this
<form action="divisiondetailsSearch" method="post" class="form-inline">
<select name="institute" id="institute">
<option selected="selected" value="Operation">Operation</option>
<option value="NPA">NPA</option>
<option value="BTS-Kurunegala">BTS-Kurunegala</option>
<option value="INOC">INOC</option>
<option value="RNO">RNO</option>
<option value="Implementation">Implementation</option>
<option value="RAN">RAN</option>
<option value="CEE">CEE</option>
<option value="BTS-Jaffna">BTS-Jaffna</option>
<option value="BTS-Colombo">BTS-Colombo</option>
<option value="Transmission">Transmission</option>
<option value="BTS-Rathnapura">BTS-Rathnapura</option>
<option value="IBS">IBS</option>
<option value="NS">NS</option>
<option value="PCN">PCN</option>
<option value="SQ">SQ</option>
<option value="Pro-Transmission">Pro-Transmission</option>
<option value="BTS-Kandy">BTS-Kandy</option>
</select>
<input type="hidden" value="{{ csrf_token() }}" name="_token" />
<input type="submit" name="submit" value="Search">
</form>
i`m getting this error.
can anyone help me to get solved this one please.

change the query to
if($queryType == 'Operation' || $queryType == 'operation' ){
$items = $items->where('traainee_division', 'like',"%$queryType%");
}

Please check you table first it seems like there is no Operation Column in table..
it might be help you.

As per your comment you need like query to traainee_division column
$items = $items->where('traainee_division','like',"%$queryType%");
note :
your matching value with value . you need to match value with table column value using like query above .

Related

HTML Multiple Select form not working

I am new in PHP.
Please check the following code; multiple select is not working. Not sure why. Thanks in advance.
<form action="test.php">
<select name="fin[]" multiple ="multiple" class = "multiple_select" id = "selected_options_fin" style="height: 200px">
<option value="week_period">Week Period</option>
<option value="comments_approved">Comments Approved</option>
<option value="comment_replies">Comment Replies</option>
<option value="avarage_response_time">Average Response Time</option>
<option value="avarage_response_time_in_minutes">Average Response Time in Minutes</option>
<option value="commenter_visits">Commenter Visits</option>
<option value="percentage_of_new_visits">% New Visits</option>
<option value="number_of_goal_completions"># of Goal Completions</option>
<option value="conversions_rate">Conversions Rate</option>
</select>
<input type="submit" name = "submit1" Value = "SUBMIT" id="submit_fin">
</form>
<?php
if (isset($_POST['fin'])) {
$fin_array = $_POST['fin'];
$loop_count = 0;
foreach ($fin_array as $key => $value) {
$loop_count++;
echo 'Column $loop_count || Array Key = $key || Value = $value<br />';
}
exit();
}
Sorry for bothering
First line need to have
form action="test.php" method="post"

PHP How can I sort results with accumulation in the URL by using GET method

I am working on a sort function which is supposed to sort my results using two different html forms. The first form will be for sorting A-Z, Z-A, Price-Asc, Price-Desc. The second one will be used to show different number of results per page. My question is how can I make these form to work "together". For example on "change" event in the first form I will get something like "?orderBy="A-Z" in the URL. Then If I decide to change the number of results per page I want to keep the current GET parameter in the url and add another one like "?orderBy="A-Z?perPage=12". I saw this kind kind of functionality in a website and I need to make the same one but I am not sure about the concepts and how this thing is supposed to work. Every answer and shared thoughts are much appreciated !
<form method="get" id="form_sort">
<select name="sort" onchange="form_sort.submit()">
<option value="">Please Choose...</option>
<option value="name">name</option>
<option value="price">price</option>
</select>
</form>
<form method="get" id="form_order">
<select name="order" onchange="form_order.submit()">
<option value="">Please Choose...</option>
<option value="asc">ASC</option>
<option value="desc">DESC</option>
</select>
</form>
<form method="get" id="form_limit">
<select name="limit" onchange="form_limit.submit()">
<option value="">Please Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
A form will automatically build a query string for you (using GET). If you can keep the selected values selected when the page loads, then every time you update the form the same values will remain selected.
Here's an example:
<form method="get">
<select name="sort">
<option value="">Please Choose...</option>
<option value="name"<?=isset($_GET['sort']) && $_GET['sort']=='name' ? ' selected' : ''?>>name</option>
<option value="price"<?=isset($_GET['sort']) && $_GET['sort']=='price' ? ' selected' : ''?>>price</option>
</select>
<select name="order">
<option value="">Please Choose...</option>
<option value="asc"<?=isset($_GET['order']) && $_GET['order']=='asc' ? ' selected' : ''?>>ASC</option>
<option value="desc"<?=isset($_GET['order']) && $_GET['order']=='desc' ? ' selected' : ''?>>DESC</option>
</select>
<select name="limit">
<option value="">Please Choose...</option>
<option value="1"<?=isset($_GET['limit']) && $_GET['limit']==1 ? ' selected' : ''?>>1</option>
<option value="2"<?=isset($_GET['limit']) && $_GET['limit']==2 ? ' selected' : ''?>>2</option>
<option value="3"<?=isset($_GET['limit']) && $_GET['limit']==3 ? ' selected' : ''?>>3</option>
</select>
<input type="submit">
</form>

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
?>

How to get 'select' value and put into 'input' value?

I have a for with a select dropdown option. I want to get the value that is selected and put it into a hidden input value. I'm trying this at the minute -
echo '<form method="post" action="cart_update.php">';
echo"<select name='qty'>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>";
echo '<input type="hidden" name="product_qty" value="'.$_GET['qty'].'" />';
echo"</form>;
When I submit this to cart-update.php it tells me that qty is an undefined index on the hidden input line.
Can anyone see the problem?
You will have to use javascript or jQuery. Some sample jQuery code:
$('select').change(function() {
var val1 = $('select').val();
$('your selector').val(val1);
});
Consider this instead:
$arr = array(1,2,3,4,5,6,7,8,9,10);
foreach($arr as $n){
$selected = $n == $_POST['qty'] ? 'selected' : '';
echo "<option value='$n' $selected>$n</option>";
}
Based on #Undefined_variable answer:
/***************************/
jQuery( document ).ready(function( $ ) {
/* selecy key from list , insert into field */
$('#select_start_key').change(function(){
var val1 = $('#select_start_key').val();
$('#start_key').val(val1);
/*reset the select to none*/
$('#select_start_key').val("");
});
});
View example :
https://jsfiddle.net/alinrzv/aj619Lbb/
A few problems:
1. Your form is in POST method, and you are trying to get the value in "Get" method ($_GET)
2. The value of $_GET will be available only after submitting the form.
You should use javascript to get the value. something like that:
<form method="post" action="cart_update.php">
<select name='qty' onChange='document.getElementById("product_qty").value = this.value'>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>
<input type="hidden" id="product_qty" name="product_qty" value="1" />

Using $_POST to get select option value from HTML

I use select as below:
<select name="taskOption">
<option>First</option>
<option>Second</option>
<option>Third</option>
</select>
How do I get the value from the select option and store it into a variable for future use, in PHP?
Use this way:
$selectOption = $_POST['taskOption'];
But it is always better to give values to your <option> tags.
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
You can access values in the $_POST array by their key. $_POST is an associative array, so to access taskOption you would use $_POST['taskOption'];.
Make sure to check if it exists in the $_POST array before proceeding though.
<form method="post" action="process.php">
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
<input type="submit" value="Submit the form"/>
</form>
process.php
<?php
$option = isset($_POST['taskOption']) ? $_POST['taskOption'] : false;
if ($option) {
echo htmlentities($_POST['taskOption'], ENT_QUOTES, "UTF-8");
} else {
echo "task option is required";
exit;
}
You can do it like this, too:
<?php
if(isset($_POST['select1'])){
$select1 = $_POST['select1'];
switch ($select1) {
case 'value1':
echo 'this is value1<br/>';
break;
case 'value2':
echo 'value2<br/>';
break;
default:
# code...
break;
}
}
?>
<form action="" method="post">
<select name="select1">
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
</select>
<input type="submit" name="submit" value="Go"/>
</form>
for php8+ versions, you can use match expression:
$select = $_POST['select1'] ?? '';
$result = match ($select) {
'value1' => 'this is value1',
'value2' => 'this is value2',
default => 'unknown value',
};
echo $result;
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
$var = $_POST['taskOption'];
Depends on if the form that the select is contained in has the method set to "get" or "post".
If <form method="get"> then the value of the select will be located in the super global array $_GET['taskOption'].
If <form method="post"> then the value of the select will be located in the super global array $_POST['taskOption'].
To store it into a variable you would:
$option = $_POST['taskOption']
A good place for more information would be the PHP manual: http://php.net/manual/en/tutorial.forms.php
Like this:
<?php
$option = $_POST['taskOption'];
?>
The index of the $_POST array is always based on the value of the name attribute of any HTML input.
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
try this
<?php
if(isset($_POST['button_name'])){
$var = $_POST['taskOption']
if($var == "1"){
echo"your data here";
}
}?>
-- html file --
<select name='city[]'>
<option name='Kabul' value="Kabul" > Kabul </option>
<option name='Herat' value='Herat' selected="selected"> Herat </option>
<option name='Mazar' value='Mazar'>Mazar </option>
</select>
-- php file --
$city = (isset($_POST['city']) ? $_POST['city']: null);
print("city is: ".$city[0]);

Categories