0

I have HTML list that looks like :

<ul>
  <li>something</li>
  <li>something1</li>
  <li>something2</li>
<ul>

<div class="randomwrapper">
   <img src="<?php echo $databaseresult[$i];?>">
   <img src="<?php echo $databaseresult[$i];?>">
   <img src="<?php echo $databaseresult[$i];?>"> // result looks like : ../assets/images/20584878157.jpg
</div>

then I have a table that looks like:

table:

is it possible to create jquery function with ajax that will send f.e.:"something1" to "process.php"(PHP file that I will create in order to process data) which will return the image name (22287895.jpg) and then the jquery function will hide any image that has the "imgname"(22287895.jpg) in it?

i'm able to create process.php but i have no skills with jquery and ajax. I don't even know if its possible.

Thank you for any possible help/references

6
  • could you clarify what you mean by "which will return the img name (22287895.jpg) and and then the jquery function will hide image that have the "imgname"(22287895.jpg) in it?" Commented Mar 12, 2018 at 13:46
  • What event will trigger the AJAX? i think if you want to hide an image on some event(s) then you can do it even without AJAX. Commented Mar 12, 2018 at 13:55
  • Simply put, yes it is possible... what have you tried so far. Commented Mar 12, 2018 at 13:57
  • @sunny-soni click on <li> .. i dont want page to reload Commented Mar 12, 2018 at 14:21
  • You know to do queries and process the result ? You could have a button to do the request ( not an ajax ) and the request would be a little bit different. Don't query for something1 and fetch his image name, but query for name different from something1 so you will get all the images without something1. And you will display the result in a GET request. Commented Mar 12, 2018 at 14:28

2 Answers 2

2

If I understood right you can do something like this:

function getImageName(name){
// do a request that will return the "imgname" from the "name"
}

function hideImage(src){
    let img = document.querySelector(`img[src=${src}]`);

    if(img !== null)
        img.style.display = 'none'; 
}

// then you can do
hideImage(getImage("something1"));
Sign up to request clarification or add additional context in comments.

2 Comments

how i do that request ? :D
@JúliusĽuptovec check this api.jquery.com/jquery.get . Then tell me if you still have trouble and I'll edit my answer with this part.
1

No need of AJAX request, you have id in your DB and name (somethings) as well.

Make li's dynamic by fetching id, name from DB and then loop through results:

<ul class"somethings">
   <?php
   foreach ( $records as $rec ) {
        echo "<li data-recordid=\"{$rec['id']}\">{$rec['name']}</li>";
    }
    ?>
</ul>

data-recordid holds the data ID for each li.

On the image create a dynamic unique id by using id from DB like:

<img id="image-<?php echo $databaseresult['id'];?>" src="<?php echo $databaseresult['src'];?>">

I will also recommend to fetch all images from your DB

Rest is jQuery:

$(function(){ // checking for DOM to be ready
    $(".somethings li").click(function(){
        var recordid = $(this).data('recordid');
        $("#image-" + recordid).hide();
    });
});

1 Comment

for certain reasons it is not possible(in my case) to foreach results :/ i posted just a example. real problem is kinda more complicated

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.