input: Write a function to generate a basic HTML form using JavaScript that takes the user's city as input and submits the details to the server. The form should prevent the default form submission behavior, collect the city input, prepare the data as JSON, and send it to a specified API endpoint using the fetch API.
  
  
  function generateForm() {
    const form = document.createElement('form');
    form.id = 'myForm';
    form.onsubmit = submitForm;
  
    const cityLabel = document.createElement('label');
    cityLabel.textContent = 'City:';
    cityLabel.for = 'cityInput';
  
    const cityInput = document.createElement('input');
    cityInput.type = 'text';
    cityInput.name = 'city';
    cityInput.id = 'cityInput';
  
    const submitButton = document.createElement('input');
    submitButton.type = 'submit';
    submitButton.value = 'Submit';
  
    form.appendChild(cityLabel);
    form.appendChild(cityInput);
    form.appendChild(submitButton);
  
    document.body.appendChild(form);
  }
  
  function submitForm(event) {
    event.preventDefault();
  
    const form = document.getElementById('myForm');
    const city = form.querySelector('#cityInput').value;
  
    const data = { city: city };
  
    const options = {
      method: 'POST',
      body: JSON.stringify(data),
      headers: {
        'Content-Type': 'application/json'
      }
    };
  
    fetch('/my-api/submit-form', options)
      .then(response => response.json())
      .then(data => {
        console.log(data);
      });
  }
  
  generateForm();
  
output: 
  def generate_form():
      form = document.createElement('form')
      form.id = 'myForm'
      form.onsubmit = submit_form
  
      city_label = document.createElement('label')
      city_label.textContent = 'City:'
      city_label.for = 'cityInput'
  
      city_input = document.createElement('input')
      city_input.type = 'text'
      city_input.name = 'city'
      city_input.id = 'cityInput'
  
      submit_button = document.createElement('input')
      submit_button.type = 'submit'
      submit_button.value = 'Submit'
  
      form.appendChild(city_label)
      form.appendChild(city_input)
      form.appendChild(submit_button)
  
      document.body.appendChild(form)
  
  def submit_form(event):
      event.preventDefault()
  
      form = document.getElementById('myForm')
      city = form.querySelector('#cityInput').value
  
      data = {'city': city}
  
      options = {
          'method': 'POST',
          'body': JSON.stringify(data),
          'headers': {
              'Content-Type': 'application/json'
          }
      }
  
      fetch('/my-api/submit-form', options)
          .then(response => response.json())
          .then(data => {
              console.log(data)
          })
  
  generate_form()
  