Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Sunday, August 2

jQuery for Validate Forms.

Validetta

Using callback functions
onValid() and onError()

Warning!
    To show error messages healthy, each field must be wrapped by an element like <div>

<div id="alert"></div>
<form id="exm" method="POST" action="#">
    <div>
        <label>Required, Email :</label>
        <input type="text" name="name" data-validetta="required,minLength[2],maxLength[3]">
    </div>
    <div>
        <label>Email :</label>
        <input type="text" name="email" data-validetta="required,email">
    </div>
    <button type="submit">Submit</button>
    <button type="reset">Reset</button>
</form>

<script>
    (function($){
        $('#exm').validetta({
            onValid : function( event ) {
                event.preventDefault();
                $('#alert').empty()
                    .append('<div class="alert alert-success">Nice, Form is valid</div>');
            },
            onError : function( event ){
                $('#alert').empty()
                    .append('<div class="alert alert-danger">Stop bro !! There are some errors.</div>');
            }
        }); 
    });
</script>


Plugin Download

Wednesday, May 20

Passing Values From JavaScript to PHP and Back

1. PHP to JavaScript

    <?php
        echo "Just wanted to say $hello";
    ?>

2. JavaScript to PHP

    <script>
        function sayHelloWorld() {
            var hello = "hello";
            var world = "world";
       
            window.location.href = "somepage.php?w1=" + hello + "&w2=" + world;
        }
    </script>
   
3. PHP Receiving and Processing The Variables

    <?php
    function sayHiBack() {
       // Check if we have parameters w1 and w2 being passed to the script through the URL
       if (isset($_GET["w1"]) && isset($_GET["w2"])) {
   
          // Put the two words together with a space in the middle to form "hello world"
          $hello = $_GET["w1"] . " " . $_GET["w2"];
   
          // Print out some JavaScript with $hello stuck in there which will put "hello world" into the javascript.
          echo "<script language='text/javascript'>function sayHiFromPHP() { alert('Just wanted to say $hello!'); }</script>";
       }
    }
    ?>

Sunday, February 22

Tools for faster, better web development

Over the years most web developers will have built an arsenal of tools or "tools of the trade". Recently I discovered rsync and I am surprised how I managed to live without it all these years. What tools do you consider to be the most compelling? Please stick to the ones you use on a regular basis and swear by. They can also be frameworks, platforms, editors and whatever else you think web developers ought to be using (jquery, joomla, xdebug, vi, notepad++, etc).

I'll start off with a couple:

1. rsync - 'One click' sync to live servers or vice-versa

2. mysqldump - used alongside rsync to sync the databases

3. test styles bookmarklet - live css editor bookmarklet which beats the heck out of the 'edit > save > reload' cycle by allowing live editing.

4. javascript shell - javascript shell window attached to a window

5. firebug - advanced javascript/css/dom debugger

6. php-shell - I use this regularly for quickly testing statements, functions, classes or scripts

7. CSS-Discuss Wiki - I'd be surprised if you couldn't find a solution to your CSS problem on this wiki (in which case you should add it)

8. Font Matrix - Helps me choose font stacks

9. PHPMyAdmin - I'm certain everyone uses this for managing their MySQL databases but thought I'd add it to the list for good measure


Even though I have highlighted tools in the LAMP environment, you may mention tools you use in your environment.


SOURCE: stackoverflow.com

Monday, February 2

Validate user login through JS(JavaScript), Ajax and PHP

    <script>       
    function validateLogIn()
    {
    
        var username  = $("#IdOfYourUserFiled").val();
        var password  = $("#IdOfYourPasswordFiled").val();
        $.ajax({                                      
            url: 'login.php',    //checking the login in                     
            data: {username:username,password:password}, //You have to pass user inputs to next page to validate in DB            
            type: "POST",     //Method by which data being transmitted
            dataType: 'json',                  
            success: function(data)          
            {
                if(data.flag === 0){
                    alert("Username or Password is incorrect");
                } else if (data.flag == 1){
                    window.open('index.php', '_self');  
                }
            }
        }); 
    }
    </script>
 
    login.php
 
    <?php
        /*Database Details */
        define('DB_HOST', 'localhost');
        define('DB_NAME', 'practice');
        define('DB_USER','root');
        define('DB_PASSWORD','');
        
        /* Database connectivity */
        $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error()); 
        $db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
        
        /* Username and Password */
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        
        $query = mysql_query("SELECT * FROM UserName where userName = '$username' AND pass = '$password'") or die(mysql_error()); 
        $row = mysql_fetch_array($query) or die(mysql_error());
        if(!empty($row['userName']) AND !empty($row['pass'])){
            print(json_encode(array('flag'=>1)));
        } else { 
            print(json_encode(array('flag'=>0)));
        }
        exit;
    ?>