Igranka u sigurnom prostoru

But, let's move on. Come on little to remind - [step-by-step]:

try { ...}, catch(err) and finally { ...}

Input Validation Example:

<script>
function myF() {
    var message, x;
    message = document.getElementById("p01102");
    message.innerHTML = "";
    x = document.getElementById("unesi").value;
    try { 
        if(x == "")  throw "is empty";
        if(isNaN(x)) throw "is not a number";
        x = Number(x);
        if(x > 100)   throw "is too high";
        if(x < 50)    throw "is too low";
    }
    catch(err) {
        message.innerHTML = "Unos " + err;
    }
    finally {
        document.getElementById("unesi").value = "";
    }
}
</script>

Primer Try it ... »

PHPObject

Class, function, create an object and show object properties. Example:

<?php
class Car {
    function Car() {
        $this->model = "Moj auto";
    }
}
// create an object
$herbie = new Car();

// show object properties
echo $herbie->model;
?>

Primer Try it ... »

Decode and Encode

Here is an example of base64_decode and base64_encode:

<?php
echo "Polazni string za encode:". "<br>";
$str = 'Under the Starlight This Night';

echo $str. "<br>". "<br>";
echo "base64_encode:". "<br>";
echo base64_encode($str). "<br>". "<br>";

echo "Ponovo, suprotno:". "<br>". "<br>";
echo "Polazni string za decode:". "<br>";
$str1 = 'VW5kZXIgdGhlIFN0YXJsaWdodCBUaGlzIE5pZ2h0';

echo $str1. "<br>". "<br>";
echo "base64_decode:". "<br>";
echo base64_decode($str1);
?>

Primer Try it ... »

header("Access-Control-Allow-Origin: *");

With header("Content-Type: application/json; charset=UTF-8");. Example:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("localhost", "", "", "dbName");

$result = $conn->query("SELECT user_id, user_name, user_password FROM tbl_user");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}
    $outp .= '{"Id":"'  . $rs["user_id"] . '",';
    $outp .= '"User":"'  . $rs["user_name"] . '",';
    $outp .= '"Password":"'. $rs["user_password"]     . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();

echo($outp);

$conn = new mysqli("localhost", "", "", "dbName_1");

$result = $conn->query("SELECT user_id, user_name, user_password FROM tbl_user_p");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}
    $outp .= '{"Id":"'  . $rs["user_id"] . '",';
    $outp .= '"User":"'  . $rs["user_name"] . '",';
    $outp .= '"Password":"'. $rs["user_password"]     . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();

echo($outp);
?>

Primer Try it ... »

Get Lat, Long from Address and Then ... /googlemaps/

Verzija PhP, with only jQuery :

<script>
window.onload=function(){
if(navigator.geolocation)
{
  navigator.geolocation.getCurrentPosition(showPosition);
}
else
{
  alert("Geolocation is not supported by this browser.");
}
}
function showPosition(pos){
   $.post('saver.php',{'lat':pos.coords.latitude,'lng':pos.coords.longitude},function(res){
      console.log(res);
      document.write(res);
   });
}
</script>

Primer Try it ... »

Arsort, $this and self, NoNStatic and Static_class

But, let's move on. Come on little to remind - [step-by-step]:

arsort();

<?php
$age = array();

$age["Peter"] = "35";
$age["Ben"] = "37";
$age["Joe"] = "43";

arsort($age);

foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "
"; } ?>

Primer Try it ... »

Usage of $this and self /1

Here is an example of correct usage of $this and self :

<?php
class X {
    private $non_static_member = 1;
    private static $static_member = 2;

    function __construct() {
        echo $this->non_static_member . ' '
           . self::$static_member;
    }
}

new X();
?>

Primer Try it ... »

Usage of $this and self /2

Here is an example of incorrect usage of $this and self

<?php
class X {
    private $non_static_member = 1;
    private static $static_member = 2;

    function __construct() {
        echo self::$non_static_member . ' '
           . $this->static_member;
    }
}

new X();
?>

Primer Try it ... »

Usage of $this and self /3

Here is an example of polymorphism with $this for member functions:

<?php
class X {
    function foo() {
        echo 'X::foo()';
    }

    function bar() {
        $this->foo();
    }
}

class Y extends X {
    function foo() {
        echo 'Y::foo()';
    }
}

$x = new Y();
$x->bar();
?>

Primer Try it ... »

XMLHttpRequest - then

Word loading then:

<script>
function get(url) {
  return new Promise(function(succeed, fail) {
    var req = new XMLHttpRequest();
    req.open("GET", url, true);
    req.addEventListener("load", function() {
      if (req.status < 400)
        succeed(req.responseText);
      else
        fail(new Error("Request failed: " + req.statusText));
    });
    req.addEventListener("error", function() {
      fail(new Error("Network error"));
    });
    req.send(null);
  });
}

function getJSON(url) {
  return get(url).then(JSON.parse);
}
</script>

<script>
  function showMessage(msg) {
    var elt = document.createElement("div");
    elt.textContent = msg;
    return document.body.appendChild(elt);
  }

  var loading = showMessage("Loading...");
  getJSON("example/sloba.json").then(function(sloba) {
    return getJSON(sloba.gde_da);
  }).then(function(gde_da) {
    return getJSON(gde_da.majka);
  }).then(function(majka) {
    showMessage("Da, ime moje majke : " + majka.name);
  }).catch(function(error) {
    showMessage(String(error));
  }).then(function() {
    document.body.removeChild(loading);
  });
</script>

Primer Try it ... »

Kontakt



Sve potrebne informacije preko      i    .