473,414 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Ajax popup php

78
Hi friends,
I need to create a popup box in my php website. It is a product display page. when click on a product I want to display corresponding product details in a popup box. Is it possible by ajax popup box? how to do that.
please help me
Jun 20 '13 #1
5 7414
Claus Mygind
571 512MB
Yes it is possible.

1) create a hidden <div> on your form

2) On the main form create a link <a> to your ajax call. Include the record key info on the link you wish to transmit to the web server/data server that you wish to have returned.

3) In your attached .js file create the ajax call and response. In the ajax call query send the key for the lookup to your server side response php app.

4) On your server side create a php response app

5) Your server side app looks up the record information and transmits back via a simple "echo" command.

6) Your client side app will then receive the information. In the response section of your ajax call add the information retrieved to the hidden <div> and make the <div> visible.

7) add a "close" link on the popup to rehide the popUp.

I would recommend that you send the information via JSON. This is very easy on each side client and server you just add a "encode" and "decode" line for each.

Since this is a lot of sample code to display, if there are parts of this you need help with, just post those specific questions for the parts of code you need help with.
Jun 20 '13 #2
Claus Mygind
571 512MB
Ok here is some sample code that is a form with 2 <div>s one of which is hidden. The hidden <div> will popup when the detail info is retrieved from the server.

The code is untested but a generic ajax call I use to both process entire forms or just make a small call for details to update the form as you are requesting here.

I have included the javaScript code on the form which most likely you would store in a separate .js file

In the javaScript code I create an ajax object which I use to both send and receive data.

