Home > Enterprise >  Getting ID from a PHP script and inplement in a URL
Getting ID from a PHP script and inplement in a URL

Time:07-15

I want to pass in the URL an ID from the following PHP script:

<tbody>
    <?php
    foreach ($EditProductInformations as $EditProductInformation) :
    ?>
    <tr>
        <td colspan="2">
            <div >
                <div >
                    <select  name="produkt[]">
                            <?php foreach ($resultsProdukt as $resultp) { ?>
                                <option value="<?= $resultp["ProduktID"] ?>" <?php if($resultp["ProduktID"] == $EditProductInformation["ProduktID"]): ?>selected<?php endif; ?>><?= $resultp["produktname"] ?></option>
                            <?php } ?>
                    </select>
                </div>
                <div > <input type="text"  placeholder="https://" name="url" value="<?= $EditProductInformation['url']; ?>"></div>
                <p> <div >  <a onclick="confirmDeleting(id)"  href="#" id="<?= $EditProductInformation['ID'] ?>"><i id="produktDel" ></i>  </a> </div> </p> 
            </div>
            <br>
        </td>
    </tr>
    <?php endforeach ?>
</tbody>

this so my js script so far:

    function confirmDeleting(elemen) {
        let confirmDeleting = confirm("are you sure, you want to delete?");
        if (confirmDeleting) {
            console.log(elemen);
            window.location.href="formprocess.php?dfID="   elemen.dataset.id;
            }
        }

My URL is good so far, but I cant reach the ID

"formprocess.php?dfID=undefined" 

is the URL but I want that it will be

"formprocess.php?dfID=144"

Thanks in advance

CodePudding user response:

You don't have any dataset attribute, and all attribute value of element will be in string, so better if you have custom attribute of your element
Something like this:

<a onclick="confirmDeleting(this)" data-id="<?php echo $resultp[\"ProduktID\"]; ?>"  href="#">

And you can access that attribute by:

elemen.getAttribute("data-id");

Also if you are using JQuery you can do it like this:

$(elemen).data("id");

CodePudding user response:

Instead of passing 'this' as a parameter to the confirmDeleting() function, you can give it as a parameter directly the ID of the product concerned. So the code will be:

<a onclick="confirmDeleting(<?php echo $resultp["ProductID"] ?>)"  ><i id="productDel" ></i></a>

Then you will only have to retrieve the ID parameter in your JS function

function confirmDeleting(ProductID) {
let confirmDeleting = confirm("are you sure, you want to delete?");
if (confirmDeleting) {
    console.log(ProductID);
    window.location.href="formprocess.php?dfID="   ProductID;
    }
}
  • Related