Aditya
Aditya

Reputation: 25

Passing the parameter 'name' in php form handling

In my database i have created a table named iso(2 columns- name of ISO certificate and the code) and by the code below, I am trying to print the names of all the certificates as submit type buttons(clickable). But I am not able to pass that which button was passed to the next file. Please help. Thanks!

     <?php 
        $con=mysqli_connect("localhost","root","","dyna");
// Check connection
        if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
         }

          $result = mysqli_query($con,"SELECT * FROM iso");
          ?>

  <form method="post" action="http://localhost/junk.php/">
    <?php while($row = mysqli_fetch_array($result))
       { ?>
       <input type="submit" name="<?php $row['Code'] ?>" class="login login-submit" value="<?php echo $row['Value'] ?>" >
        <?php } ?>
    <?php mysqli_close($con); ?>
  </form>

Upvotes: 0

Views: 83

Answers (3)

JYoThI
JYoThI

Reputation: 12085

if you want both value and code means .concatenate the code with value with delimiter.

<input type="submit" name="certificate" class="login login-submit" value="<?php echo $row['Value'].'_'.$row['Code'] ?>" >

       $ss = explode('_',$_POST['certificate']);

       echo  $ss[0];  //value
       echo $ss[1];   //code

Upvotes: 0

gre_gor
gre_gor

Reputation: 6800

Use a <button> element named cert, to pass your certificate code as a value, but show the name certificate as the button name:

<button name="cert" value="<?php echo $row['Code']; ?>" type="submit"><?php echo $row['Value']; ?></button>

Then in your junk.php, use the POST argument cert to determine which button was pressed based on your certificate code:

if (isset($_POST['cert']))
{
    $cert_code= $_POST['cert'];
    // do something with $cert_code
}

Upvotes: 2

matiaslauriti
matiaslauriti

Reputation: 8082

First of all, you can use <?= $var ?> or <?= "test" ?> to print to avoid using <?php echo....

So, if I understood your question, you cannot print correctly the $row values to the <input> tag.

You can debug what is receiving your query why doing var_dump($row); at any part of the while part. So if you are receiving correctly the array, you will see which keys it have.

And one more thing, replace your mysqli_fetch_array by mysqli_fetch_assoc because the first one assign the name of the columns but it adds numeric keys to the $row array too.

To understand better, read mysqli_fetch_array and mysqli_fetch_assoc.

Upvotes: -1

Related Questions