1

I got a select box where i can choose a Project-ID and pass it to the next form with a submit button. Right now it looks like this:

enter image description here

This is my code:

 <form name="form2" action="formular3.php" method="post">

 <p><strong>Choose Project:</strong></p>
 <select name = "project_id">

 <?php
 while ($row = mysqli_fetch_array($result)) {
     echo "<option value =" . $row['project_id'] . "> Project_ID: " . $row['project_id'] . " - (" . $row['name'] . ")</option>";
 }
 ?>

 </select>

 <input type="submit" value="Send" />

 </form>

Now I want to have a list which has a Submit-button for every row. It should look like this:

enter image description here

0

3 Answers 3

1

If you are only sending one option per form, then remove the select tag entirely.

Better Method (and notice mysqli_fetch_assoc() instead of mysqli_fecth_array():

echo "<form name=\"form2\" action=\"formular3.php\" method=\"post\">";
    while($row=mysqli_fetch_assoc($result)){
        echo "Project_ID: {$row['project_id']} - ({$row['name']}) ";
        echo "<button name=\"project_id\" value=\"{$row['project_id']}\">Send</button>";
    }
echo "</form>";

This will submit the value in the clicked button without having to write so many form blocks into the html. You will only need to adjust the actual displaying of the buttons with <br>, table cells, etc.


My previous method that will work, but is not DRY:

while($row=mysqli_fetch_array($result)){
    echo "<form name=\"form2\" action=\"formular3.php\" method=\"post\">";
        echo "<input type=\"hidden\" name=\"project_id\" value=\"{$row['project_id']}\">";
        echo "Project_ID: {$row['project_id']} - ({$row['name']}) ";
        echo "<input type=\"submit\" value=\"Send\" />";
    echo "</form>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

@k.troy2012 I gave you some suboptimal advice previously. Please implement my better method in your project.
1

Use submit button inside the loop,

 <p><strong>Choose Project:</strong></p>
 <select name = "project_id">

 <?php
 while ($row = mysqli_fetch_array($result)) {
     echo "<option value =" . $row['project_id'] . "> <input type='submit' value='Send' />Project_ID: " . $row['project_id'] . " - (" . $row['name'] . ")</option>";
 }
 ?>

 </select>



 </form>

2 Comments

Looks good, but no matter what submit-button i press, it always submits the first project-ID of the list .. in this case project_id = 5
Actually You are submitting the form before selecting one from the dropdown. Just make the input type of send button from submit to button, then on change of dropdown, submit the form using jquery.
0

You need do stop occuring current submit event and give submit button to a id, use jquery to click on submit you should post a spicific form.

like on of the submit will be clicked, you will run this script.

$(document).on('click', '#submit', function () {
    $(this).closest("form").submit();
});

Comments

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.