skip to Main Content

I have a submit form like this.

<form method="post" id="formCard" name="formCard">
<input type="radio" name="name[]" value="0">
<input type="radio" name="name[]" value="1">
<input type="radio" name="name[]" value="2">
</form>

I want to know if it is possible to remove with javascript the name[] from the POST if value selected was == 0. Remember the form still need to submit, but it can not send name in the POST.

In other words, if in my PHP page i do isset($_POST[name]) it should return not exist, since javascript remove this from submission

$( "#formCard" ).submit();

2

Answers


  1. This one removes the "name" attribute from the element with value="0"

    document.body.querySelector('[value*="0"]').removeAttribute('name');
    
    Login or Signup to reply.
  2. Disabled controls do not submit their values.

    So you can disable the control before the form submits, which allows the user to still select it.

    $("form").submit(() => {
      $("[name='name[]'][value='0']").attr("disabled", "disabled");
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form method="post" id="formCard" name="formCard">
      <input type="radio" name="name[]" value="0">
      <input type="radio" name="name[]" value="1">
      <input type="radio" name="name[]" value="2">
      <button type='submoit'>
    submit
    </button>
    </form>

    Select 1 or 2, submit, check network tab and payload, the value will be included. Then select 0, submit, check network tab and payload and it won’t be included.

    Depending on your requirement, you might need to re-enable the radio button (by removing the disabled attribute), eg if the submit does not go ahead due to other validation checks.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search