Showing posts with label ajax. Show all posts
Showing posts with label ajax. Show all posts

Wednesday, January 8, 2014

ASP.net page methods with argument passing

Related to ASP.net, in some cases we need to call server side methods using client side javascript functions with Ajax. The most efficient way to implement that facility is called page methods. I have found a very successful Blog by related to implementing this page methods. This blog is a small extension of the above mentioned blog.
In practical situations we may need to pass parameters to the server-side from client-side. How can we do that using the same example described in the above mentioned blog.

Let's assume that we need to add two numbers. The numbers are going to input by the user via a text input in HTML.Addition will be done in server-side (by a C# method in ASP.net) after we click a button. (Ajax technology will be used)

  • Define 2 text inputs in HTML

<input id="Text1" type="text" />
<input id="Text2" type="text" />

  • Define a HTML button and calls to a function in the onclick event

<input id="Button1" type="button" value="button" onclick="inputNum()"/>

  • Define a ASP.net script manager in client-side (just drag and drop the controller from the tool box)

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" EnablePartialRendering="true"> </asp:ScriptManager>

  • Define following functions in the client-side using Javascript. (Here I use an array to pass the two numbers as arguments to the server-side method)

function inputNum() {

            var nums = new Array();
            nums[0] = document.getElementById("Text1").value;
            nums[1] = document.getElementById("Text2").value;
            GetInputNumFromServer(nums);
        }

        function GetInputNumFromServer(numsArray) {
            PageMethods.GetSum(numsArray, OnSuccess, OnError);

            return false;
        }

        function OnSuccess(response) {
            alert(response);
        }
        function OnError(error) {
            alert(error);
        }



  • Define the C# method in the server-side. (You have to use the System.Web.Services reference)

 [WebMethod]
        public static int GetSum(int[] arr)
        {
            return arr[0]+arr[1];
        }



That's All.

Wednesday, July 17, 2013

Loading a separate page to a DIV using AJAX

In some occasions we need to load a separate page or pages into a div tag without reloading the whole current page. That functionality can be implemented by using AJAX technology.

According to the below code xxxx.php page will be loaded to the div which has the id=content at the load event of the current page.


<html>

        <head>

       //Importing the Jquery library. Tgis can be through offline or online.
       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>

         <script>
                    function formLoad(){
                              $('#content').load('xxxx.php'); //AJAX function to load the page.

                      }
        
         </script>

       </head>

       <body OnLoad="formLoad()"> //Call the formLoad() on load event

                 <div id="content"></div>//div to where the external page will be loaded


       </body>

</html>

Wednesday, May 15, 2013

How to submit data with Ajax

Hi ! This post is about a way that you can submit (to a PHP Script) a collection of data which have been collected by a HTML form . Here we are going to do it with Ajax technology. Even though this is a simple post you are required to have a somewhat knowledge in Java Script and PHP for a better understanding.
  1. Visit the JQuery site and download the JQuery API. ( Direct link to the API. Just save the page as .js)
  2. Link the JQuery API with your web page where the form is located.
  3. Write a Java Script function to collect data from the form by the IDs of the form items and send them to the PHP script. 

Form.php

<html>
<head>
    <title></title>
    <script type="text/javascript" src="jScript/jquery-1.9.1.min.js"></script> <!-- Link to the JQuery API-->
    <script>
    <!--JavaScript Function-->
    function formSubmit(){

       $.ajax({ // create an AJAX call...
        type: "POST",  // Submitting Method is POST
    url: "collectData.php", // To where the data should be submitted 
    data: { firstName: $('#fName').val(),    //Value in the textbox which is identified by ID 'fName' will be transmitted to variable firstName in the PHP script
            lastName: $('#lName').val(), //Value in the textbox which is identified by ID 'lName' will be transmitted to variable lastName in the PHP script
            age: $('#age').val(), // Value in the textbox which is identified by ID 'age' will be transmitted to variable age in the PHP script           
        },
        success: function(response) { // on success..
                alert(response);   // parameter response retrives any data which are echo by the PHP script
        }
    });
    return false;
    }
    </script>
   
</head>
    <body>
            <form onsubmit="return formSubmit()">  <!-- formSubmit() will be called at the event of click on the submit button of the particular form -->   
                <table>
                    <tr>
                    <td>First Name :</td>
                    <td><input type="text" id="fName"></td>
                    </tr>
                    <tr>
                    <td>Last Name :</td>
                    <td><input type="text" id="lName"></td>
                    </tr>
                    <tr>
                    <td>Age :</td>
                    <td><input type="text" id="age"></td>
                    </tr>
                    <tr>
                    <td></td>
                    <td><input type="submit" value="Submit"></td>
                    </tr>
                </table>
            </form>
    </body>
</html>




collectData.php

<?php   

$firstName=$_POST['firstName']; //Catch the data from form.php and store it in the particular variable
$lastName=$_POST['lastName'];
$age=$_POST['age'];

//Now we have stored the required data in the PHP script. Anything can be done on that data with PHP.


echo("Success"); // This statement will be returned to the parameter response.

?>



With this method you will not be re-directed to the PHP file as the traditional way. Have fun !!!!!