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>
10

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I am getting this notice but not the output from database also apology about the confusion. Please check my updated nav bit... <br /> <b>Notice</b>: Array to string conversion in <b>C:\xampp\htdocs\series\dynamics\admin\CleanURL\test.php</b> on line <b>11</b><br /> Array
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?
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 />");
    }
});

6 Comments

In your javascript, data is a list to results. Use console.log(data) and view in console firebug or console chrome to see the elements.
this is the output [], with above changes
So, your query return empty.
try : "SELECT DISTINCT product FROM prdct_categories WHERE class LIKE '{$value}'"
And in your javascript, to set text o code in de tag, use html for example. I edit my answer.
|
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>

1 Comment

using .tml doesn't make any difference to the output

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.