1

I have a form that contain multiple button displayed by database I want to open each button by it's ID. This is what I tried It didn't work whene I use variable in ISSET POST.

    <form method="post" action="" enctype="multipart/form-data">

    <?php
    if (mysqli_num_rows($result) > 0) { ?>

    <?php
    $i=0;
    while($row = mysqli_fetch_array($result)) {
        $nom=$row['nom'];
    ?>

    <div class="card" style="width: 10rem; margin:4px">
        <div style="height: 48px;">
            <?php echo "<img class='card-img-top' alt='' src='../admin/image/mercedes/".$row['image']."' >";?>
        </div>
        <div class="card-body">
            <input type="submit" id="button1" class="btn" name="<?php echo "$nom"; ?>" Value="<?php echo "$nom"; ?>" />
        </div>
        
    </div>

    <?php
    $i++;
    }
    ?>
</form>

At this is the isset post:

<?php
  if(ISSET($_POST[$nom]))
    {
     echo '<script type="text/javascript">';
     echo ' alert("JavaScript Alert Box by PHP")';  //not showing an alert box.
     echo '</script>'; 
    }
?>

Here is the image image1

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
walid
  • 51
  • 5
  • 1
    What do you mean by "open a button"? – CBroe Jul 28 '22 at 10:57
  • _"It didn't work whene I use variable in ISSET POST"_ - well unless you had given `$nom` a value prior to that, this makes little sense. – CBroe Jul 28 '22 at 10:58
  • Direct the form to variable isset – walid Jul 28 '22 at 10:58
  • That doesn't clarify anything for me. Still unclear what you actually want to achieve here. – CBroe Jul 28 '22 at 10:59
  • If you need access to the `nom` value of the specific record - then use the same name for all those buttons. You already put the `nom` into the value attribute of the button, so that is what it will submit. – CBroe Jul 28 '22 at 11:01
  • 1
    you are setting a variable in the name of input tag ? on which basis you will Get value in your action. – Aqib Javed Jul 28 '22 at 11:02
  • What i truly want is to insert the value of inserted button in database but without selecting by id it insert automaticly the last value – walid Jul 28 '22 at 11:02
  • @AqibJaved exactly – walid Jul 28 '22 at 11:03
  • var_dump($_POST) ; in your action and check your variable name and value there – Aqib Javed Jul 28 '22 at 11:06
  • Also bear in mind [the difference between server-side and client-side processing](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) - when you submit the form, it is a completely new execution of the PHP, and will run through the whole file. It won't jump to a particular piece of PHP code and execute that. – IMSoP Jul 28 '22 at 13:27

1 Answers1

-1

Is this what you are missing ? $nom doesn't have a value unless yoo read the data again and iterate though the result.


<?php
if (mysqli_num_rows($result) > 0) 
{
    $i=0;
    while($row = mysqli_fetch_array($result)) 
    {
        $nom=$row['nom'];
        if(ISSET($_POST[$nom])) 
        {
           echo '<script type="text/javascript">';
           echo ' alert("JavaScript Alert Box by PHP")';
           echo '</script>'; 
        }
    }
}
?>
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41