Notification texts go here Contact Us Buy Now!

Add html code with a function when button is pressed

Update

If you want something like, repeating additional form elements, you should be using JavaScript for that. This is something like a start using jQuery (an awesome JS library):

$(function () {
  $(".add").click(function (e) {
    e.preventDefault();
    $(this).before('<label>Nom d\'un stagiaire : </label><input type="text" name="ajoutStagiaireNom"/><br/><label>Mail du stagiaire : </label><input type="text" name="ajoutStagiaireMail"/><br/><label>Telephone du stagiaire : </label><input type="text" name="ajoutStagiaireTel"/><br/><br/>');
  });
});
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<h5>Et ses employés ?</h5>
<form method="post" action="AjoutFormation.php">
  <label>Nom d'un stagiaire : </label><input type="text" name="ajoutStagiaireNom[]"/><br/>              
  <label>Mail du stagiaire : </label><input type="text" name="ajoutStagiaireMail[]"/><br/>
  <label>Telephone du stagiaire : </label><input type="text" name="ajoutStagiaireTel[]"/><br/>
  <br/><a href="#" class="add">Add Another Set</a><br/>
  <input type="submit" value="Ajouter un stagiaire"/>
</form>

While using the above way, you need to send the data to the server in an array way. Like above. See the name for all the input elements.

Let me know if this is what you want?


I'm trying to insert some code thanks to a submit button. To sum up, I want that this button call an PHP function which add a block of HTML code.

PHP is a server side language, which gets executed before the client side code renders. This is not possible with PHP, but you need to use some client side programming language such as JavaScript.

For this, you can simply load the file with the form elements hidden and the on the click of a button, you can make it display with JavaScript.

<h5>Et ses employés ?</h5>
<form method="post" action="AjoutFormation.php">
<div style="display: none;" id="hidform">
  <label>Nom d'un stagiaire : </label><input type="text" name="ajoutStagiaireNom" /><br/>
  <label>Mail du stagiaire : </label><input type="text" name="ajoutStagiaireMail" /><br/>
  <label>Telephone du stagiaire : </label><input type="text" name="ajoutStagiaireTel" /><br/>
  </div>
  <a href="#" onclick="hidform.style.display = 'block'; return false;">Show Form</a><br/>
  <input type="submit" value="Ajouter un stagiaire" />
</form>

First remove the HTML from your html so we control it through your php function.

Remove this:

<h5>Et ses employés ?</h5>
            <label>Nom d'un stagiaire : </label><input type="text" name="ajoutStagiaireNom"/><br/>              
            <label>Mail du stagiaire : </label><input type="text" name="ajoutStagiaireMail"/><br/>
            <label>Telephone du stagiaire : </label><input type="text" name="ajoutStagiaireTel"/><br/>

Secondly in change your <?php ajouterStag(); ?> so it is checking if the button was clicked.

So it'll become <?php if(@$_POST) { ajouterStag(); } ?>

That way the function that displays the HTML code will run only if button is posted.

So your final code would be :

<?php if(@$_POST) { ajouterStag();  } ?> 
<form method="post" action="AjoutFormation.php">        
    <input type="submit" value="Ajouter un stagiaire"/>
</form>

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.