2

I have this code on php

for ($i=0;$i<$myCounter;$i++) {
    $inum++; //showing only by the counter.
    echo "<hr>ID de Producto ".$inum.":".$dataReceive[$i]["Detalle"]["IDENDODET"];
    echo '
        <form method="POST">
            <input type="hidden" name="IDENDODET" id="IDENDODET" class="IDENDODET" value="'.$dataReceive[$i]["Detalle"]["IDENDODET"].'">
            <button type="button" id="Delete" name="Delete" style="border:0;background-color:yellow;" onclick="deletecookieTest()"/>Borrar este ID</button>
        </form>';
}

And I want the value from the IDENDODET to be sent with the button clicked. I've tried this:

 var form = document.querySelector('input[name=IDENDODET]').value;

but that only gives me the first value when the button is clicked instead of the value of the form in particular. For example no matter which button I click below I get "165" instead of "167" which I need to later delete that section. here is what it looks like

2
  • When you say first value which value are you talking about? I only see one value in the form. Have you done a view source to see if the value is being properly populated? Commented Feb 26, 2019 at 14:27
  • I added a picture to help out. It always shows the first value with the selector. With querySelectorAll it shows me all of the values. But I need to get the value in particular to later be able to delete the API product Commented Feb 26, 2019 at 14:32

2 Answers 2

1

There's another issue that you have in the code. You technically can't have the button have the same id, but what I would do is have the id being passed into the deleteCookieTest function. This way you don't have to traverse the DOM.

<button type="button" name="Delete" style="border:0;background-color:yellow;" onclick="deletecookieTest('.$dataReceive[$i]["Detalle"]["IDENDODET"].')"/>
Borrar este ID
</button>
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to send two variables (IDENDODET + Quantity) instead of just one (IDENDODET) ?
1

Use document.querySelectorAll which will give all the elements. This will give a static nodelist and then use forEach to get the value from each item

document.querySelectorAll('input[name=IDENDODET]').forEach(function(elem){
  console.log(elem.value);

})

1 Comment

this works, it shows me all values. but I need to send the particular value that's chosen to a post

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.