Angry Progy
Angry Progy

Reputation: 23

I want my input to get the value of my button when it is clicked

i need help: how to get the name of my button inside my input. I have 2 files (index.php, contact.php) the buttons I have are in index.php and the form is in contact.php

contact.php file

<form class="form" id="contactPage">
  <div class="labelBlock contactPageLabel">
    <label for="name">Name *</label>
    <div>
      <input id="name" type="text" name="name" required>
    </div>
  </div>
  <div class="labelBlock contactPageLabel">
    <label for="email">E-mail *</label>
    <div>
      <input id="email" type="email" name="email" required>
    </div>
  </div>
  <div class="labelBlock contactPageLabel">
    <label for="subject">Subject</label>
    <div>
      <input id="subject" type="text" name="subject">
    </div>
  </div>
  <div class="labelBlock contactPageLabel">
    <label for="message">Message *</label>
    <div>
      <textarea name="textarea" id="message" required></textarea>
    </div>
  </div>
  <div class="labelBlock contactPageLabel">
    <div>
      <button>Send</button>
    </div>
  </div>
</form>                  

and the next file:

index.php file

<button name="contracted_instructor">Contracted instructor</button>
<button name="talented-people">Talented people</button>

I want that after clicking on one of these buttons, the name of my button is displayed in the subject. for example, if I click on

<button name = "contracted_instructor"> Contracted instructor </button>

then the form will display

<input id = "subject" type = "text" name = "subject" value = "contracted_instructor">

Upvotes: 0

Views: 40

Answers (1)

salam
salam

Reputation: 46

You can place the buttons inside a form and make the action of that form = contact.php and method = GET Then on the contact.php, you receive the value of the clicked button and load the content accordingly You can add a php code like:

$clicked_button = $_GET['b'];

And inside your input element:

<input id="subject" type="text" name="subject" value="<?= $clicked_button ?>">

Upvotes: 1

Related Questions