0

I have a form containing the following:

echo"<textarea name='pb' required></textarea>";

echo"<button name='bt'>button1</button>";
echo"<button name='bt'>button2</button>";
echo"<button name='bt'>button3</button>";
echo"<button name='bt'>button4</button>";

echo"<input type=submit name='submit' value='Submit'>";

As you can see in my code, the textarea is required, but I have many buttons. I want only when I press the submit button that the message that the textarea is required is displayed, but when I click the other buttons this message shouldn't appear.

Phortuin
  • 770
  • 4
  • 20
HasanHajjar
  • 149
  • 2
  • 14

2 Answers2

0

Adding type="button" to the buttons should do the trick:

<button name="bt" type="button">button</button>

See MDN for more about buttons: https://developer.mozilla.org/nl/docs/Web/HTML/Element/button

submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.

button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.

Community
  • 1
  • 1
Phortuin
  • 770
  • 4
  • 20
0

Phorturin is correct. As stated in this question, buttons have a default type of submit (see the html spec for more info.) If you set the type to button for your 'bt's, then they won't submit your form.

Barn on a Hill
  • 359
  • 3
  • 10