0

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>
4
  • What is error message? Commented Jun 18, 2016 at 10:15
  • 1
    <?php echo $row['Value'].'_'.$row['Code'] ?> concatenate the code with value with delimiter. Commented Jun 18, 2016 at 10:21
  • If any of the answers solved your problem, you should accept it. Commented Jun 19, 2016 at 18:28
  • mark an answer as correct... Commented Jun 20, 2016 at 20:45

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I am sure he doesn't want a button named something like Certificate name_CRT-AB-3535 and just wants Certificate name. And what happens, if the value has underscores?
-1

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.

6 Comments

I didn't downvote you, but probably because you didn't answer the question and just mentioned other mistakes.
I wrote quote "if I understood your question, you cannot print correctly the $row values to the <input> tag." so, why downvote me people ? Insted of downvoting, tell me what is he asking... Thanks for the comment @gre_gor
Changing echo style and mysql fetch types doesn't solve the OP's problem. They are a matter of personal taste not a mistake. You didn't answer the question.
@Progrock how could I answer the question if I am asking if I did understand... read comments pls...
I read all the comments, if you don't understand the question, ask the OP to clarify before answering.
|

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.