JavaScript Code Mixed-up with PHP Code

Sometimes we need to add something in JavaScript code but unfortunately on other side we are using PHP in same file and the value of some one variable is in PHP code.

Suppose


<?php 

$invoiceno = 2343434; // suppose we have value of one variable in PHP code but we want to use it in JavaScript Code

// so this is JavaScript Code but in the form of PHP while using PHP variable value.

 echo "<SCRIPT LANGUAGE='JavaScript'>
       window.alert('Invoice Posted!')
       window.location.href='sales.php?inv=".$invoiceno."';
       ";  



?>

PHP: User Information on Web Page

When a visitor visit web page, he can his specification like IP Address, Country Name, Country Flag, City etc.
The following code is available for PHP developers.

<?php

// IP Address
$user_ip = getenv('REMOTE_ADDR');
echo $user_ip; 

// Note: use 'echo' to display the IP Address of user or you can insert into database for record. Similarly for others one...

// Country Name
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$country = $geo["geoplugin_countryName"];


// City Name
$city = $geo["geoplugin_city"];

// Time (with proper time zone)
date_default_timezone_set("Asia/Karachi"); 
$time = date('Y-m-d'); // Date as per the region of Karachi, Pakistan Time Zone.
$time2= date('h:i:s A'); // Time as per the region of Karachi, Pakistan Time Zone.

On Select, Fill The Div with Some Results (with the help of PHP and JavaScript)

Website and web application developers need to select an input to have some results in a div dynamically. So every option of selector would display results in the target div. These results would be scripted in another file.
Now how is it possible?

First need to create a selector having some options and a div which would be filled with the results.


Now JavaScript Code and would declare a file from where the result would be transferred to the target div.


Database Connection to Connect with MySql through PHP Programming language

Whenever we insert, update, delete or retrieve data from MySql database, we need database connection. Different programming languages have different syntax to interact with database.

Database Connection with MySql through PHP Programming Language 

There are different drivers of PHP which are used to interact with MySql database.

<?php

  
                                            
// DATABASE CONNECTION //

//Specifications

        $DB_host = "hostname";
        $DB_user = "user_database";
        $DB_pass = "passowrd";
        $DB_name = "database_name";


//After having defined DNS information, now you can use any driver's connection. I wrote here all connection' codes.

// 1. 
                                            
        $sqliconnect = mysqli_connect($DB_host, $DB_user, $DB_pass, $DB_name);


//2. 



        
        try {
            $conn = new PDO('mysql:host=$DB_host;dbname='.$DB_name.'', $DB_user, $DB_pass);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
        }
        
//3. 

        

        $SQLConn = new MySQLi($DB_host, $DB_user, $DB_pass, $DB_name);

        if ($SQLConn->connect_errno) {
        die("ERROR : -> " . $SQLConn->connect_error);
        }
                

//4.      
                
             
        // Create connection
            $newsqliconn = new mysqli($DB_host, $DB_user, $DB_pass, $DB_name);
        // Check connection
        if ($newsqliconn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
        }
?>

How Cookies to be Implemented in PHP Language

Session variable store values on the server side while cookie variable store on the user side (on computing device).

<?php
if(isset($_POST['submit'])){
setcookie('remember_me',$_POST['ci'], time()+60*60*24*365, '/');
}
?>

<div>

 <?php echo 'Hello'.$_COOKIE['remember_me']; ?>
<form method="post" action="">
<input name="user_id" type="tex" />
<br>
<input type="submit" />

</form>

</div>

Time and Date in PHP Programming Language

It is very  important to have time and date in website and applications, so here we would describe the method to display the time and date in PHP Programming language.

Date Without Time

  • Current Date
echo date("Y-m-d");
  • Current Year
echo date("Y");
  • Current Month
echo date("m");
  • Current Day/Date
echo date("d"); // result: If today is 2019-01-28 so result would be 28.

or
echo date("1");

We can describe it into words;

echo date("D"); // result: Suppose today is Thursday so result would be Thu.
  • First Date of Current Month
echo date("Y-m-01");
  • Last Date of Current Month
echo date("Y-m-t");

Time

  • Current Time
echo date("h:i:sa");

Date with Time

  • Current Date and Time
echo date("Y-m-d h:i:sa");

Date Format

  • 1st Example
echo date("Y/m/d");
  • 2nd Example is One of Complete Format of Date and Time
echo date("l jS \of F Y h:i:s A");

Time Zone

Before displaying date in PHP programming language, need to define time zone as under;

It is compulsory to define time zone prior to display date.

date_default_timezone_set("Asia/Bangkok");
echo date("Y-m-d");

Time zone name can be printed.

echo date_default_timezone_get();

Same value is stored in variable and displayed or inserted into database. Later it can be retrieved as per requirement.  If there is need to select data between two dates, the following query is used;

select * from table_name where date_column between $start_date and $end_date

If within ‘where’ clause,  there are more queries than one, so describe in this way;

select * from table_name where (date_column between $start_date and $end_date) and column_other = 'value_required'

(Note: The above ‘between’ condition is not only for date range but for range of any kind of data (in number).

Get Subscribed for E-News Letter and Updates
Our RSS Link
https://primeburg.com/feed/