Undefined index error message in CRUD page : PHP AJAX JQUERY - php

while using CRUD to one of my database driven website ..when we try to create / updating database using Create or Update option i am getting following error
Notice: Undefined index: crud_table in /home/sulabgqh/public_html/leads/controllers/grid_controller.php on line 89
i wnat to know what is the possible error , for reference below is the public declation of variable 'crud_table' table name
/********************* PUBLIC METHODS ********************/
public function setDbTable($table){
$_SESSION['crud_table'] = $table;
}
public function setPrimaryKey($primaryId) {
$_SESSION['crud_primary_key']=$primaryId;
}
and contructor is used here ..
/********************* CONSTRUCTOR ********************/
public function __construct(){
session_start();
$_SESSION['crud_table'] = null;
$_SESSION['crud_title_map'] = null;
$_SESSION['crud_actions'] = null;
$_SESSION['crud_primary_key']='id';
$_SESSION['crud_per_page']=10;
}
and Crete function goes like this
public function create(){
//setting from grid object
$table = $_SESSION['crud_table'];
$pk = $_SESSION['crud_primary_key'];

Seems like the Session-Variable isnt set, because of that it is "undefined". Try to check the varibale before u use it in the function like
if(isset($_SESSION['crud_table'])){
do stuff...
}
Is the Session startet correct?

Make sure you have put session_start() at the top of all scripts that are using sessions, this is the most common reason for session variables not being set.
session_start() doesn't just start a session, it checks if one is already running, if so it loads that one if not it creates a new one.

Related

Magento data array storage in custom session object

I am using my own session class, in that class, I am using some protected data members and some public data methods While I storing some variable on my session class Like
Mage::getSingleton('decision/session')->storeProductInfo('2');
Here is function implementation, $this->_productId is the private data member of my session class.
Public function storeProductInfo($product_id){
$this->_productId = $product_id;
return $this;
}
I am getting the stored variable by calling the below statement, it return me "null".
$product_stored_id = Mage::getSingleton('decision/session')->getStoredProductInfo();
public function getStoredProductInfo(){
return $this->_productId;
}
Even
Mage:getSingleton('decision/session')->setData('product_id', '2');
Didn't working. Can you please let me know where I am going wrong? I have to store some arrays in my session that's why I created my own session class to separately deal with my logic.
Use Magento Magic Method get and set
For that when your observer will call then you can create the session and set the value of that.
you can set the session using set, getting value using get and unset session using uns.
Mage::getSingleton('core/session')->setMySessionVariable('MyValue');
$myValue = Mage::getSingleton('core/session')->getMySessionVariable();
echo $myValue;
To Unset the session
Mage::getSingleton('core/session')->unsMySessionVariable();
$inputMessage = 'Hello World';
Mage::getSingleton('core/session')->setWelcomeMessage($inputMessage);
Now you want to echo the "welcome message" somewhere else in your code/site.
$outputMessage = Mage::getSingleton('core/session')->getWelcomeMessage();
echo $this->__($outputMessage);

Trying to set a session variable with Zend_Session_Namespace, everytime NULL

I'm running an application where in the controller I'm trying to set session variables using Zend_Session and Zend_Session_Namespace:
Bootstrap.php
protected function _initSession()
{
Zend_Session::start();
}
SomeController.php
protected function updateQuestionViewsTotal($question)
{
$userSession = new Zend_Session_Namespace('QA_Session');
if (! is_array($userSession->questionViews)) {
$userSession->questionViews = array();
}
// create session array to contain the questions this
// user has viewed.
if(array_search($question->id, $userSession->questionViews) === false) {
$question->views_total++;
$question->save();
}
// ensure that this page is in the array
array_push($userSession->questionViews, $question->id);
$userSession->questionViews = array_unique($userSession->questionViews);
}
As you can see from above, I have within one of my controllers a method with an attempt to use session variables via Zend_Session_Namespace.
However, when I insert a var_dump on the second page load (refresh):
protected function updateQuestionViewsTotal($question)
{
$userSession = new Zend_Session_Namespace('QA_Session');
var_dump($userSession));
if (! is_array($userSession->questionViews)) {
$userSession->questionViews = array();
}
..Please note: this is AFTER I've run it once, so I'm expecting that the session variable has been set. Anyway on every occasion, it is NULL. So it would seem that the variable isn't being written to $_SESSION? What am I doing wrong?

Is there any way I can send a variable as an argument to the constructor of a controller in CodeIgniter?

I am new to CodeIgniter. I was just thinking, is there any way I can send any variable to the constructor of a controller, the same way I can do in Java when I create an object?
You can send variables to controller function through URL.
For example, if your URL is www.domain.com/index.php/reports/userdata/35
then your controller function in file controllers/reports.php would look like:
function userdata($userId) {
.....
}
I don't know why you want do this and where you intend to get the variable you are sending from but this does work in this case:
function __construct($f=null) {
parent::__construct();
if($f){
return $f; //Here use the variable for whatsoever you want.
}
}
function testvariable($id) { //Using $id, you could still get the value from url
$myVariable = 3; //Or you could just hard code the value
if($id){
$myVariable = $id;
}
echo $this->__construct($myVariable);
exit;
}
When you run http://localhost/controller/testvariable/54
You'd get the result 54
When you run http://localhost/controller/testvariable
You'd get the result 3
Outside these, the other option would be to define the variable in the construct.

Set and get global variable in php (ZendFramework)

I am using ZendFramework with PHP, and I want to set and get a variable as a global variable. I.E. I set it in the class of Zend Controller and access it any action in the class.
For example:
<?php
class SubscriptionController extends Zend_Controller_Action
{
private $EMAIL = false;
private $USERNAME = false;
First I am validating email addres with ajax call
public function checkusernameAction()
{
$email = //query to find email;
if($email){
$EMAIL = true;
}else{
$EMAIL = false;
}
}
then I want subscribe user on the basis of private variable again with ajax call
public function subscribeAction
{
if($EMAIL == true)
{
//some stuff
}
}
I am getting private var by $this->EMAIL, but not able to access it
You can use Zend_Registry to use the variable throughout application.
You can set a variable like this
Zend_Registry::set('email', $EMAIL);
and later can get it like this
$email= Zend_Registry::get('email');
Looks to me like you are making two distinct requests calling, respectively, checkusernameAction() and subscribeAction(). Since these are distinct requests, the email value you set in the controller during checkusernameAction() will be gone on the second request which calls subscribeAction(). It's the same controller class, but you are looking at two distinct instances, one in each request.
As I see it, you can either:
Pass the email address in each AJAX request, but this seems unlikely since you get the email address from the first call to checkusernameAction().
Save the email in the session during the first checkusernameAction() call and then pick it up during the second subscribeAction() call.
Extract the "get email from username" code into a separate class or method and then call it in both places. After all, you don't want to get bitten by a "race condition" in which the state of the system changes between the two AJAX requests (maybe the user's email changes via some other process or via another set of requests that occur after the first AJAX request containing the call to checkusernameAction().
You can also used a function for set and get a value.
// Setter function
public function setConsumerKey($key)
{
$this->_consumerKey = $key;
return $this;
}
// Getter function
public function getConsumerKey()
{
return $this->_consumerKey;
}

Independent Private Sessions

I need to manage multiple sessions in a website. I need to start a private sessions which is very page specific so it will terminate once I walk away from the page. But When I browse to previous page, i should be able to use my old session also. For example:
Page A -> Starts Session A
Page A -> is forwarded to Page B
Page B -> Start its own private Session B
Page B -> Completes the tasks and Terminates its private session B
Page B -> Redirects to Page A
Page A -> Again display the page using its old Session A
Can I start multiple sessions within a same website like this? If yes, How can I manage this?
You can use session_name for this, but if page B is killing its session as soon as a single page view is complete, it seems like a waste of time to use a session in the first place.
Perhaps something like;
$_SESSION[$_SERVER['PHP_SELF']]['name'] = $value;
//Page X termination
unset($_SESSION[$_SERVER['PHP_SELF']]);
Just tersely tossing out ideas here.
Expanding on the privatization of session data; A wrapper could help:
class Session implements ArrayAccess{
private $_data = array();
public function __construct(){
$this->_data = $_SESSION;
}
public function offsetSet($offset, $value){
$this->_data[$_SERVER['PHP_SELF']][$offset] = $value;
}
public function offsetExists($offset){
return isset($this->_data[$_SERVER['PHP_SELF']][$offset]);
}
public function offsetUnset($offset){
unset($this->_data[$_SERVER['PHP_SELF']][$offset]);
}
public function offsetGet($offset){
return isset($this->_data[$_SERVER['PHP_SELF']][$offset])
? $this->_data[$_SERVER['PHP_SELF']][$offset]
: null;
}
public function __destruct(){
$_SESSION = $this->_data;
}
}
$session = new Session;
//etc
Given the query string or more is relevant, you could hash the relevant values for the key.
Eg: $key = md5($_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING']); though using $_SERVER['REQUEST_URI'] as a key may suffice.

Categories