In laravel how to use if /else in controller - php

I am trying to pass value from view to controller, but it not working it was sent only "APPROVED"
my view
<div class="form-group row">
<label for="colFormLabelLg" class="col-md-2 col-sm-3 control-label control-label-lg"><b>DIGITAL SIGNATURE</b></label>
<div class="col-md-3">
<div class="form-group" id=digital_signature >
<select class="form-control" name="digital_signature" value="{{ old('digital_signature') }}" required autofocus >
<option value=""></option>
<option style="color:green">WITH DIGITAL SIGNATURE</option>
<option style="color:green">WITHOUT DIGITAL SIGNATURE</option>
</select>
</div>
</div>
</div>
my controller
public function new_approvel_update(Request $request, $id)
{
if($request->digital_signature == 'WITH DIGITAL SIGNATURE')
{
$input= Student::Where('delete_status','NOT DELETED')->find($id);
$input['center_approved'] = strtoupper ('APPROVED');
$input['date_of_join'] = $request->date_of_join;
} elseif($request->digital_signature == 'WITHOUT DIGITAL SIGNATURE') {
$input= Student::Where('delete_status','NOT DELETED')->find($id);
$input['center_approved'] = strtoupper ('NOT-APPROVED');
$input['date_of_join'] = $request->date_of_join;
}
$certificate->save();
return redirect('new_application')->with('success',' APPLICATION APPROVED SUCCESSFULLY .');
}

In option tag of select you haven't use value attribute. so it will pass null to a controller.
Change option tags as below :
<option value="APPROVED" style="color:green">WITH DIGITAL SIGNATURE</option>
<option value="NOT-APPROVED" style="color:green">WITHOUT DIGITAL SIGNATURE</option>
Change in your controller :
if($request->digital_signature == 'APPROVED'){
// your code
}
elseif($request->digital_signature == 'NOT-APPROVED'){
// your code
}

Related

Price range filter using CodeIgniter4

I am trying to create a search with multiple filters, one of the filters is a budget or price filter and I am using dropdown instead of creating a price range bar.
In other words, my budget filters are:-
0-2500000
250000-500000
500000-10000000
10000000-25000000
25000000-50000000
50000000 and above
Here is my code and I need help modifying it. The function in Controller and SQL Query in the model only takes a single budget (price) value and displays the result, instead of taking the budget (price) range and showing the result which comes under that budget (price) range.
What I want to achieve is that when the user selects the budget (price) range in the search form, it should display properties that fall under the selected budget range or price range.
HTML Form
<div class="search__area-inner">
<?= form_open('/home/search_residential'); ?>
<div class="row">
<div class="col-12"><?= csrf_field(); ?></div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<select name="budget[]" class="wide select_option">
<option data-display="Select Budget">Select Budget</option>
<option value="0-2500000">less than 25 lacs</option>
<option value="250000-500000">25 to 50 lacs</option>
<option value="500000-10000000">50 lacs to 1 cr</option>
<option value="10000000-25000000">1 cr to 2.5 cr</option>
<option value="25000000-50000000">2.5 cr to 5 cr</option>
<option value="50000000">5 cr +</option>
</select>
</div>
</div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<select name="type" class="wide select_option">
<option data-display="Select Property Type">Select Property Type</option>
<option value="Apartments">Apartments</option>
<option value="Bungalow">Bungalow</option>
<option value="Row House">Row House</option>
<option value="Villas">Villas</option>
</select>
</div>
</div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<input type="text" class="form-control" id="inputLocation" name="city" value="" placeholder="Enter City" required>
</div>
</div>
<br>
<div class="mx-auto">
<div class="form-group">
<button class="btn btn-primary text-uppercase btn-block"> search
<i class="fa fa-search ml-1"></i>
</button>
</div>
</div>
</div>
<?= form_close(); ?>
</div>
Controller code
public function search_residential() {
$data['getfooter'] = $this->homeModel->getFooterInfo();
$data['getad'] = $this->homeModel->getCarousel();
$data['pagelist'] = $this->pageModel->getPages();
$data['cities'] = $this->db->table('tblcity')->select('*')->get()->getResult();
$params = array(
'budget' => $this->request->getVar('budget'),
'type' => $this->request->getVar('type'),
'city' => $this->request->getVar('city'),
);
$data['search'] = $this->homeModel->getSearch($params);
return view('searchresidential', $data);
}
**Model code**
//Searching residential data
public function getSearch($params) {
$budget = $params['budget'];
$type = $params['type'];
$city = $params['city'];
$builder= $this->db->table('tblresidential');
$builder->select('*');
$builder->where('1 = 1');
if ($budget != '') {
$builder->where('budget', $budget);
}
if ($type != '') {
$builder->where('type', $type);
}
if ($city != '') {
$builder->where('city', $city);
}
return $builder->get()->getResult();
}
Instead of:❌
// ...
$builder->where('budget', $budget);
// ...
Use this:✅
// ...
foreach ($budget as $range) {
$builder->orWhere((function (string $range) {
$limits = array_map(
fn($limit) => floatval($limit),
explode("-", $range)
);
return match (count($limits)) {
1 => ['budget >=' => $limits[0]],
2 => ['budget >=' => $limits[0], 'budget <=' => $limits[1]],
default => ['1 =' => 1]
};
})($range));
}
// ...
Resource: $builder->where() : Associative array method

