ajax issue in zencart - php

I have some main categories and subcategories of each main category..I've a dropdown list contains main category, when i choose a main category then the subcategory dropdown list shows the subcategories of that main category..
am using the following code for this, but this shows the subcategory box contains the whole page with header and footer...
<select name="main_category" id="main_category" onchange="showSubCategory(this.value)">
<option>--Select--</option>
</select>
<script type="text/javascript">
function showSubCategory(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
document.getElementById("subcategory").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","index.php?main_page=getsubcategory&cid="+str,true);
xmlhttp.send();
}
</script>
in tpl_subcategory_default.php contains
<?php
$cid=$_GET['cid'];
$sql="select cd.categories_name, cd.categories_id
from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
where c.parent_id = '" . (int) $_GET['cid'] . "'
and c.categories_id = cd.categories_id
and c.categories_status= 1";
$r=mysql_query($sql);
while($row=mysql_fetch_array($r))
{
echo "<option value=$row[categories_id]>$row[categories_name]</option>";
}
?>

It's showing the whole page with header and footer because you're accessing the "page" via index.php?main_page=foo but haven't added architecture to replace the normal templating system outputs with your own page-specific output ... ie: to jump directly to the output without first calling the normal things which appear on every page.
Your question can't really be accurately answered without knowing also what you've done in the /includes/modules/pages/subcategory/header_php.php file, or if you've even created one. Chances are that the code you've put into tpl_subcategory_default.php could go into the header_php.php file mentioned above, followed by a die() statement at the end, and accomplish the same thing which you seem to be looking for.
It would be easier to answer your question fully if you supplied more information about what you've done so far.

To remove header,footer etc you can override the tpl_main_page.php. Go to this directory /includes/templates/custom template. As per your information you have created main_page=getsubcategory page. so create a folder named getsubcategory under this directory. then copy tpl_main_page.php from includes/templates/custom template/common/ and paste it in /includes/templates/your custom template/getsubcategory. Then do the below changes in tpl_main_page.php file.
if (in_array($current_page_base,explode(",",'getsubcategory')) ) {
$flag_disable_left = true;
$flag_disable_header = true;
$flag_disable_footer = true;
}

Related

Update HTML table every 5 seconds with mySQL, and PHP?

I have a simple PHP page that sends a query to a mySQL database and displays the data onto a HTML table.
How can I get it to do this every 5 seconds? Currently it does this upon the page loading. The database will have changing data, so am trying to get the code to refresh every 5 seconds to keep the table updated.
How might I go upon doing this?
You need to use a client-side scripting language such as JavaScript to have a webpage do something after it has been served to a client.
Specifically, you will want to use timers and AJAX calls. You may also find the jQuery library useful for abstracting AJAX calls however it is not a requirement.
You need to use javascript ajax. For example ,
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText; // your div
}
}
xmlhttp.open("GET","getdatabase.php",true); //your php file
xmlhttp.send();
}
window.setInterval(function(){
loadXMLDoc();
}, 5000);
</script>
Then in html , you must have a div with specified ID(myDiv) Fpr example:
<body>
<div id="myDiv"></div>
</body>
Then in getdatabase.php you must fetch the value from the database and echo it out. For example,
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Hope it works , see more at www.w3schools.com/
Use javascript timers. From there-on u can periodically run the SQL command to read the databases.
If this problem still arises, I can post some example code.

communication with database through ajax

