How to pass variables and data from PHP to JavaScript?

There are actually several approaches to do this. Some require more overhead than others, and some are considered better than others.

In no particular order:

  1. Use AJAX to get the data you need from the server.
  2. Echo the data into the page somewhere, and use JavaScript to get the information from the DOM.
  3. Echo the data directly to JavaScript.

In this post, we'll examine each of the above methods, and see the pros and cons of each, as well as how to implement them.

1. Use AJAX to get the data you need from the server

This method is considered the best, because your server side and client side scripts are completely separate.

Pros

  • Better separation between layers - If tomorrow you stop using PHP, and want to move to a servlet, a REST API, or some other service, you don't have to change much of the JavaScript code.
  • More readable - JavaScript is JavaScript, PHP is PHP. Without mixing the two, you get more readable code on both languages.
  • Allows for async data transfer - Getting the information from PHP might be time/resources expensive. Sometimes you just don't want to wait for the information, load the page, and have the information reach whenever.
  • Data is not directly found on the markup - This means that your markup is kept clean of any additional data, and only JavaScript sees it.

Cons

  • Latency - AJAX creates an HTTP request, and HTTP requests are carried over network and have network latencies.
  • State - Data fetched via a separate HTTP request won't include any information from the HTTP request that fetched the HTML document. You may need this information (e.g. if the HTML document is generated in response to a form submission) and, if you do, will have to transfer it across somehow. If you have ruled out embedding the data in the page (which you have if you are using this technique) then that limits you to cookies/sessions which may be subject to race conditions.

Implementation Example

With AJAX, you need two pages, one is where PHP generates the output, and the second is where JavaScript gets that output:

get-data.php

/* Do some operation here, like talk to the database, the file-session   * The world beyond, limbo, the city of shimmers, and Canada.   *    * AJAX generally uses strings, but you can output JSON, HTML and XML as well.    * It all depends on the Content-type header that you send with your AJAX   * request. */    echo json_encode(42); //In the end, you need to echo the result.                         //All data should be json_encode()d.                          //You can json_encode() any value in PHP, arrays, strings,                        //even objects.  

index.php (or whatever the actual page is named like)

<!-- snip -->  <script>      function reqListener () {        console.log(this.responseText);      }        var oReq = new XMLHttpRequest(); //New request object      oReq.onload = function() {          //This is where you handle what to do with the response.          //The actual data is found on this.responseText          alert(this.responseText); //Will alert: 42      };      oReq.open("get", "get-data.php", true);      //                               ^ Don't block the rest of the execution.      //                                 Don't wait until the request finishes to       //                                 continue.      oReq.send();  </script>  <!-- snip -->

The above combination of the two files will alert 42 when the file finishes loading.

Some more reading material

2. Echo the data into the page somewhere, and use JavaScript to get the information from the DOM

This method is less preferable to AJAX, but it still has its advantages. It's still relatively separated between PHP and JavaScript in a sense that there is no PHP directly in the JavaScript.

Pros

  • Fast - DOM operations are often quick, and you can store and access a lot of data relatively quickly.

Cons

  • Potentially Unsemantic Markup - Usually, what happens is that you use some sort of <input type=hidden> to store the information, because it's easier to get the information out of inputNode.value, but doing so means that you have a meaningless element in your HTML. HTML has the <meta> element for data about the document, and HTML 5 introduces data-*attributes for data specifically for reading with JS that can be associated with particular elements.
  • Dirties up the Source - Data that PHP generates is outputted directly to the HTML source, meaning that you get a bigger and less focused HTML source.
  • Harder to get structured data - Structured data will have to be valid HTML, otherwise you'll have to escape and convert strings yourself.
  • Tightly couples PHP to your data logic - Because PHP is used in presentation, you can't separate the two cleanly.

Implementation Example

With this, the idea is to create some sort of element which will not be displayed to the user, but is visible to JavaScript.

index.php

<!-- snip -->  <div id="dom-target" style="display: none;">      <?php           $output = "42"; //Again, do some operation, get the output.          echo htmlspecialchars($output); /* You have to escape because the result                                             will not be valid HTML otherwise. */      ?>  </div>  <script>      var div = document.getElementById("dom-target");      var myData = div.textContent;  </script>  <!-- snip -->

