0

I am hoping to be able to use .post method to send variables to a php file where I could be able to retrieve data from database with mysql query. So far I can send information about the variable using ajax but not able to retrieve output from the php script using ajax. I am very immature to ajax, hence learning through errors...Thanks for looking into my query.

How can I get to display php output within the div tag of index.php

test.php

   <?php require('../config/connection.php'); ?>
        <?php
        $value = $_POST['value'];
        $query = mysqli_query($dbc, "SELECT DISTINCT class,product FROM prdct_categories WHERE class = '$value'");
        while($list = mysqli_fetch_assoc($query)){
        $prdct = $list['product'];
        echo $prdct;    
        }
   ?>

ajax code (index.php)

    <div class="col-md-2" >

         <?php

        $test  = $_GET['product'];
        $q = "SELECT * FROM prdct_categories WHERE product = '$test' ";
        $r = mysqli_query($dbc, $q);
        $path_info = get_path();
        $test1 = $path_info['call_parts'][1];

       While($list = mysqli_fetch_assoc($r)) {?>
      <li class="nav" <?php if($test1==$list['slugs']){echo'id="actives"';} ?>>

<a href="<?php echo $test;?>/<?php echo $list['slugs'];?> ">

<?php echo $list['subgroup']."(".$list['contains'].")".'<br/>';?></a></li>

                 <?php }?>   
                 </div>

                <div class="col-md-4" id="testing">

                </div>

                <script>
                    $(document).ready(function(){

                        $(".nav").click(function(){

                        $.post("test.php", {value:$(this).text()}, function(data) {$("#testing").text(data)} );

                        event.preventDefault();
                        });
                    });
                </script>
Gautam P Behera
  • 171
  • 2
  • 13

3 Answers3

1

Please use this code instead. You do have to prevent the default action of the links that you're clicking within the .nav elements:

    $(".nav > a").click(function(e){ // see change here
            e.preventDefault();   // added here e must match var e above.
            $.post("test.php", {value:$(this).text()}, function(data) {$("#testing").text(data)} );
    });

And your .nav script should be:

<li class="nav<?php if($pageid==$nav['product']){echo ' active';}?>"> 
....

Your current script does not create any elements with class="nav" and therefore, the selector .nav returns nothing. Your current script also creates several elements with the same id, id="nav", which makes your HTML invalid.. Make the above changes and

PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • I am getting this notice but not the output from database also apology about the confusion. Please check my updated nav bit...
    Notice: Array to string conversion in C:\xampp\htdocs\series\dynamics\admin\CleanURL\test.php on line 11
    Array
    – Gautam P Behera Feb 15 '16 at 16:12
  • In effect you're confirming that the above changes indeed do work, in that you're now getting a response from `test.php`, only that the php script needs to be fixed, right? If this is so, would it be better to ask a new **PHP** question? – PeterKA Feb 15 '16 at 16:20
0

Form to return data from ajax.

<?php require('../config/connection.php'); 
      header('Content-Type: application/json');

        $value = $_POST['value'];
        $prdct = array();
        $query = mysqli_query($dbc, "SELECT DISTINCT product FROM prdct_categories WHERE class LIKE '{$value}'");
        while($list = mysqli_fetch_assoc($query)){
        $prdct[] = $list['product'];
        }
        $return = json_encode($prdct);
        echo $return;
        die;
   ?>

In javascript replace:

$.post("test.php", {value:$(this).text()}, function(data) {$("#testing").text(data)} );

to

$.post("test.php", {value:$(this).text()}, function(data){
    for(var k in data) {
    $("#testing").append(data[k]+"<br />");
    }
});
Maxi Schvindt
  • 1,432
  • 1
  • 11
  • 20
0

Use $("#testing").html(data); instead of $("#testing").text(data);

<script>
    $(document).ready(function(){
        $(".nav").click(function(){
              $.post("test.php", {value:$(this).text()}, function(data) {
                        $("#testing").html(data);
                    });
              event.preventDefault();
        });
    });
</script>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77