Maybe this will get you started.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3.     <head>
  4.         <title>Sample PopUp Box</title>
  5.         <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  6.  
  7.         <script language="JavaScript">
  8.             function getItem(lineNo){
  9.                 /*
  10.                 ----------------------------------------------
  11.                 data object holds data we want to transmit to
  12.                 the server side app
  13.                 ----------------------------------------------
  14.                 */
  15.                 var dataObj = new Object();
  16.                 dataObj['table'] = "myDetails";
  17.                 dataObj['search'] = lineNo;
  18.  
  19.                 /*fake form node created so action can be assigned*/
  20.                 var fNode = new Object();
  21.                 fNode.action = "/<your_path>/<your_detail_app>.php";
  22.                 fNode.id = "fetchDetails";
  23.  
  24.                 ajax.makeCall(fNode, dataObj);
  25.             }
  26.  
  27.             function updatePage(form, aCallBack)
  28.             {
  29.                 /*
  30.                 ----------------------------------------------------------------
  31.                 the returned detail information is loaded into the <span></span>
  32.                 then the hidden <div> is made visible.
  33.                 ----------------------------------------------------------------
  34.                 */
  35.                 document.getElementById("details").innerHTML = aCallBack['details'];
  36.                 document.getElementById("ratesDiv").style.visibility = 'visible';
  37.             }
  38.  
  39.             /*
  40.             --------------------------------------------------------------------------------
  41.                 ajaxCall
  42.  
  43.                     Author:    Claus Mygind
  44.                     Date:    12/20/10
  45.  
  46.                     Purpose:    Custom Object which adds a generic ajax call to the app.
  47.                                 To get information or save information from the data base.
  48.  
  49.                     usage:
  50.  
  51.                         //create ajax object
  52.                         var ajax = new myAjax();
  53.  
  54.                           made at time when getting or storing data 
  55.                           takes 2 parameters
  56.                             form        =    the calling form object
  57.                                 form.action is the server side app /path/appname.php
  58.                                         that will process data  or request
  59.  
  60.                             dataObj        = request paramters for data retrieval 
  61.                                             or form data to be saved
  62.  
  63.                         example:
  64.  
  65.                         ajax.makeCall(document.getElementById("form"), dataObj);
  66.  
  67.                         data is returned in an array aCallBack which is used in 
  68.                         "updatePage(form, aCallBack)"    is passed two parameters 
  69.                                 1) the form to be updated 
  70.                                 2) the data returned
  71.  
  72.                                 updatePage(form, aCallBack)
  73.  
  74.                         Because the data is specific to each page
  75.                         the updatePage() function is located in a separate file unique
  76.                         to the calling page.
  77.             --------------------------------------------------------------------------------
  78.             */
  79.  
  80.  
  81.             /*
  82.             ------------------------------------------------
  83.             the method myAjax is a proto type.
  84.             it takes 2 parameters.
  85.                 form    = the form object.
  86.                 dataObj = data values collected from the form
  87.  
  88.             myAjax() will send data in a JSON format to 
  89.             the the server and process the response from 
  90.             the server based on the 3 parameters submitted.
  91.             ------------------------------------------------
  92.             */
  93.             function myAjax (form, dataObj ) {
  94.  
  95.                 /*
  96.                 ---------------------------------
  97.                 aCallBack is an array used 
  98.                 to store the ajax call response
  99.                 ---------------------------------
  100.                 */
  101.                 var    aCallBack = new Array();
  102.  
  103.                 this.makeCall = function (form, dataObj) {
  104.  
  105.                     /*
  106.                     -----------------------------------------------------
  107.                     newer browsers already contain JSON code.
  108.                     older browsers may need an addon library
  109.                     from json.org use the JSON2.js library for this.
  110.  
  111.                     create queryString by making data array "dataObj"
  112.                     a JSON formatted string. This will send all the 
  113.                     form's data in one variable "appData" to the server.
  114.                     -----------------------------------------------------
  115.                     */
  116.                     var queryString  = "appData="+JSON.stringify(dataObj);
  117.  
  118.                     /*
  119.                     -----------------------------------
  120.                     define the XMLHttpRequest object.
  121.                     note the iif conditions that test
  122.                     if this is IE or some other type
  123.                     of browser
  124.                     -----------------------------------
  125.                     */
  126.                     var request = window.ActiveXObject ?
  127.                         new ActiveXObject('Microsoft.XMLHTTP') :
  128.                         new XMLHttpRequest;
  129.  
  130.                         /*
  131.                         ----------------------------------------------
  132.                         create URL. Note the timeStamp, it is used
  133.                         to prevent the browser from using caches data
  134.                         each request will be unique.
  135.  
  136.                         The first parameter "callApp" is used here to
  137.                         specify which server side app to process this
  138.                         request.
  139.                         ----------------------------------------------
  140.                         */
  141.                         var url = form.action+'?timeStamp=' + new Date().getTime();
  142.  
  143.                         /*
  144.                         ---------------------------------------------------
  145.                         open the connection to the server via a POST method
  146.                         ---------------------------------------------------
  147.                         */
  148.                         request.open("POST", url, true);
  149.                         /*
  150.                         ---------------------------------------------------
  151.                         the "onreadystatechange" will activate when there is
  152.                         a response from the server.
  153.  
  154.                         it will call the "processResponse()" method.
  155.                         processResponse() receive 2 parameters
  156.  
  157.                         request.responseText = this could 
  158.                                                     a message, or
  159.                                                     data returned, or
  160.                                                     an error message
  161.  
  162.                         form = this was the 1st parameter passed to myAjax.
  163.                                 it uses the form's id to clear the form upon 
  164.                                 completion.  
  165.                                 It is a    pass through parameter here to the 
  166.                                 "updatePage(form)" method to follow.
  167.                         ---------------------------------------------------
  168.                         */
  169.                         request.onreadystatechange = function chkStateChange(){
  170.                                                         if (request.readyState == 4) {
  171.                                                             if (request.status == 200) {
  172.                                                                 processResponse(request.responseText, form);
  173.                                                             }
  174.                                                         }
  175.                                                      };
  176.                         request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  177.  
  178.                         /*
  179.                         ---------------------------------------------------
  180.                         with the connection opened above,
  181.                         we can now send the data "queryString"
  182.                         to the server.
  183.                         ---------------------------------------------------
  184.                         */
  185.                         request.send( queryString );
  186.                 };
  187.  
  188.                 /*
  189.                 --------------------------------------------------------------------------
  190.                 processResponse when data is returned from onreadystatechange.
  191.  
  192.                 If an error, save or deleted message is detected, display a simpe alert. 
  193.                 Otherwise pass the data onto the "updatePage()" method.
  194.                 --------------------------------------------------------------------------
  195.                 */
  196.                 var processResponse = function (response, form) {
  197.  
  198.                     aCallBack = response.split(';');
  199.  
  200.                     /* display error or save message */    
  201.                     if ( aCallBack[0].indexOf("An error occured") > -1 ||
  202.                          aCallBack[0] == "Record Saved"    ||
  203.                          aCallBack[0].indexOf("Delete Notice!!") > -1     ||
  204.                          aCallBack[0] == "The data you saved was the same as found in the database!"
  205.                        ) { 
  206.  
  207.                         eAlert  = aCallBack[0]+"\n";
  208.                         for (var i = 1; i < aCallBack.length; i++ )
  209.                         {
  210.                             eAlert += aCallBack[i]+"\n";
  211.                         }
  212.                         alert( eAlert );
  213.                         if (aCallBack[0] == "Record Saved" ||
  214.                             aCallBack[0].indexOf("Delete Notice!!") > -1 ||
  215.                             aCallBack[0].indexOf("Deletes are not allowed") > -1
  216.                             )
  217.                         {
  218.                             initilizeForm(form);
  219.                         }
  220.                     }else{
  221.                         /*
  222.                         ------------------------------------
  223.                         updatePage()
  224.                             sending two parameters
  225.                                 form 
  226.                                 aCallBack
  227.                         updatePage() is a unique method
  228.                         in each html page. The function
  229.                         is located in the ...Functions.js
  230.                         for that specific page.  This will
  231.                         allow for custom processing of 
  232.                         the returned data for each page
  233.                         ------------------------------------
  234.                         */
  235.                         aCallBack = JSON.parse(response);    
  236.                         if ( aCallBack[0] == "No records found")
  237.                         {
  238.                             alert("No records found");
  239.                         }else{
  240.                             updatePage(form, aCallBack);
  241.                         }
  242.                     }
  243.                 };
  244.  
  245.             }
  246.             /*
  247.             ----------------------------------
  248.             ajax object created after 
  249.             proto type myAjax() is constructed
  250.             by binding ajax to myAjax
  251.             ----------------------------------
  252.             */
  253.             var ajax = new myAjax();
  254.         </script>
  255.     </head>
  256.     <body>
  257.         <form id="mainForm">
  258.  
  259.             <div id="mainDiv">
  260.                 <table>
  261.                     <tr><td><a href="#" onclick="getItem(1);" >show details</a> item 1</td></tr>
  262.                     <tr><td><a href="#" onclick="getItem(2);" >show details</a> item 2</td></tr>
  263.                     <tr><td><a href="#" onclick="getItem(3);" >show details</a> item 3</td></tr>
  264.                     <tr><td><a href="#" onclick="getItem(4);" >show details</a> item 4</td></tr>
  265.                     <tr><td><a href="#" onclick="getItem(5);" >show details</a> item 5</td></tr>
  266.                 </table>
  267.             </div>
  268.  
  269.             <div id="detailPopUp" style="position:fixed; border:5px ridge #006600; top :25; left:250; height:300; width:700px; overflow: scroll; background-color: #ccffcc; background-image:linear-gradient(to top, #ccff00 0%, White 100%); z-index:99; visibility: hidden;">
  270.                 <span id="details"></span><br />
  271.                 <input type="button" value="Close" onclick="document.getElementById('detailPopUp').style.visibility = 'hidden';" />
  272.             </div>
  273.         </form>
  274.     </body>
  275. </html>
  276.  
