0

In my code I send $id to Java Script section and it returns the same value inside <p id="retid"></p> and I can't get the value as integer inside $text variable.

        `while ($row = $result->fetch_assoc())
            {
            $id = $row["id"];
            $regname = $row["reg-name"];
            echo '<button class="btnicon btndel" type="button" name="answer" onclick="showDiv('.$id.')"><i class="fa fa-trash"></i>  '.$regname.'</button>';
            }
        } else {
            //header("Location: login.php?Msg=wrongPass"); }
        }

?>
</div>
        <div id="DelMessageDiv" style="display:none;" class="answer_list" >
            <form action="_modify.php" method="get">
                <div id="alerts">
                <?php



                $text = '<p id="retid"></p>';


                    echo $text.'-+-'.strip_tags($text,'<p>').'
                    Are you sure for delete? 
                    <input name="id" type="hidden" value="'.strip_tags($text).'" />                         
                    <button class="btnicon btndel" type="submit" name="operation" value="0+regDelete">Yes Delete</button>
                    ';
                ?>
                </div>
            </form>
        </div>

</div>
<?php include '_footer.php'; ?>
<script src="js/extention/choices.js"></script>
<script>
    const choices = new Choices('[data-trigger]',
    {
      searchEnabled: false
    });
    
    function showDiv(x) {
        document.getElementById('DelMessageDiv').style.display = "block";
        document.getElementById('retid').innerHTML = x;
    }         
</script>`

Thanks for all helps.

Reza Sedagheh
  • 43
  • 1
  • 8
  • 2
    Inline event handlers like `onclick` are [bad practice](/q/11737873/4642212). They’re an [obsolete, cumbersome, and unintuitive](/a/43459991/4642212) way to listen for events. Always [use `addEventListener`](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Dec 07 '22 at 12:44
  • 3
    `I can't get the value as integer inside $text variable`...what do you mean? There is nowhere that you've tried to do that. Your JS code will add the value into the "retid" element after the page has loaded, when the button is clicked. But that happens in the browser and has nothing to do with PHP – ADyson Dec 07 '22 at 12:45
  • 1
    It's not clear to me what you're asking, or what this code is even trying to do. Can you elaborate on the specific problem you are observing and the specific debugging you have done? – David Dec 07 '22 at 12:46
  • 1
    Do you think `document.getElementById('retid').innerHTML = x;` will somehow update the value of ``? That’s not how JavaScript or PHP work. – Sebastian Simon Dec 07 '22 at 12:46
  • 1
    If you want the selected ID value to be posted back to the server, you'll also need to put it into the `` hidden field **using JavaScript**. `value="'.strip_tags($text).'"` won't achieve it, because that code ran when your page was first loading, while PHP was still executing on the server, which is **before** the user clicked on anything. – ADyson Dec 07 '22 at 12:47
  • 4
    Make sure you understand the lifecycle of your page and your server application, and the difference between JS and PHP. Required reading: [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – ADyson Dec 07 '22 at 12:48

0 Answers0