I'm trying to save in database some elements and a photo with a form, I've added the "enctype="multipart/form-data" on the form and I've executed the command "php artisan storage:link", but when I click on Upload button, Laravel returns me this error: "Call to a member function store() on null" on file PostsController
Here is my file PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Post;
class PostsController extends Controller
{
public function submitpost(Request $req)
{
$posttitle = $req->input('posttitle');
$postauthor = $req->input('postauthor');
$postcontent = $req->input('postcontent');
$img = $req->input('img')->store('public/img');
$dati = compact('posttitle', 'postauthor', 'postcontent', 'img');
$b = new Post();
$b->posttitle = $posttitle;
$b->postauthor = $postauthor;
$b->postcontent = $postcontent;
$b->img = $img;
$b->save();
$posts = Post::all();
return view('blog', compact('posts'));
}
}
This is my view file addpost.blade.php
<main class="main-content">
<div class="container-fluid photos">
<div class="row justify-content-center">
<div class="col-md-6 pt-4" data-aos="fade-up">
<h2 class="text-white mb-4">Create a new post</h2>
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12">
<form action="{{route('submitpost')}}" enctype="multipart/form-data" method="post">
#csrf
<div class="row form-group">
<div class="col-md-12">
<label class="text-white" for="subject">Post Title</label>
<input type="subject" name="posttitle" id="subject" placeholder="Give a title to the post" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="subject" readonly hidden name="postauthor" id="subject" value="{{Auth::user()->name}}" class="form-control">
</div>
</div>
<div class="row form-group mb-5">
<div class="col-md-12">
<label class="text-white" for="message">Content of post</label>
<textarea name="postcontent" id="message" cols="30" rows="7" class="form-control" placeholder="Write your post here"></textarea>
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="file" name="img" value="{{old('img')}}" placeholder="Image" value="">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" value="Create Post" class="btn btn-primary btn-md text-white">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
This is my Route in web.php
Route::post('/addpost/submitpost', 'PostsController#submitpost')->name('submitpost');
And this is my model Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'posttitle',
'postauthor',
'postcontent',
'img',
];
protected $table = 'posts';
}
As you can see in the documentation, uploaded files can be accessed using the file() method.
Try updating this:
$img = $req->input('img')->store('public/img');
To this:
$path = $req->file('img')->store('public/img');
^^^^^^^^^^
Also, you can confirm if the request actually has the file in its payload checking if the file exists, to avoid exceptions:
if ($req->hasFile('img')) {
$path = $req->file('img')->store('public/img');
}
This and more useful methods, can be seen in the File Uploads section of the docs.
Related
Hello friends
I'm a new in laravel framework am trying to build a crud app so got this issue When i click Edit btn in the index page which calls data in specific ID to edit it well it shows "error 404 page not found" i still can't find where the problem is so please help
Product controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\product;
use Illuminate\Support\Facades\DB;
/*call product by specific id to update */
public function edit($id){
$product = DB::table('products')->where('id',$id)->first();
return view('product.edit',compact('product'));
}
/* product update */
public function update(request $request ,$id){
$oldlogo = $request->old_logo;
$data = array();
$data['product_name'] = $request->product_name;
$data['product_code'] = $request->product_code;
$data['product_details'] = $request->product_details;
$image = $request->file('product_logo');
if ($image){
unlink($oldlogo);
$image_name = date('dmy_H_s_i');
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name.'.'.$ext;
$upload_path = 'public/media/';
$image_url = $upload_path.$image_full_name;
$data['product_logo'] = $image_url;
$success =$image->move($upload_path,$image_full_name);
$data['product_logo'] =$image_url;
$product = DB::table('products')->where('id'.$id) -> update($data);
}
return redirect()->route('product.index')
->with('success','Product updated successfully');
}
edit btn`
<a class="btn btn-primary" href="{{ URL :: to ('edit/product'.$pro->id) }}">Edits</a>
edit page
#extends('product.layout')
#section('content')
<br><br><br>
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Product</h2>
</div>
</div>
<br><br><br>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('product.index') }}">Back</a>
</div>
<form action="" method="POST" enctype="multipart/form-data">
#csrf
<div class="form-row">
<div class="col-xs-12 col-xm-12 col-md-12">
<div class="form-group">
<strong>Product name</strong>
<input type="text" name="product_name" calss="form-control" value="{{ $product ->product_name }}">
</div>
</div>
<div class="col-xs-12 col-xm-12 col-md-12">
<div class="form-group">
<strong>Product Code</strong>
<input type="text" name="product_code" calss="form-control" value="{{ $product ->product_code }}">
</div>
</div>
<div class="col-xs-12 col-xm-12 col-md-12">
<div class="form-group">
<strong> Detials </strong>
<textarea class="form-control" name="Details" style="height:150px" >
{{ $product ->product_details" }}</textarea>
</div>
<div class="col">
<div class="form-group">
<strong>Product image</strong>
<input type="file" name="logo">
</div>
</div>
<div class="col">
<div class="form-group">
<strong>Product old image</strong>
<img src ="{{ URL::to($product->product_logo) }}" height="70px" width="80px" alt="logo">
<input type="hidden" name=" old_logo" value="{{ $product->product_logo }}">
</div>
</div>
<button type="button" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
** edit Route **
Route::get('edit/product/{id}','ProductController#edit');
Route::post('update/product/{id}','ProductController#update');
laravel Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class product extends Model
{
protected $fillable = [
'product_name', 'product_details', 'product_code','product_logo'
];
}
I think your problem is right here: {{ URL :: to ('edit/product'.$pro->id) }} because its going to print for example this: /edit.product23. What you want is /edit/product/23, so change your URL to {{ URL::to('edit/product/'.$pro->id) }} or {{ url('edit/product/'.$pro->id) }}. Also make sure you set the route like this Route::get('/edit/product/{id}, 'ControllerName#edit'); in your web.php route file.
iam new in laravel i have problem to add data in data base using laravel , i get only validation response in form but after i submit data i don't get any response only just page refresh . without any message appear in view
ac any on help me ?
this is man categories request for validation
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class MaincategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'id' => 'integer|required',
'name' => 'min:2|required|max:50|unique:category_translations',
'slug' => 'required|unique:categories,slug,'.$this->id,
'url' => 'required|url',
'status' => 'integer'
];
}
// validation messages
public function messages()
{
return [
'id.required'=>trans('dashboard\validate.required'),
'id.integer'=>trans('dashboard\validate.integer'),
'id.exists'=>trans('dashboard\validate.exists'),
'name.required'=>trans('dashboard\validate.required'),
'name.min'=>trans('dashboard\validate.min'),
'name.max'=>trans('dashboard\validate.max'),
'name.unique'=>trans('dashboard\validate.unique'),
'slug.required'=>trans('dashboard\validate.required'),
'slug.min'=>trans('dashboard\validate.min'),
'slug.max'=>trans('dashboard\validate.max'),
'slug.unique'=>trans('dashboard\validate.unique'),
'url.active_url'=>trans('dashboard\validate.url'),
'url.required'=>trans('dashboard\validate.required'),
'status.integer'=>trans('dashboard\validate.integer'),
];
}
}
this is my route in details
Route::get('create','MainCategoriesController#create') -> name('maincategories.create');
Route::post('store','MainCategoriesController#store') -> name('maincategories.store');
this is my controller in details
public function store(MaincategoryRequest $request )
{
try{
DB::beginTransaction();
// prepare data
$validatedData = array(
'name' =>$request->name,
'url' =>$request->url,
'slug' =>$request->slug,
'last_updated_by' =>auth('admin')->user()->id,
'created_by' =>auth('admin')->user()->id,
'created' =>time(),
);
//check if status is sent
$request->has('status') ? $validatedData['status'] = 1: $validatedData['status'] = 2;
// check if category is exist
$add = Category::create($validatedData);
if (!$add){
return redirect()->route('maincategories.create')->with(['error'=> trans('dashboard\messages.addfailed')]);
}
// start add translated data
$add->name=$request->name;
$add->save();
return redirect()->back()->with('success',trans('dashboard\messages.save'));
DB::commit();
}catch (\Exception $ex){
return redirect()->back()->with('error',trans('dashboard\messages.addfailed'));
DB::rollBack();
}
}
this is my view in details
#extends('layouts.admin')
#section("title",trans('dashboard\category.title-add'))
#section('content')
<div class="app-content content">
<div class="content-wrapper">
<div class="content-header row">
<div class="content-header-left col-md-6 col-12 mb-2">
<div class="row breadcrumbs-top">
<div class="breadcrumb-wrapper col-12">
<ol class="breadcrumb">
<li class="breadcrumb-item"> {{trans('dashboard\messages.home')}}
</li>
<li class="breadcrumb-item active">{{trans('dashboard\category.title-add')}}
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="content-body">
<!-- Basic form layout section start -->
<section id="basic-form-layouts">
<div class="row match-height">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4 class="card-title" id="basic-layout-form"> {{trans('dashboard\category.title-add')}} </h4>
<a class="heading-elements-toggle"><i
class="la la-ellipsis-v font-medium-3"></i></a>
<div class="heading-elements">
<ul class="list-inline mb-0">
<li><a data-action="collapse"><i class="ft-minus"></i></a></li>
<li><a data-action="reload"><i class="ft-rotate-cw"></i></a></li>
<li><a data-action="expand"><i class="ft-maximize"></i></a></li>
<li><a data-action="close"><i class="ft-x"></i></a></li>
</ul>
</div>
</div>
#include('dashboard.includes.alerts.success')
#include('dashboard.includes.alerts.errors')
<div class="card-content collapse show">
<div class="card-body">
<form class="form"
action="{{route('maincategories.store')}}"
method="post"
enctype="multipart/form-data">
#csrf
<div class="form-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="projectinput1"> {{trans('dashboard\category.name')}} </label>
<input type="text" value="{{old('name')}}"
id="name"
class="form-control"
placeholder=" "
name="name">
#error("name")
<span class="text-danger">{{$message}}</span>
#enderror
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="projectinput1"> {{trans('dashboard\category.slug')}} </label>
<input type="text"
value="{{old('slug')}}"
id="email"
class="form-control"
placeholder=" "
name="slug">
#error("slug")
<span class="text-danger">{{$message}}</span>
#enderror
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label for="projectinput1"> {{trans('dashboard\category.url')}} </label>
<input type="text"
value="{{old('url')}}"
id="plain_value"
class="form-control"
placeholder=" "
name="url">
#error("url")
<span class="text-danger">{{$message}}</span>
#enderror
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label> {{trans('dashboard\category.image')}} </label>
<label id="projectinput7" class="file center-block">
<input type="file" id="file" name="image">
<span class="file-custom"></span>
</label>
#error('image')
<span class="text-danger">{{$message}}</span>
#enderror
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group mt-1">
<input type="checkbox" value="1"
name="status"
id="switcheryColor4"
class="switchery" data-color="success"
checked />
<label for="switcheryColor4"
class="card-title ml-1">{{trans('dashboard\messages.status')}} </label>
#error("status")
<span class="text-danger"> </span>
#enderror
</div>
</div>
</div>
<div class="form-actions">
<button type="button" class="btn btn-warning mr-1"
onclick="history.back();">
<i class="ft-x"></i> {{trans('dashboard\messages.back')}}
</button>
<button type="submit" class="btn btn-primary">
<i class="la la-check-square-o"></i> {{trans('dashboard\messages.save')}}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- // Basic form layout section end -->
</div>
</div>
</div>
#stop
#section('script')
<script >
$(document).ready(function() {
//start update password
$("#EditPassword").submit(function(){
var formData = $(this).serialize();
var allData = formData + "&action=editPass";
$('#repassword_error').text('');
$('#password_error').text('');
$.ajax({
url: "{{url("admin/category/update_password/{id}")}}",
type:"PUT",
data: allData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
beforeSend:function(){
//alert(allData)
},
statusCode: {
404: function() {
alert( "page not found" );
},
},
success:function(valdata) {
//alert(valdata);
//alert("")
if(valdata.status == "success")
{
$("#updatePasswordResult").html(valdata.message);
setTimeout(function(){$('#changepassword').modal('hide');}, 2000);
}else{
$("#updatePasswordResult").html(valdata.message);
}
},
error: function (reject) {
var response = $.parseJSON(reject.responseText);
$.each(response.errors, function (key, val) {
$("#" + key + "_error").text(val[0]);
});
}
});
return false;
});
});
</script>
#stop
You don't need to use transaction here, However, move DB::commit before return .
besides, check $guarded and $fillable in your model.
also you can dd($ex->getMessage()) to track the error.
So i was trying to make a posting form using ckeditor and post it to database, and then try to display it in the same view, but after i submit the form i don't see anything in my database table so clearly it's not even storing to database, is there any mistakes in my controller or view ?
this is my GuestbookController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Guestbook;
class GuestbookController extends Controller
{
public function index()
{
$guestbooks = Guestbook::get();
return view('post.post_textarea',[
'guestbooks' => $guestbooks,
]);
}
public function store(Request $request)
{
Guestbook::create([
'name' => $request->name,
'message' => $request->message
]);
return redirect()->back();
}
}
this is my routes
Route::get('/posting','GuestbookController#index')->name('guestbook');
Route::post('/posting','GuestbookController#store')->name('guestbook.store');
this is my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Guestbook extends Model
{
protected $fillable = ['name', 'message'];
}
and this is my view
<section class="games-single-page">
<div class="container">
#foreach ($guestbooks as $guestbook)
<div class="card">
<div class="card-body">
<label>mike</label>
<h3>{{ $guestbok->name }}</h3>
{!! $guestbook->message !!}
</div>
</div>
#endforeach
<div class="card">
<div class="card-body">
<form action="/posting" method "POST">
<div class="form-group">
<label style="color: black;" >Title</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label style="color: black;" >Your Input</label>
<br>
<textarea class="form-control" name="message" id="" rows="10"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value"Send">
</div>
</form>
</div>
</div>
</div>
</section>
You forgot an equal sign '=' and a csrf field. try the answer.
<form action="/posting" method="POST">
{{csrf_field()}}
I created an edit form to update values in my database and then show it on the main page. The problem is that it doesn't save the data to DB.
the request passes: Status Code: 302 Found
with all the values that I want to change (for example adding phone and mail):
effective_date_practitioner: 2019-01-01
expiry_date_practitioner:
phone_number_practitioner: 918273645
mobile_number_practitioner:
email_practitioner: test#test.pl
practitioner_specialty_id_update: 1
effective_date_specialty: 2019-01-01
expiry_date_specialty:
Edit blade
<div class="edit-practitioner" style="display: none;">
<form style="box-shadow: none;" action="/practitioner/update/{{$practitioner->practitioner_id}}" method="post"
class="j-pro" id="update-practitioner-form">
#csrf
<div class="j-content">
<div id="j-row-id" class="j-row">
<div class="row">
<div class="col-sm-12 col-lg-12 col-xl-5">
<div class=" j-unit">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>Practitioner Information</span>
</div>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->effective_date}}"
name="effective_date_practitioner">
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->expiry_date}}"
name="expiry_date_practitioner">
</div>
<label class="j-label">{{trans('personalData.phoneNumber')}}</label>
<div class="j-input">
</label>
<input type="tel" value="{{$practitioner->phone}}"
name="phone_number_practitioner">
</div>
<label class="j-label">{{trans('personalData.mobileNumber')}}</label>
<div class="j-input">
<input type="tel" value="{{$practitioner->mobile}}"
name="mobile_number_practitioner">
</div>
<label class="j-label">{{trans('personalData.email')}}</label>
<div class="j-input">
<input type="email" value="{{$practitioner->email}}"
name="email_practitioner">
</div>
</div>
</div>
<div class="col-xl-1 j-unit"></div>
<div class="col-sm-12 col-lg-12 col-xl-6">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>{{trans('settings.specialty')}}</span>
</div>
<select name="practitioner_specialty_id_update"
id="practitioner_specialty_id_update"
class="form-control-practitioner required">
#foreach($specialties as $specialty)
<option
value="{{$specialty->specialty_id}}">{{$specialty->name}}</option>
#endforeach
</select>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner_specialty->effective_date}}"
name="effective_date_specialty">
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner_specialty->expiry_date}}"
name="expiry_date_specialty">
</div>
</div>
</div>
</div>
<div class="j-divider j-gap-bottom-45 j-gap-top-10"></div>
<button type="submit"
class="btn btn-editpanel btn-success btn-round">Save changes
</button>
<!-- end /.footer -->
<button id="update-cancel-button-practitioner" type="button"
class="btn btn-editpanel btn-danger btn-round">Cancel
</button>
</div>
</form>
</div>
web.php
Route::post('/practitioner/update/{id}', 'Crud\Settings\PractitionerController#updatePractitioner')->name('updatePractitioner');
Controller:
<?php
namespace App\Http\Controllers\CRUD\Settings;
use App\Models\Practitioner;
use App\Models\PractitionerCompetenceLevel;
use App\Models\PractitionerSpecialty;
use App\Repositories\PractitionerRepository;
use Illuminate\Http\Request;
class PractitionerController extends CrudController
{
protected $repository;
public function __construct(PractitionerRepository $repository)
{
$this->middleware('auth');
$this->repository = $repository;
}
public function updatePractitioner($id, Request $request)
{
$this->validate($request, [
'effective_date_practitioner' => 'required',
'effective_date_specialty' => 'required',
]
);
$input = $request->all();
$input['data'][$this->repository->getIdName()] = $id;
$this->repository->update($input['data']);
return back()->with('successUpdate', 'Practitioner has been updated!');
}
}
My guess is that the data I want to update belongs to two different tables in DB one called practitioner and the other one called practitioner_specialty
As per your code you are trying to do mass assignment to your model
You may do this using the $fillable property on the model
protected $fillable = ['effective_date_practitioner','effective_date_specialty',......];
And you can use attach() method to updated data in related tables
i have form in which there are 2 dropdowns which are select batch from first dropdown and according to that batch, students will appear in second dropdown , Right now i am getting dropdown of batch but, not getting dropdown of students using batch
This is my view
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use common\models\TblBetch;
use common\models\TblCompany;
use common\models\TblCourse;
use common\models\TblStudent;
use common\models\TblStudentExpence;
use kartik\widgets\DepDrop;
use yii\db\Query;?><link rel="shortcut icon" href="<?php echo Yii::$app->params['global_theme_path'];?>images/logo1.png" type="image/png"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script src="http://iamrohit.in/lab/js/location.js"></script><div class="pageheader"><h2><i class="fa fa-money"></i>Expense</h2>
<div class="breadcrumb-wrapper">
<span class="label">You are here:</span>
<ol class="breadcrumb">
<li>Westline Shipping</li>
<li class="active">Add Expense</li>
</ol></div></div><div class="contentpanel">
<?php
$session = Yii::$app->session;
if($session->hasFlash('success'))
{
echo $session->getFlash('success');
}
if($session->hasFlash('error'))
{
echo $session->getFlash('error');
}
$site = '';
?>
<div class="row">
<div class="col-md-12">
<div class="container-fluid-50">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-btns">
−
</div>
<h4 class="panel-title">Add Expense</h4>
</div>
<form class="panel-body" action="dashboard" method="post">
<div class="row">
<?php $form = ActiveForm::begin(); ?>
<div class="col-sm-9">
<div class="form-group">
<label class="control-label"></label>
<?php
//$batch = new TblBetch();
//$quer = new Query();
//$getid = [''];
//$getid = $quer->select('b_sn')->from($batch->tableName())->all();
//echo $form->field($model,'se_b_id')->dropDownList($getid,['id'=>'se_b_id']);
?>
<?=
$form->field($model,'se_s_id')->dropDownList(
ArrayHelper::map(TblBetch::find()->all(),'b_id','b_sn'),
[
'prompt'=>'Select Batch',
'name'=>'s_b_id',
'id'=>'s_b_id',
'onchange'=>'$.post("student?id="+$(this).val(),function(data){$("select#tblstudentexpence-se_s_id").html(data);});'
]);
?>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label"></label>
<?=
$form->field($model,'se_s_id')->dropDownList(
ArrayHelper::map(TblStudent::find()->all(),'s_id','s_fname'),
[
'prompt'=>'Select Student',
'name'=>'se_s_id',
'id'=>'se_s_id'
]);
?>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Payment Mode</label>
<div class="radio"><label><input type="radio" name="num" value="cash" required> Cash </label></div>
<div class="radio"><label><input type="radio" name="num" value="check" required> Check </label></div>
<div class="radio"><label><input type="radio" name="num" value="dd" required> DD </label></div>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Check / DD No</label>
<input type="text" name="num" id="num" class="form-control" placeholder="Enter your check or DD number" required/>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Amount</label>
<input type="text" name="lastname" class="form-control" placeholder="Enter your Amount" required/>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group"> <br><br>
<input type="submit" value="Submit" class="btn btn-primary">
<input type="reset" value="Cancel" class="btn btn-primary">
</div></div>
</div><!-- row -->
</form>
</div><!-- panel-body -->
</div>
</div>
</div> <div class="col-md-12">
</div>
</div></div></div> <script>
$(function() {
window.invalidate_input = function() {
if ($('input[name=num]:checked').val() == "check" || $('input[name=num]:checked').val() == "dd")
$('#num').removeAttr('disabled');
else
$('#num').attr('disabled', 'disabled');
};
$("input[name=num]").change(invalidate_input);
invalidate_input();
});
This is my controller (sitecontroller.php)
public function actionStudent($id)
{
//p($id);
$student = new TblStudent();
$queryobj = new Query();
$data = [];
$data = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->count();
//p($data);
$queryobj1 = new Query();
$getid = [];
$getid = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->all();
//p($getid);
if($data > 0)
{
foreach($getid as $gid)
{
echo "<option value='".$gid->s_id."'>".$gid->s_fname."</option>";
}
}
else
{
echo "<option> - </option>";
}
}
I am getting this response in firebug after ajax call
You can implement dependent drop-down much simpler in yii 2 because there is a widget available which you can use.
use kartik widget here -> kartik dependent drop down