Jun 20 '13 #3
Ammu
78
@Claus Mygind
thank you for your response. let me try with this sample code.
Jun 21 '13 #4
Thanks claus, your concept is too easy and good, i have required this logic.
Jun 21 '13 #5
Claus Mygind
571 512MB
Ok so now you have the javaScript side of the equation. Here is the php part of the solution.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* sample response page - 
  3.  
  4.        Author: Claus Mygind
  5.  
  6.        Date: 06/22/13 - php
  7.  
  8.        It receives one parameter:
  9.        $_REQUEST['appData']
  10.  
  11.        It returns one parameter:
  12.        $res_array
  13.  
  14. */
  15. try  // Error trap entire applet.; 
  16. {
  17.     /*
  18.     -------------------------------------------------------------
  19.     re-code input parameters from $_REQUEST into a $inArray array
  20.     $inArray can be any name you want.
  21.     -------------------------------------------------------------
  22.     */
  23.     $inArray = json_decode($_REQUEST['appData'], true);
  24.  
  25.     // Create connection
  26.     $dbx=mysqli_connect("<Either a host name or an IP address>","<user>","<password>",$inArray["myDetails"]);
  27.  
  28.     /* check connection */
  29.     if (mysqli_connect_errno()) {
  30.         throw new Exception("Connection to database failed!");
  31.     }else{
  32.         /*build sql query*/
  33.         $sql = 'select thisDetail from '.$inArray['myDetails'];
  34.  
  35.         /*query for detail information*/
  36.         if ( $result = $dbx->query($sql) )
  37.         {
  38.             if ($result->num_rows == 0)
  39.             {
  40.                 throw new Exception("No detail record was not found!");
  41.             }else{
  42.                 /* create and load a response array */
  43.                 $res_array = array();
  44.                 while ($row = $result->fetch_array()) 
  45.                 {
  46.                     $res_array['details']= $row["thisDetail"];
  47.                 }
  48.             }
  49.         }else{
  50.             throw new Exception("Query to get details failed!");
  51.         }//end if query failed
  52.  
  53.     }//end if connection failed
  54. }// end try
  55.  
  56. catch (exception $e) 
  57. {
  58.   $res_array = array( 'errorAlert'=>'An error occured<br/>','errorMsg'=>$e->getMessage());
  59. }
  60.  
  61. echo json_encode($res_array);
  62. $dbx->close();
  63. ?>