i have following set of table rows which i have fetched from the database
echo "<td value='".$id."' style='border:double' bgcolor='" . getcolour($catc) . "' onclick = 'addValue(this.value);'>$id</td>";
this is actually a set of boxes and $id = $row['id'] is the number attached to each box. I want the mechanism like when a user clicks on the box the number attached to it should be echoed on a particular place on the webpage. MySql query and everything else works fine i have a problem with ajax code understanding i think so.
<script>
function valueAdd(str)
{
if (str=="")
{
document.getElementById("seats").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("seats").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ceckbus.php?q="+str,true);
xmlhttp.send();
}
</script>
waiting for ur help everyone!!
First I suggest that you use a javascript library in order to use Ajax, and avoid trying to fix the compatibility issues at your own. For example you could use jquery.com, it is pretty standard nowadays.
For the question part, please have a look at your echo code, there is an error at 'addValue(this.value);'
As I can understand the question, you want on the click event to have the $id. A simple fix it would be by using the innerHTML property, instead on .value:
echo "<td value='".$id."' style='border:double' bgcolor='" . getcolour($catc) . "' onclick = 'addValue(this.innerHTML);'>$id</td>";
or better on the echo you could pass the value directly to the addValue function, eg:
echo "<td value='".$id."' style='border:double' bgcolor='" . getcolour($catc) . "' onclick = 'addValue(\"" . $id . "\");'>$id</td>";

Beginner PHP / SQL / AJAX - Query not working when parameter passed to server side script

I am beginning to learn PHP + AJAX + SQL. I cannot seem to crack the syntax issue I am running into. I am running the latest WAMP version on a server in my network as the test environment.
I have the following script in my INDEX.PHP (from W3Schools.com):
<script>
$(document).ready(function() {
$('#submit').click(function() {
str = document.getElementById("cn").value;
if (str=="")
{
document.getElementById("txtHint").innerHTML="No name typed, returning all rows ";
str="all";
};
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
};
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open("GET","getcustomer.php?q="+str,true);
xmlhttp.send();
});
});
</script>
I then have a server side script called GETCUSTOMER.PHP which contains (in part) the following:
<?php
$q = $_GET['q'];
echo "The variable contains = ".$q; **<--- this shows me on the browser the correct variable value is passed to the server side script**
$con = mysqli_connect('localhost','phpuser','abc123','learnphp');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
};
mysqli_select_db($con,"PHP_AJAX_Demo");
if ($q == "all") {
$sql="SELECT * FROM customers";
} else {
$sql="SELECT * FROM customers WHERE 'Company' = '" . $q . "'";
};
$result = mysqli_query($con,$sql);
When my main form submits with a "blank" value for COMPANY it works fine as the "str=all" is executed and returns to the browser all rows in the CUSTOMERS table. When I submit it with a value (that exists in the table) I get no rows back. I also get no errors back.
The syntax came from W3 and it works on their demo. What am I missing? Thanks in advance!
You're putting single quotes around your column name when single quotes are used to indicate the beginning and end of a string value. Use backticks instead. Change:
$sql="SELECT * FROM customers WHERE 'Company' = '" . $q . "'";
to
$sql="SELECT * FROM `customers` WHERE `Company` = '" . $q . "'";
Note that this script is open to SQL injection and that you should be using prepared statements.

AJAX, PHP, MYSQL - innerHTML not working & Neither any Alternatives

I am a noob and i am trying to get this script working. It works perfectly in all browsers except IE. I have tried most of the solutions here on stackoverflow but none of them works for me. I am doing some kiddish mistake somewhere which I am unable to spot. Would request your help.
Also just to let you know the DOM Objects format of using appendchild might not work 100% correctly for me since this script basically passes values from a drop down which shows say numbers from 1 to 10 and based on those numbers it pulls up corresponding charts. So if I am viewing chart 1 then i can go ahead and select the drop down to view chart 3 as well. But if we use the appendchild property, it only allows me to see one of the charts by selecting 1 to 10 and it doesn't work unless I refresh the page. So unless deletechild is used I think it would not work, just my guess. Hence, I have commented that section out and I am using the good old method of innerhtml.
This is my JS function.
function showUser(str) {
debugger;
var xmlhttp = GetXmlHttpObject("Browser does not support HTTP Request");
if (str=="") {
document.getElementById("pairname").innerHTML="";
return;
}
/* <-- I HAVE ALREADY COMMENTED THIS OUT & am using a more robust function for this.
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
*/
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("pairname").innerHTML=xmlhttp.responseText;
//AS YOU CAN SEE THE ALTERNATIVE SOLUTION OF USING DOM OBJECTS HAS BEEN COMMENTED OUT.. SINCE THIS IS ALSO NOT WORKING IN IE FOR ME.
//var wrappingElement = document.createElement("div");
//wrappingElement.innerHTML = xmlhttp.responseText;
//document.getElementById('pairname').appendChild(wrappingElement);
}
}
xmlhttp.open("GET","getimgdetails.php?q="+str,true);
xmlhttp.send();
}
function GetXmlHttpObject(errorMessage) {
var r = false;
try { r = new XMLHttpRequest(); }// Opera 8.0+, Firefox, Safari
catch(e) {
// Internet Explorer Browsers
try { r = new ActiveXObject("Msxml2.XMLHTTP"); }// older IE
catch(e) {
try { r = new ActiveXObject("Microsoft.XMLHTTP"); }// IE 5+
catch(e) {// AJAX not possible
if(errorMessage) { alert(errorMessage); }
}
}
}
return r;
}
AND THIS IS WHERE I AM SHOWING IT
<div id="pairname"><b>Charts will be shown here.</b></div>*
AND this is the Script which receives and sends back the info. SO basically it shows 5 charts for each number 1 to 10.
$q=$_GET["q"];
Echo "<img src=mqluploads/".$q."15.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
Echo "<img src=mqluploads/".$q."60.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
Echo "<img src=mqluploads/".$q."240.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
Echo "<img src=mqluploads/".$q."1440.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
Echo "<img src=mqluploads/".$q."10080.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
Echo "<img src=mqluploads/".$q."43200.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
?>
Many many thanks in advance for your help and for reading this far. I have been struggling with this for the past one week and now finally decided to seek some help. Looking forward to your advice.
Have you checked that your php script is returning some data. Try to access getimgdetails.php?q=str (replace str with correct paremeter) directly in browser.
Also place an alert(xmlhttp.responseText) before this line:
document.getElementById("pairname").innerHTML=xmlhttp.responseText;
This will help you to debug.