3. Echo the data directly to JavaScript

This is probably the easiest to understand, and the most horrible to use. Don't do this unless you know what you're doing.

Pros

  • Very easily implemented - It takes very little to implement this, and understand.
  • Does not dirty source - Variables are outputted directly to JavaScript, so the DOM is not affected.

Cons

  • Insecure - PHP has no trivial JavaScript escape functions, and they aren't trivial to implement. Especially when using user inputs, you are extremely vulnerable to second tier injections.Disputed see comments
  • Tightly couples PHP to your data logic - Because PHP is used in presentation, you can't separate the two cleanly.
  • Structured data is hard - You can probably do JSON... kinda. But XML and HTML will require special attention.

Implementation Example

Implementation is relatively straightforward:

<!-- snip -->  <script>      var data = <?php echo json_encode("42", JSON_HEX_TAG); ?>; //Don't forget the extra semicolon!  </script>  <!-- snip -->

Good luck!

What is a NullPointerException, and how do I fix it?

When you declare a reference variable (i.e. an object) you are really creating a pointer to an object. Consider the following code where you declare a variable of primitive type int:

int x;  x = 10;

In this example the variable x is an int and Java will initialize it to 0 for you. When you assign it to 10 in the second line your value 10 is written into the memory location pointed to by x.

But, when you try to declare a reference type something different happens. Take the following code:

Integer num;  num = new Integer(10);

The first line declares a variable named num, but, it does not contain a primitive value. Instead it contains a pointer (because the type is Integer which is a reference type). Since you did not say as yet what to point to Java sets it to null, meaning "I am pointing at nothing".

In the second line, the new keyword is used to instantiate (or create) an object of type Integer and the pointer variable num is assigned this object. You can now reference the object using the dereferencing operator . (a dot).

The Exception that you asked about occurs when you declare a variable but did not create an object. If you attempt to dereference num BEFORE creating the object you get a NullPointerException. In the most trivial cases the compiler will catch the problem and let you know that "num may not have been initialized" but sometime you write code that does not directly create the object.

For instance you may have a method as follows:

public void doSomething(SomeObject obj){     //do something to obj  }

in which case you are not creating the object obj, rather assuming that is was created before the doSomething method was called. Unfortunately it is possible to call the method like this:

doSomething(null);

in which case obj is null. If the method is intended to do something to the passed-in object, it is appropriate to throw the NullPointerException because it's a user error and the user will need that information for debugging purposes.

Alternatively, there may be cases where the purpose of the method is not solely to operate on the passed in object, and therefore a null parameter may be acceptable. In this case, you would need to check for a null parameter and behave differently. You should also explain this in the documentation. For example, doSomething could be written as:

/**@param obj An optional foo for ____. May be null, in which case   *  the result will be ____. */  public void doSomething(SomeObject obj){      if(obj != null){         //do something      } else {         //do something else      }  }

Finally, How to pinpoint the exception location & cause using Stack Trace

Resolved ( How to set a session in codeigniter 3 database? )

First of all CI3 session table and CI2 session table( Saving Session Data to a Database)structure is different

New session table structure

 CREATE TABLE IF NOT EXISTS `ci_sessions` (      `id` varchar(40) NOT NULL,      `ip_address` varchar(45) NOT NULL,      `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,      `data` blob NOT NULL,      PRIMARY KEY (id),      KEY `ci_sessions_timestamp` (`timestamp`)  );

Second They support old configuration variables with new configuration but it is better to use new configuration

$config['sess_driver'] = 'database';  $config['sess_cookie_name'] = 'ci_sessions';  $config['sess_expiration'] = 7200;  $config['sess_save_path'] = 'ci_sessions';//its your table name name  $config['sess_match_ip'] = FALSE;  $config['sess_time_to_update'] = 300;

See more details at their docs

Few new feature(function) available for session library.

Remember Don't forget it to load via autoload.php or loading $this->load->library('session');before use it.