Jun 22 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rob Meade | last post by:
Hi all, I played with my first bit of AJAX the other week and was pleasantly surprised that I achieved my goal..now I'd like to try something else.. Question... If I have an updatePanel,...
1
by: Samuel Rhodes | last post by:
hi i am using a ajax modal popup to allow the user to search an item and select the required item from the list. whenever i try to search the item, which causes a post back....the modal popup...
0
by: =?Utf-8?B?QWxCcnVBbg==?= | last post by:
I have a situation in which I need to check for data in either of two fields and display an alert if neither field contains data or if both fields contain data when the user clicks on a View...
3
by: Victor | last post by:
Hi guys. I want to implement a customized user webcontrol (.ascx). Inside this control I have a dropdownlist and a button. when user click the button, it will open a pop up window(not Ajax popup...
1
by: amalmraj | last post by:
Dears I making an web page that have a dropdown box and that have some names. also the first item is <New Person>. my need is if i select the <new personthen the new popup window should come...
1
by: mbruyns | last post by:
i have been trying (and sometimes succeeding) to use the modalpopupextender to show various panels of controls on my asp pages. the strange problem that i keep on running into is that sometimes it...
9
by: Trapulo | last post by:
Hello, with ASP.NET 2.0 Ajax every unexpected error is managed client-side with a popup that reports the error to the user. In ASP.NET 3.5 this behavor has been changed: how can I have a similar...
0
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am using a popup control extender (ajax control) and have placed a .net button on the panel that is displayed when the hyperlink is selected that is the target control of the popup extender. ...
6
by: SAL | last post by:
hello, I'm using a radiobuttonlist in an updatepanel in an item template in a Gridview control. I'm populating the radiobuttonlist in the RowDataBound event. I have the control toolkit registered...
2
rahulephp
by: rahulephp | last post by:
I am looking for the PHP Ajax mouse move popup box. It should show a small cool looking popup window when i move mouse on "Seller" link. I saw this thing here: Move the mouse on "Sellers" (next...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.