How to prevent the page from scrolling after an ajax request (return false not working or maybe in the wrong palce)

I want to display a tree with information about the Catalogue of Life which includes the Kingdom, phylum, order, etc information. I'm using ajax/php to interact with a mySQL database, while using javascript to display and output the information. I have finally completed the things I wanted it to do: expand and collapse on clicking, load only once, etc. But everytime something is clicked below the scroll limit, it bounces back to the top of the page. I researched this and saw something about including 'return false' in the function or the tag, but this did not solve the problem. Am I just putting it in the wrong place? Thanks for your help...
Here is the code: in its php/javascript goodness...
<?php
include ('includes/functions.php');
$connection=connectdb();
$result=runquery('
SELECT scientific_name_element.name_element as shortname ,taxon_name_element.taxon_id as taxonid
FROM taxon_name_element
LEFT JOIN scientific_name_element ON taxon_name_element.scientific_name_element_id = scientific_name_element.id
LEFT JOIN taxon ON taxon_name_element.taxon_id = taxon.id
LEFT JOIN taxonomic_rank ON taxonomic_rank.id = taxon.taxonomic_rank_id
LEFT JOIN taxon_name_element AS tne ON taxon_name_element.parent_id = tne.taxon_id
LEFT JOIN scientific_name_element AS sne ON sne.id = tne.scientific_name_element_id
LEFT JOIN taxon AS tax ON tax.id = tne.taxon_id
LEFT JOIN taxonomic_rank AS tr ON tr.id = tax.taxonomic_rank_id
WHERE taxon_name_element.parent_id is NULL');
set_time_limit(0);
ini_set('max_execution_time',0);
?>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script type="text/javascript">
function load_children(id){
if(document.getElementById(id).active != 'true'){
if(document.getElementById(id).loaded != 'true'){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function()
{//actual stuff
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{//change the text
this.active='true'
//document.getElementById(id).innerHTML = xmlhttp.responseText;
var oNewNode = document.createElement("ul");
document.getElementById(id).appendChild(oNewNode);
document.getElementById(id).active = 'true';
document.getElementById(id).loaded = 'true';
oNewNode.innerHTML="<i>"+xmlhttp.responseText+document.getElementById(id).active+"</i>";
}
}
xmlhttp.open("GET","new.php?id="+id,true);
xmlhttp.send(false);
}else{
$("#"+id).children().children().show(); document.getElementById(id).active = 'true'}}else{ $("#"+id).children().children().hide();
document.getElementById(id).active = 'false';
}return false;}
</script>
</head>
<body>
<?php
echo "<ul>";
while($taxon_name_element = mysql_fetch_array($result)){
echo "
<li id=\"{$taxon_name_element['taxonid']}\" style=\"display:block;\">{$taxon_name_element['shortname']}</li>
";}
echo "</ul></body></html>";
?>
You were missing a closing parenthesis - if popped it in with a comment where I think you were wanting it. If JS has an error it generally won't execute, and instead it'll fall back to the non JS (ie, the return false wont fire because of the error). Were you getting an actual error when running this? The console is a good friend when you're debugging JS.
Formatting your code like I have below will also help to identify any missing elements more easily.
function load_children(id)
{
if(document.getElementById(id).active != 'true')
{
if(document.getElementById(id).loaded != 'true')
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
//actual stuff
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//change the text
this.active='true'
//document.getElementById(id).innerHTML = xmlhttp.responseText;
var oNewNode = document.createElement("ul");
document.getElementById(id).appendChild(oNewNode);
document.getElementById(id).active = 'true';
document.getElementById(id).loaded = 'true';
oNewNode.innerHTML="<i>"+xmlhttp.responseText+document.getElementById(id).active+"</i>";
}
}
xmlhttp.open("GET","new.php?id="+id,true);
xmlhttp.send(false);
} // This was missing - were you getting an error?
}
else
{
$("#"+id).children().children().show(); document.getElementById(id).active = 'true'}
}
else
{
$("#"+id).children().children().hide();
document.getElementById(id).active = 'false';
}
return false;
}
When you're done checking and if everything is ok, take a look at: http://api.jquery.com/jQuery.get/ - the example's are at the bottom.

Categories