1

I tried to add dynamically a form in JavaScript, but when I click on the submit button nothing happens. I know that is because the button has no listener, but I don't know what I have to add in the listener for a normal sending button behavior.

Here some of my code

document.getElementById("partTwo").innerHTML += "<form id='download_report' method='post' class='form-horizontal' role='form' action='include/client.process.php'>";
document.getElementById("partTwo").innerHTML += "<input type='hidden' name='jobid' value='" + jobId + "'/>";
document.getElementById("partTwo").innerHTML += "<input type='hidden' name='job' value='" + job_details.heavyInfosJson + "'/>";
document.getElementById("partTwo").innerHTML += "<input type='hidden' name='download_report' value='1'/>";
document.getElementById("partTwo").innerHTML += "<button id='download_report_button' type='submit' class='btn btn-default btn-xs'>T&eacute;l&eacute;charger le rapport</button>";
document.getElementById("partTwo").innerHTML += "</form>";

I want from the listener to juste send the form to my client.process.php

I know that it s a beginner question but any help would be appreciated. Thanks

Adam Azad
  • 11,171
  • 5
  • 29
  • 70

3 Answers3

2

Working fiddle.

You should add the inputs inside form and not inside div partTwo, so your code should be like :

document.getElementById("partTwo").innerHTML += "<form id='download_report' method='post' class='form-horizontal' role='form' action='include/client.process.php'>";
document.getElementById("download_report").innerHTML += "<input type='hidden' name='jobid' value='" + jobId + "'/>";
document.getElementById("download_report").innerHTML += "<input type='hidden' name='job' value='" + job_details.heavyInfosJson + "'/>";
document.getElementById("download_report").innerHTML += "<input type='hidden' name='download_report' value='1'/>";
document.getElementById("download_report").innerHTML += "<button id='download_report_button' type='submit' class='btn btn-default btn-xs'>T&eacute;l&eacute;charger le rapport</button>";

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
1

The problem is that <form> tag is autoclosed. innerHTML returns well formed html. See this question

Community
  • 1
  • 1
Oleksandr Papchenko
  • 2,071
  • 21
  • 30
0

You should consider using createElement rather than innerHTML, see 2946656

Here is a basic example of how to create a form and append it to the page:

var form = document.createElement("form");
form.setAttribute('action', 'contact.php');
form.setAttribute('method', 'POST');

var inputField = document.createElement("input");
inputField.setAttribute('value', 'example');
form.appendChild(inputField);

document.body.appendChild(form);
Community
  • 1
  • 1
cynicaljoy
  • 2,047
  • 1
  • 18
  • 25