Thursday, 11 August 2016

Yii2.0 DropDownList

Default DropDownList

Yii2.0 default dropdownlist syntaxt.
1<?php echo $form->field($model'name[]')->dropDownList(['a' => 'Item A''b' => 'Item B''c' =>'Item C']); ?>
Yii2.0 default dropdownlist syntaxt with prompt text like default text value.
1<php echo $form->field($model'name[]')->dropDownList($listData,
2                        ['prompt'=>'Select...']);>
Using below code, we can display the database value (via model) in dropdownlist of yii2.0 framework.
01<?php
02//use app\models\Country;
03$countries=Country::find()->all();
04
05//use yii\helpers\ArrayHelper;
06$listData=ArrayHelper::map($countries,'code','name');
07
08echo $form->field($model'name')->dropDownList(
09                                $listData,
10                                ['prompt'=>'Select...']);
11?>

Default Selection DropDownList Yii2

Using below code, we can set the default value for dropdownlist of yii2.0 framework. When we create the new entry for country population, we will try to set the value to auto select of dropDownList. During the updation it will fetch the country code automatically from db record. Please see the below code to get it.

Controller Code

01<??php
02...................
03public function actionCreate()
04{
05    $model new Population();
06    // Default Selection for country code
07    $model->country='IN';
08    if ($model->load(Yii::$app->request->post()) && $model->save()) {
09            return $this->redirect(['view''id' => $model->code]);
10    else {
11        return $this->render('create', [
12            'model' => $model,
13        ]);
14    }
15}
16public function actionUpdate($id)
17{
18    $model $this->findModel($id);
19    return $this->render('update', [
20        'model' => $model,
21    ]);
22}
23...................
24?>

Create/Update Form Code

01<??php
02use yii\helpers\Html;
03use yii\widgets\ActiveForm;
04use yii\helpers\ArrayHelper;
05?>
06
07<?div class="population-form">
08    <??php $form = ActiveForm::begin(); ?>
09    ...................
10    <??php
11        //$model->name='IN';
12        $listData=ArrayHelper::map($countries,'code','name');
13        echo $form->field($model'name')->dropDownList($listData, ['prompt'=>'Choose...']);
14    ?>
15    ...................
16    <??php ActiveForm::end(); ?>
17<?/div>


Yii2.0 DropDownList

Monday, 8 August 2016

Google New reCaptcha using PHP, Are you a Robot?

Get reCaptcha Key
Click here to create a Google reCaptcha application. 

Register Your Website
Give your website domain details without http:
Google New reCaptcha using PHP - Are you a Robot?

Google Site Key
You will use this in HTML code. 
Google New reCaptcha using PHP - Are you a Robot?

Google Secret Key
This will help your website to communication with Google. 
Google New reCaptcha using PHP - Are you a Robot?


HTML Code
Contains simple HTML code with Google reCaptcha widget snippet. Here you have to modify the Google Site Key value.
<html>
<head>
/* Google reCaptcha JS */
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form action="" method="post">
Username
<input type="text" name="username" class="input" />
Password
<input type="password" name="password" class="input" />
<div class="g-recaptcha" data-sitekey="Google Site Key"></div>
<input type="submit"  value="Log In" />
<span class='msg'><?php echo $msg; ?></span>
</form>
</body>
</html>

Google New reCaptcha using PHP - Are you a Robot?


index.php
Contains PHP code, here you have to modify the Google Secret Key.
<?php
include("db.php");
session_start();

$msg='';
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$recaptcha=$_POST['g-recaptcha-response'];
if(!empty($recaptcha))
{
include("getCurlData.php");
$google_url="https://www.google.com/recaptcha/api/siteverify";
$secret='Google Secret Key';
$ip=$_SERVER['REMOTE_ADDR'];
$url=$google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
$res=getCurlData($url);
$res= json_decode($res, true);
//reCaptcha success check 
if($res['success'])
{
//Include login check code
}
else
{
$msg="Please re-enter your reCAPTCHA.";
}

}
else
{
$msg="Please re-enter your reCAPTCHA.";
}

}
?>

Login Check Code
This code will verify username and password details in database.
$username=mysqli_real_escape_string($db,$_POST['username']);
$password=md5(mysqli_real_escape_string($db,$_POST['password']));
if(!empty($username) && !empty($password))
{
$result=mysqli_query($db,"SELECT id FROM users WHERE username='$username' and passcode='$password'");
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result)==1)
{
$_SESSION['login_user']=$username;
header("location: home.php"); //Success redirection page. 
}
else
{
$msg="Please give valid Username or Password.";
}

}
else
{
$msg="Please give valid Username or Password.";
}

getCurlData.php
CURL function for Google reCaptcha verification. Enable php_curl extension inphp.ini configuration file.
<?php
function getCurlData($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
?>


db.php
Database configuration file, modify username, password and database values. 
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>


Google New reCaptcha using PHP, Are you a Robot?

Thursday, 4 August 2016

jQuery removeClass()

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").removeClass("intro");
    });
});
</script>
<style>
.intro {
    font-size: 120%;
    color: red;
}
</style>
</head>
<body>

<h1>This is a heading</h1>

<p class="intro">This is a paragraph.</p>
<p class="intro">This is another paragraph.</p>

<button>Remove the "intro" class from all p elements</button>

</body>
</html>






jQuery removeClass()