0

I'm trying to do a form with a get method but I have a problem. I don't know how to get the value `. I do this code but is not working, I don't understand why.

I know this code is very simple, but I want to understand how to use the get method:

document.querySelector("button").addEventListener("click", function() {
    console.log('Click détecté ');
    var form = document.createElement('form');
    form.setAttribute('method', 'GET');
    form.setAttribute('action', 'test1.php');
    document.body.appendChild(form);

    let monSelect = document.createElement('select');
    let monOption = document.createElement('option');
    monOption.setAttribute('value', 1);
    monOption.innerText = 'choice1';
    monSelect.appendChild(monOption);
    let monOption2 = document.createElement('option');
    monOption2.setAttribute('value', 2);
    monOption2.innerText = 'choice2';
    monSelect.appendChild(monOption2);
    form.appendChild(monSelect);
    let input = document.createElement('input')
    input.setAttribute('type', "submit");

    input.setAttribute('value', "submit");
    form.appendChild(input);
  }
);
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Quelques langages</title>
</head>

<body>
  <button id="test">Ajouter un block</button>

  <script src="test.js"></script>
  <script src="jquery-3.3.1.min.js"></script>
</body>

</html>

<?php 

var_dump($_GET['typecolonne']);

?>

`

1 Answer 1

1

You're almost there. For a form element to be submitted, it needs to have a name attribute. All you need is to add

monSelect.setAttribute('name', 'premier');

and then you'll have a query string added to your GET request, like this:

https://stacksnippets.net/test1.php?premier=1

document.querySelector("button").addEventListener("click", function() {
    console.log('Click détecté ');
    var form = document.createElement('form');
    form.setAttribute('method', 'GET');
    form.setAttribute('action', 'test1.php');
    document.body.appendChild(form);


    let monSelect = document.createElement('select');
    monSelect.setAttribute('name', 'premier');
    let monOption = document.createElement('option');
    monOption.setAttribute('value', 1);
    monOption.innerText = 'choice1';
    monSelect.appendChild(monOption);
    let monOption2 = document.createElement('option');
    monOption2.setAttribute('value', 2);
    monOption2.innerText = 'choice2';
    monSelect.appendChild(monOption2);
    form.appendChild(monSelect);
    let input = document.createElement('input')
    input.setAttribute('type', "submit");

    input.setAttribute('value', "submit");
    form.appendChild(input);

  }

);
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Quelques langages</title>
</head>

<body>
  <button id="test">Ajouter un block</button>

  <script src="test.js"></script>
  <script src="jquery-3.3.1.min.js"></script>
</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.