简体   繁体   中英

Form variables not passing through the POST

My js code dynamically creates as many input fields as the user will indicate.Thats look like this图片1

My js code for this action:

function createAdultFileds(ele) {
  let container = document.getElementById("form-adult");
  var box1 = document.createElement('form');
  box1.className='box-1';
  box1.name='adultForm';
  box1.method='post';
  box1.action='http://test1.ru/form-handler.php';

  var box2 = document.createElement('div');
  box2.className='box-2';
  form.innerHTML = '';

   for(let i = 0; i < ele.value; i++) {
     box1.innerHTML +='<label class="alegreya-sans-regular">Adult ' + (i+1) 
     + '</label><br/>' 
     +'<p><input name="adultInput ' + i + '" id="input' + i + '" 
     class="form-size" placeholder="Firstname and Lastname" 
     onkeyup="onlineUpdateAdult(' + i + ')"></p>'
     +'<p><input type="checkbox" id="checkImgAd' + i + '" 
     onClick="changeImgAdult(' + i + ')"> +Fast Pass>></p>'
     +'<p><input type="submit" value="submit" ></p>';
    container.appendChild(box1);
  }
}

For example, I choose 1 person as in the picture above, the name of this new input is "adultInput 0",we can see it on this picture图片2

When I click the submit button that redirects me to " http://test1.ru/form-handler.php " but my variables not passing through the POST and I have a blank page.

My "form-handler.php" code. Also this php file is in the local server:

<?php

    echo $_POST['adultInput 0'];

 ?>

What am I doing wrong?

I would change this

<input name="adultInput ' + i + '" id="input' + i + '" 
     class="form-size" placeholder="Firstname and Lastname" 
     onkeyup="onlineUpdateAdult(' + i + ')">

to

<input name="adultInput' + i + '" id="input' + i + '" 
     class="form-size" placeholder="Firstname and Lastname" 
     onkeyup="onlineUpdateAdult(' + i + ')">

which will omit the space in the input s name attribute, making it adultInput0 instead of adultInput 0 (and also $_POST['adultInput0'] ), which is likely to cause problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM