I have file that is supposed to pass the id of a link to using the ajax post method to a php file so that a div on my webpage updates. But nothing happens so i think the class is not being passesd. Can anyone see the problem.

blog.js

$('a.bloglink').click(function(e) {
    // first stop the link to go anywhere
    e.preventDefault();
    // get the class of the link
    var linkClass = $(this).attr("class");
    //get the text of the link by converting the clicked object to string
    var linkText = new String(this);
    // the value after the last / is the category ID
    var categoryValue = linkText.substring(linkText.lastIndexOf('/') + 1);
    // put the post parameters into 'params' to pass through the AJAX post request
    var params = {};
    params[linkClass] = categoryValue;  
    // send the category ID to the showproducts.php script using jquery ajax post method
    // send along a category ID
    // on success insert the returned text into the chosen div
    $.post('../inc/blogchoice.php', params, function(data) {
        //display returned data and page links in chosen div (.blog)
        $('.blog').html(data);
    });
});

blogchoicephp

 include 'connect.php';
   if(isset($_POST['bloglink'])){
      // cast the category to integer (just a little bit of basic security)
      $id = (int) $_POST['bloglink'];
      $q = "SELECT * FROM blogs WHERE id=$id";
      $result = $link->query($q);
      // this will be the string that you will return into the product-data div
      $returnHtml = '';
      // construct the html to return
         while($row = mysqli_fetch_array($result)) {
            $returnHtml .= $row['content'];
         }
    }
   // display the html
   echo $returnHtml;

Thanks for looking..........

I get this error in chromes inspector
Failed to load resource: the server responded with a status of 404 (Not Found)

Anyone shed light?

Are you sure this is the correct path?

$.post('../inc/blogchoice.php', .....

What happens if you replace this with the absolute URL just to test?

Im on a local server at the minute, wamp. Does it have a URL?

Never mind that last post. It works with the url

So if an absolute path works but not the relative, it sounds to me like the relative path you are using is not correct and that's why you see a 404 error which means not found. In other words, the relative URL you provided was not located on the web server.

[edit] - I must have been typing this at the same time you marked it as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.