0

i want to fetch 1st category data in category div and subcategory data in sub-cat div. i am using Ajax , Jquery. i have tried below code but result shows undefined. Please let me know where the code is wrong ?

HTML is :

<body class="bg-color">
    <div id="page-wrap">
        <div class="border-radius">
            <div class="box-heading">Assignment</div>
                <div class="footer group">
                    <div id="category">
                    5
                    </div>
                    <div id="sub-cat">
                        5                 
                    </div>
                    <div id="sub-cat1">
                    5                  
                    </div>
                    </div>
                </div>
        </div>
    </div>
</body>

Javascript is :

<script type="text/javascript">
$(document).ready(function(){                    
                 });
function pageLoad()
{
    var result = new Array();
    $.ajax({
                        type: "POST",
                        url: "get.php",
                        data: "",
                        async: false
    }).responseText.split(",");
document.getElementById('category').innerHTML = result[0];  

}
window.onload = pageLoad;


</script>

PHP code is

<?php
    require("_assets/config/dbc.php");


        $id = $_REQUEST['id'];
        $query = mysql_query("SELECT * FROM category");

        while($row  = mysql_fetch_array($query)){
        $a = '<a href="javascript:void(0);" onClick="vote1(' . $row['cat_id'] . ');">' .$row['title'] . '</a></p>';
        }
        $query1 = mysql_query("SELECT * FROM subcategory");
        while($row1  = mysql_fetch_array($query1)){
        $b = '<a href="javascript:void(0);" onClick="vote1(' . $row1['cat_id'] . ');">' .$row1['title'] . '</a></p>';
        }

        echo $result = $a."*".$b;

?>

Actually i want when the page is load ajax go to get.php file and get category by ID , sub category by id and sub sub category by ID. so i will show these on my 3 divs.. Please sort out my this problem..

2
  • what is result array? where u loading it from? Commented Nov 15, 2012 at 12:59
  • result array is showing undefined. i am loading on index.php main form. Commented Nov 15, 2012 at 13:02

2 Answers 2

1

you can do this by json in php use json_encode ( Returns the JSON representation of a value) to encode value and in ajax try

for example in php

$sequential = array("foo", "bar", "baz", "blong");
echo  json_encode($sequential);

and in ajax success try

success: function(data) {
       data = jQuery.parseJSON(data);
 }
Sign up to request clarification or add additional context in comments.

Comments

0

I have done by itself. my code is :

function vote(id)
{ 
    var result = new Array();
     result = $.ajax({
                       type: "POST",
                       url: "ajax.php",
                       data: "work=vote&id="+id,
                       async: false
               }).responseText.split("^");
document.getElementById('sub-cat').innerHTML = result[0];
document.getElementById('sub-cat1').innerHTML = result[1];
//window.location.reload()
}

now i am facing another problem.. please let me know how to get 1st value of while loop. so i will get sub sub category using sub category id..

php code is :

<?php
    require("_assets/config/dbc.php");

    if($_REQUEST['work']=="vote")
    {
        $id = $_REQUEST['id'];
        $query = mysql_query("SELECT * FROM subcategory WHERE cat_id='$id'");
    $HTML= "";
        while($result  = mysql_fetch_array($query)){
            $HTML .= '<a href="javascript:void(0);" onClick="vote1(' . $result['subcat_id'] . ');">' .$result['title'] . '</a></p>';  
        }
    $HTML .= "^";
    $sql = mysql_query("select * from subscategory "HOW TO GET 1ST SUBCATEGORY ID?"") or die(mysql_error());
    while($row = mysql_fetch_assoc($sql)){
        $HTML .= '<a href="javascript:void(0);" onClick="vote1(' . $row['subcat_id'] . ');">' .$row['title'] . '</a></p>';


        }

    echo $HTML;
    }

?>

2 Comments

your code is vulnerable to sql ...bad guy will kill your website
buddy this just a sample test. its not a working website.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.