0

I have a form that will add a book to a database, and the authors of these books should be from the database, so the way I want to do this is to make a php loop that will create a checkbox in the form for each author, so the user can just choose the author using them. My problem now is how to process the form? How do I know which authors the user picked in other words?

What other ways can I approach this problem other than checkboxes? (there are no restrictions, this a personal project I just know php, html, JS and jQuery)

Sorry for not providing any code and I hope I explained it well.

6
  • I would create a <select name="author"><option ...>... with all the authors. If they are not too many. In case they are too many I would use an auto completion search. Commented Dec 21, 2017 at 10:51
  • I dont have alot of authors, from my knowledge this only works if there is one author, I want multiple authors for one book, or is there a way I could allow them to select multiple options? Commented Dec 21, 2017 at 11:00
  • Do you want to submit the form by each author ?stackoverflow.com/questions/11945802/… Commented Dec 21, 2017 at 11:08
  • The form will be submitted by the administrator of the website if this what you are asking for, and I want to allow them to choose from 1 author up to the number of authors in the database Commented Dec 21, 2017 at 11:14
  • Ok, Its means you want to submit multiple number of authors selected by the checkbox. If yes, You can use the same url i have provided. Commented Dec 21, 2017 at 11:20

1 Answer 1

0

So first you will need to make a select element where the user can select multiple authors like so:

<select name="authors[]" multiple>
    <?
    foreach ($authors as $a => $author) {
    ?>
        <option value="<?=$author;?>"><?=$author;?></option>
    <?  
    }
    ?>
</select>

next you wil need a server side script to get all the authors.
This select input will give an array to the server containing all the authors the user selected. See the image below. the image below is a print of the $_POST data
enter image description here

i hope this explains it. Let me know if there is anything unclear.

Sign up to request clarification or add additional context in comments.

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.