I am beginner trying to update status on view table laravel but i am getting controller error that (Attempt to read property "status" on null)

invoices/index.blade.php
<td>
<form action="{{url('/invoice_status_upadated')}}" method="POST">
{{ csrf_field() }}
<div class="input-group mb-3">
<select class="form-select" aria-label="Default select example">
<option value="0" {{$inv->status == 0 ? 'selected':''}}>Pending </option>
<option value="1" {{$inv->status == 1 ? 'selected':''}}>In Process </option>
<option value="2" {{$inv->status == 2 ? 'selected':''}}>Completed </option>
<option value="3" {{$inv->status == 3 ? 'selected':''}}>Cancelled </option>
<?php
if ($inv->status == 0){
echo "selected";
}
?>
<?php
if ($inv->status == 1){
echo "selected";
}
?>
<?php
if ($inv->status == 2){
echo "selected";
}
?>
<?php
if ($inv->status == 3){
echo "selected";
}
?>
</select>
<button type="submit" class="btn btn-outline-success">Update</button>
</div>
</form>
</td>
/TemplateContorller
public function invoice_status_upadated(Request $request){
$data= Invoice::find($request->status);
$data->status=$request->$data->status;
$data->save();
return redirect('invoices');
}
web.php
Route::post('/invoice_status_upadated', 'App\Http\Controllers\TemplateController#invoice_status_upadated')->name('invoice_status_upadated');
View
error
Try this :
<select class="form-select" aria-label="Default select example" name="status">
And also in your controller you have to change :-
$data = Invoice::find($invoice_id);
$data->status = $request->status;
$data->save();
Hope this will work for you.
Instead of doing $data->status=$request->$data->status; do $data->status = $request->status;
$request does not have the data from the model, which is in the $data variable.
Hi first of all you need to async you select tag with name tag and set it to status
for example
<select class="form-select" aria-label="Default select example" name="status">
<input type="hidden" value="{{ $inv->id }}" name="inv_id"/>
then in your controller first of all you need to validate your request incomping data for mor information please see this in laravel documentations
after that you need to change your code like this
$data= Invoice::find($request->inv_id);
$data->status= $data->status;

Set Default option in dropdown list HTML

