Home Why is my id not getting fetched from my url PHP AJAX MYSQL
Reply: 2

Why is my id not getting fetched from my url PHP AJAX MYSQL

Jack Warren
1#
Jack Warren Published in 2018-01-13 02:44:54Z

Im trying to grab the my 'id' from the url to load the page however it doesn't seem to be showing anything. This is what i have in my url

http://localhost/test/product.php?id=21

When i use

$product_query = "SELECT * FROM photo WHERE photo_id = '21'";

it works fine but obviously i want to be able to load any item that is clicked on that has an

from my gallery.php

<a href='product.php<?php echo"?id=$photo_id" ?>'>Get this Product!</a>"

So this is what i have got from researching but still not results

this is my action.php

if(isset($_GET["getProductind"])){

$photo_id = (isset($_GET['id']) ? $_GET['id'] : null);

    $product_query = "SELECT * FROM photo WHERE photo_id = '$photo_id'";
    $run_query = mysqli_query($conn,$product_query);
    if(mysqli_num_rows($run_query) > 0){
        while($row = mysqli_fetch_array($run_query)){

And this is what im using in my ajax script

main.js

function productind(){
$.ajax({
    url :   "action.php",
    method: "GET",
    data    :   {getProductind:1},
    success :   function(data){
        $("#get_productind").html(data);
    }
})
}

Updated hopefully this makes more sense

hexYeah
2#
hexYeah Reply to 2018-01-13 02:53:27Z

You are using $photo_id = $_GET['id'] to do your query, but in your ajax call you are passing only getProductind and no id parameter

Racil Hilan
3#
Racil Hilan Reply to 2018-01-13 03:18:06Z

There are so many errors that's it's even hard to list. I'll give a try.

1- You're using a link:

<a href='product.php<?php echo"?id=$photo_id" ?>

First, the link is not formatted correctly and is invalid. That aside, the link will request the PHP page directly from the server, and will never call your Ajax code. How are you expecting that to work? You need to use a button. If you really want a link, then you need to disable it and use the onclick event to call your function:

<a href="javascript:void 0;" onclick="productind()">Get product</a>

Then you can put the id directly in your productind() function. If you prefer to pass the id from the link, you can do this:

<a href="javascript:void 0;" onclick='productind(<?php echo"$photo_id"?>)'>Get product</a>

And you need to add the id parameter to the productind() function, but I'm not going to show this, because it's pretty trivial.

2- Your Ajax function is not passing the id parameter, it is only passing a getProductind parameter set to 1. You need to need to add the id parameter. Also the URL should be pointing to the product.php page not the action.php page:

function productind() {
    $.ajax({
        url     :   "product.php",
        method  :   "GET",
        data    :   {getProductind: 1, id: <?php echo"$photo_id"?>},
        success :   function(data){
            $("#get_productind").html(data);
        }
    });
}

I cannot comment on your PHP code, because you only posted part of it. That part seems to be fine and will work after you do the above fixes, but I don't know if the rest of the code is returning meaningful data to the Ajax function.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303601 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO