CI Session is not working - codeigniter

We are using CI (ver 2.2.1). We are creating a session, based on url and based on session value, we do some functionalists in Controller and send to view. When we hit the view, undefined error is coming at very first time. when i refresh the page, the view load properly.
We are creating the session in Modal file:
$this->session->set_userdata('url_data',$total_result);
$total_result has array value.
My Controller:
if(isset($plan_id) && $plan_id!=''){ .... }
else {
$sessData = $this->session->userdata('url_data');
$planid = $sessData['id'] ;
$plan_details = $this->mymodel->modelname("...where id = $sessData['id']")
}
Very first time, I had a Error like
Undefined index: id

Related

Laravel 5.8 + Vue2JS Session and Old values in component

I have question about normal laravel multi form with post action as method based on laravel session. I added in this form component of vue, which is simple city autocomplete. I want load to inputs into fields of this component from session (session()), or old (old()) values, if session exist i want to load from session city and province, but if session doesnt exist, but old laravel values exist I want to load them in this component form, if they doesnt exist, leave fields empty. What's the easiest way to do that?
Get the session and pass a default which would be the old value.
$data = session('session_data_key', $old_value); // however you get the old value is up to you.
Pass it down to your view. If no session is set on the server, it will use the default (if default is null it will not set the auto-complete).
UPDATE
If you are using axios to make calls to a laravel app, before loading the form component (in mounted() {}) do a call to axios to get the value of (session or old value). This is what you'll load into the form.
axios.get('/api/load_dafault').then(resp => {
this.defaultValueToBePassedToField = resp.data.value;
});
The endpoint '/api/load_dafault' or whatever yours is named, will have it's controller like so:
public function loadDefaults()
{
$old_value = ... // however you get it is up to you.
$data = session('session_data_key', $old_value);
}
Do not forget to ensure you set the routes.

In a user's Edit Info form, how do I include the name of the field(s) and user's input value(s) in a response from the model

On a Yii2 project, in a user's Edit Info form (inside a modal):
I'm currently figuring out which fields were changed using the jQuery .change() method, and I'm grabbing their value with jQuery's .val() method.
However, I want to do less with JavaScript and do more with Yii's framework.
I can see in the Yii debugger (after clicking into the AJAX POST request) that Yii is smart enough to know which fields were changed -- it's showing SQL queries that only UPDATE the fields that were changed.
What do I need to change in the controller of this action to have Yii include the name of the field changed -- including it's value -- in the AJAX response? (since my goal is to update the main view with the new values)
public function actionUpdateStudentInfo($id)
{
$model = \app\models\StudentSupportStudentInfo::findOne($id);
if ($model === null) {
throw new NotFoundHttpException('The requested page does not exist.');
}
$model->scenario = true ? "update-email" : "update-studentid";
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->renderAjax('_student_support_alert_success');
}
return $this->renderAjax("_edit_student_info",[
"model" => $model,
]);
}
I'm currently returning a static success view.
You can use $model->dirtyAttributes just after load the data to get a $attrib => $value pair array.
http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#getDirtyAttributes()-detail (this docs says:)
Returns the attribute values that have been modified since they are loaded or saved most recently.
The comparison of new and old values is made for identical values using ===.
public array getDirtyAttributes ( $names = null )
(sorry for formatting, sent by mobile)

x editable and codeigniter: display error message if data not updated into db

In my application I have a dataTable to display record from db, I am trying to edit record using x-editable and its working fine. But the problem is I want to display an error message if submitted data not updated successfully into the db.
I tried 'success:' but it triggered after submit the form whatever the data reach the db or not, but i want to trigger the success if controller return true.
Thanks
if you are processing the editable submission using ajax you need to set:
echo 1; //if model returns true
and
echo 0; //if model returns false
in the controller.
Then in the ajax reponse you need to check in the script:
(Consider the response variable is response)
if(response==1)
{
alert('Update Success.');
}
else
{
alert('Update Failure !')
}
This would be easier if you provided us with a code sample.
If you're working with ActiveRecord, you can always use:
if($this->db->affected_rows() > 0)
{
return true;
}
else
{
return false;
}

Checking for CodeIgniter session_id to not be set

I'm trying to check if its set in my codeigniter application like:
elseif(!isset($this->session->userdata('session_id'))){ ## SESSION data is NOT set
// load a generic 'issue' occurred view.
}
but I keep getting this error, is !isset not valid in Codeigniter? How do I go about easily checking this?
Thanks
You must use the code like below:
$id = $this->session->userdata('session_id');
// your other code
elseif(!isset($id)){
// load a generic 'issue' occurred view.
}
The reason you can see here
You coud also do this ,
elseif($this->session->userdata('session_id') !== false)
{
// SESSION data is NOT set
// load a generic 'issue' occurred view.
}
Note: The function returns FALSE (boolean) if the item you are trying to access does not exist.

MVC3 url routing - render view with previous url (like a postback)

I have a Controller which has a Create method to handle HttpPost data from a form. The page containing the form is accessed by the URL
CallOutcome/Call?orderId=114565
When the form is submitted, I do a db insert & create a view model object which is returned to the view to display the form again. This works fine, however the URL has now changed to the name of my action method:
CallOutcome/Create
How can I make it show the original URL? The desired result would be like that it worked like a postback, i.e. reshowing the same page and URL.
This is my (simplified) action method, which returns a CallDetailsViewModel object to a view named 'Call':
[HttpPost]
public ActionResult Create(GGAP_CallOutcome callOutcome)
{
if (ModelState.IsValid)
{
callRepository.SaveCallOutcome(callOutcome);
return View("Call", new CallDetailsViewModel{
CustomerOrder = new CustomerOrder{},
CallOutcome = new CallOutcome{},
Task = new Task{}
});
}
}
not many responses! too close to christmas maybe?
For the record, I used RedirectToRoute:
return RedirectToRoute(new
{
controller = "CallOutcome",
action = "Call",
orderId = Convert.ToInt32(callOutcome.OrderId)
});
Which does exactly what I wanted.

Resources