In my create.blade.php I have a dropdown list which is like that.
<div class="form-group">
<label for="company-content">Sexe</label>
<select name="sex" id="" class="form-control">
<option value="">Choice</option>
<option>Women</option>
<option>Man</option>
</select>
</div>
If I choose the option 2, that is to say the item man, in my edit.blade.php I will wish to get the item man.
However, when I want to change the item, my dropdown list is always on woman bij default. It's not practical...
Here is my code concerning the file edit.blade.php, do you have an idea please?
Thank you
<div class="form-group">
<label for="company-content">Sex</label>
<select name="sexe" id="" class="form-control">
<option>Women</option>
<option>Man</option>
</select>
</div>
Edit: 16/03/2019
Visibly, I should do several modifications on my Controller too?
My function edit
public function edit($id)
{
//
$candidats = Candidat::find($id);
$permis = Permis::all();
return view('admin.candidats.edit', compact('candidats', 'permis'));
}
My function update
public function update(Request $request, $id)
{
$request->validate([
'sexe' => 'required|string',
'fk_permis' => 'required'
]);
$candidats = Candidat::find($id);
$candidats->sexe = $request->get('sexe');
$candidats->fk_permis = $request->get('fk_permis');
$candidats->save();
return redirect()->route('candidats.index')
->with('success', 'mise à jour effectuée');
}
Where I should add this line please?
return view('admin.candidats.edit', ['data' => $data]);
Here is my edit.blade.php
<div class="form-group">
<label for="company-content">Sex</label>
<select name="sexe" class="form-control" >
<option value="Man" {{ $data['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $data['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
</div>
So, my problem is in my Controller?
for laravel from controller return view with data like
return view('edit', ['data' => $data]);
<select name="sexe" class="form-control" >
<option value="Man" {{ $data['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $data['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
and for php
<select name="sexe" class="form-control" >
<option value="Man" {{ $_GET['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $_GET['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
Assuming you're POSTing your data in a form, and have included values as suggested by #Second2None, you need to tell your form to select the relevant option.
<option<?php if ($_POST['sexe'] == 'man') echo " selected"; ?>>Man</option>

Laravel 5.1 - Store input data in my session

I have a view show.php of my single product, when user click on ADD PRODUCT my controller CartController.php update my session cart and update my cartSession table,
Now i'm tryng to put new data (Color input,Size input, Quantity input) on my session and also in my CartSession table. But i dont know why it doesn't work.
-I think that the principal problem is that $request->get('input') doesn't pass to my CartController.php , i tried to return
$request->all() and there is not nothing.
CartController.php
namespace dixard\Http\Controllers;
use Illuminate\Http\Request;
use dixard\Http\Requests;
use dixard\Http\Controllers\Controller;
use dixard\Product;
use dixard\CartSession;
use dixard\CartItem;
use dixard\Shipping;
use Session;
// i think that all classes are ok
public function add(Product $product, CartSession $CartSession,Request $request)
{
$id = $request->get('id');
$cart = \Session::get('cart');
$product->quantity = $request->get('qty');
$product->size = $request->get('size');
$product->color = $request->get('color');
$cart[$product->id] = $product;
\Session::put('cart', $cart);
return $request->all(); // here i tried to get all inputs but it shows me nothing result
$subtotal = 0;
foreach($cart as $producto){
$subtotal += $producto->quantity * $producto->price;
}
$session_code = Session::getId();
$CartSession = new CartSession();
$session_exist = CartSession::where('session_code', $session_code)->orderBy('id', 'desc')->first();
if (isset($session_exist)) {
$s = new CartSession;
$data = array(
'subtotal' => $subtotal,
);
$s->where('session_code', '=', $session_code)->update($data);
}else {
$CartSession = new CartSession();
$CartSession->session_code = $session_code;
$CartSession->subtotal = $subtotal;
$CartSession->save();
}
//return $cart;
//salveremo tutte le informazioni nel array cart nella posizione slug
foreach($cart as $producto){
$this->saveCartItem($producto, $CartSession->id, $session_code, $cart);
}
return redirect()->route('cart-show');
}
Routes.php
Route::bind('product', function($id) {
return dixard\Product::where('id', $id)->first();
});
Route::get('cart/add/{product}', [
'as' => 'cart-add',
'uses' => 'CartController#add'
]);
show.php view
Get Data about the product is ok, title, description, color avaible, price ecc. all information pass to my view.
{!! Form::open(['route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="id" value="{{$product->id}}">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="p_color">Colore</label>
<select name="color" id="p_size" class="form-control">
#foreach($colors as $color)
<option value="{{ $color->color }}">{{ $color->color }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="size">Size</label>
<select name="size" id="p_size" class="form-control">
<option value="XS">XS</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="qty">Quantity</label>
<select name="qty" id="p_qty" class="form-control">
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
</select>
</div>
</div>
</div>
<div class="product-list-actions">
<span class="product-price">
<span class="amount">{{$product->price}}</span>
<input type="submit" class="btn btn-lg btn-primary" >
ADD PRODUCT
</input>
</div><!-- /.product-list-actions -->
{!! Form::close() !!}
Thank you for your help!
You have to explicitly set the post-method to be "get" or you need to change your router to accept the request as post. Even though you're calling the route by name, Form::open defaults to "POST"
https://laravelcollective.com/docs/5.2/html#opening-a-form
Option 1. Change
Route::get('cart/add/{product}',...
to
Route::post('cart/add/{product}',...
Option 2. Change
{!! Form::open(['route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}
to
{!! Form::open(['method'=>'GET', 'route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}

$_POST not working with <select>

I can get the $_POST working perfectly with the inputs but not with the select statements for some reason. Here is my code:
registration.php
<form class="form-horizontal" action="results.php" method="post">
<div class="form-group">
<label class="col-md-4 control-label" for="user_type">Type</label>
<div class="col-md-4">
<select id="user_type" name='user_type' class="form-control">
<option selected="" value="<?php if (isset($user_type)) { echo 'admin'; } ?>">Faculty</option>
<option value="<?php if (isset($user_type)) { echo 'registeredUser'; } ?>">Student</option>
</select>
</div>
</div>
<!-- Submit Button -->
<div class="form-group">
<div class="col-md-12">
<button id="submit" name="submit" class="btn btn-primary center-block"> <span class="k-icon k-si-plus"></span>Submit</button>
</div>
</div>
</form>
results.php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// Setting the form inputs to variables
$first_name = trim($_POST["first_name"]);
$last_name = trim($_POST["last_name"]);
$email_address = trim($_POST["email_address"]);
if (isset($_POST["submit"])) {
$user_type = $_POST['user_type'];
echo $user_type . '<br><br>';
}
echo $first_name . '<br><br>';
echo $last_name . '<br><br>';
echo $email_address . '<br><br>';
}
ouput:
Alex
Cory
alex#mydomain.com
Any Ideas as to why this $_POST['user_type']; isn't working?
Assuming that what you have posted is the complete contents of registration.php then the issue is that your value for each of the user_name options contains an empty string.
The actual HTML that your script generates is:
<select id="user_type" name='user_type' class="form-control">
<option selected="" value="">Faculty</option>
<option value="">Student</option>
</select>
This is happening because your registration.php script is checking if the PHP variable $user_data is set before adding the values of admin or registeredUser to the <option> tags.
If we expand your code you can see that the if condition will never fire:
<option selected="" value="<?php
if (isset($user_type)) {
// This will never be fired because the PHP variable $user_data is not set.
echo 'admin';
}
// The result is that nothing is echoed so the HTML reads value=""
?>">Faculty</option>
To fix, then either remove the conditional check:
<select id="user_type" name='user_type' class="form-control">
<option selected="selected" value="admin">Faculty</option>
<option value="registeredUser">Student</option>
</select>
Or set $user_data at the start of the script:
<?php $user_data = true; ?>
<form class="form-horizontal" action="results.php" method="post">
<div class="form-group">
...
But what I think you are really trying to achieve is to determine which of these options to populate as selected in your form. If this is the case, you should check that $user_type is set and if it's value matches the option value, mark that option as selected.
<select id="user_type" name='user_type' class="form-control">
<option <?php
if (isset($user_type) && $user_type === 'admin') {
echo 'selected="selected" ';
}
?>value="admin">Faculty</option>
<option <?php
if (isset($user_type) && $user_type === 'registeredUser') {
echo 'selected="selected" ';
}
?>value="registeredUser">Student</option>
</select>
I cant see the submit button in your code, does it have name='submit' on it ?
Additionally if you are using chrome inspect the post in network(developer tools) to see if 'submit' is posted
This happens because both of your options under select id="user_type" have no values when registration.php is executed.
<option value="<?php if (isset($user_type)) { echo 'registeredUser'; } ?>">Student</option>
If $user_type is not defined the value for both options will be "" (empty string) - and I assume this is what happens.
Here's the Answer
The problem had a little to do with PHP and also a little to do with HTML. PassKit did a great job explaining the PHP which I ended up using in my code to make it work properly. Look to his answer about the PHP and mine about the HTML.
The opening < form > tag needed an "id" attribute that matched the < select > tag's "form" attribute which neither of them had.
Solution:
<form id="user-form" class="form-horizontal" action="results.php" method="post">
right here -^ needed: id="someFormID"
<!-- Faculty or Student? DROP-DOWN -->
<div class="form-group">
<label class="col-md-4 control-label" for="user_type">Type</label>
<div class="col-md-4">
<select form="user-form" id="user_type" name='user_type' class="form-control">
right here ---------------------^ needed: form="someFormID"
<option <?php
if (isset($user_type) && $user_type === 'admin')
{
echo 'selected="selected" ';
}
?>value="admin">Faculty</option>
<option <?php
if (isset($user_type) && $user_type === 'registeredUser')
{
echo 'selected="selected" ';
}
?>value="registeredUser">Student</option>
</select>
</div>
</div>
<!-- Submit Button -->
<div class="form-group">
<div class="col-md-12">
<button id="singlebutton" name="submit" class="btn btn-primary center-block"> <span class="k-icon k-si-plus"></span>Submit</button>
</div>
</div>
</form>
use submit button in your form or check post with if($_POST)

Categories