Use same php & Ajax script twice on same page - Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Webmaster Tech > Programming > PHP Development

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-17-2011, 01:52 PM   #1 (permalink)
Junior Member
 
Join Date: Aug 2011
Posts: 1
Default Use same php & Ajax script twice on same page

Hello,

I have read a lot on this forum for a long time but not as registered member. Now I have a question that I can't find out so I hope you can help.

I have found a script that fully fits my needs and I have also changed it to read my database fields. My problem is that I want to include it twice on the same page but with different tables and column in the two versions. I have changed all database info in the code but it didn't help. The sub drop down isn't shown for the second version so I think I have to change more in the script but don't know where and to what...

I hope you can help... The code is:

Main.php
PHP Code:
<html>
<body>

<script type="text/javascript">
function AjaxFunction(cat_id)
{
var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
          try
                        {
                    httpxml=new ActiveXObject("Msxml2.XMLHTTP");
                    }
              catch (e)
                    {
                try
              {
              httpxml=new ActiveXObject("Microsoft.XMLHTTP");
              }
                catch (e)
              {
              alert("Your browser does not support AJAX!");
              return false;
              }
            }
  }
function stateck() 
    {
    if(httpxml.readyState==4)
      {

var myarray=eval(httpxml.responseText);
// Before adding new we must remove previously loaded elements
for(j=document.testform.subcat.options.length-1;j>=0;j--)
{
document.testform.subcat.remove(j);
}


for (i=0;i<myarray.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myarray[i];
optn.value = myarray[i];
document.testform.subcat.options.add(optn);


      }
    }
    var url="dd.php";
url=url+"?cat_id="+cat_id;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
  }
</script>

<form name="testform" method='POST' action='mainck.php'>Name:<input type=text name=fname>
Select first one <select name=cat onchange="AjaxFunction(this.value);">
<option value=''>Select One</option>
<?
require "config.php";// connection to database 
$q=mysql_query("select * from category ");
while(
$n=mysql_fetch_array($q)){
echo 
"<option value=$n[cat_id]>$n[category]</option>";
}

?>
</select>

<select name=subcat>

</select><input type=submit value=submit>
</form>

</body>
</html>
dd.php
PHP Code:
<?
$cat_id
=$_GET['cat_id'];
require 
"config.php";
$q=mysql_query("select * from subcategory where cat_id='$cat_id'");
echo 
mysql_error();
$myarray=array();
$str="";
while(
$nt=mysql_fetch_array($q)){
$str=$str "\"$nt[subcategory]\"".",";
}
$str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string
echo "new Array($str)";

?>
mainck.php

PHP Code:
<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>(Type a title for your page here)</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">

<?
while (list ($key,$val) = each ($_POST)) {
echo 
"\$$key = $val";
echo 
"<br>";


?>

</body>

</html>
config.php
PHP Code:
<?php

$dbservertype
='mysql';

// hostname or ip of server
$servername='localhost';

$dbusername='';
$dbpassword='';

$dbname='dd';

////////////////////////////////////////
////// DONOT EDIT BELOW  /////////
///////////////////////////////////////

connecttodb($servername,$dbname,$dbusername,$dbpassword);
function 
connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global 
$link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!
$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}

?>
menhir is offline   Reply With Quote
Sponsored Links
Old 09-04-2011, 12:10 AM   #2 (permalink)
Junior Member
 
Join Date: Sep 2011
Location: Long Islanf, NY, USA
Posts: 33
Default Re: Use same php & Ajax script twice on same page

Starting at

// Before adding new we must remove previously loaded elements

in your first block of code is where the data returned by the PHP code is put onto the page. You can duplicate all the AJAX code for the second location on the page, changing what's done with the returned data or you can add code to the stateck() function to fill HTML elements in both places on the page.

(Or, once you learn a little more programming, you can use jQuery's ajax() function to eliminate a lot of that Javascript code.)
Rukbat is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Sponsors

» Links

» Affiliates
Web Hosting
Online Backup Reviews
Marketing Find
Merchant Select
SiteMap Builder
Host Compare

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 05:00 PM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Webmaster Forums
Web Hosting | Chicago Web Hosting | Web Hosting