0
votes

I wanna send form data to test.html by using asynchronous communication. I wrote in index.html

<body>
   <form method="post" action="">
    <select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
    {% for i in json_data.items.values %}
            <option value="{{forloop.counter}}">{{ i }}</option>
    {% endfor %}
    </select>

    {% for key, values in preprocessed %}
    <select name="type" id=type{{forloop.counter}}>
    {% for counter, value in values %}
        <option value="{{forloop.counter}}">{{ value }}</option>
    {% endfor %}
    </select>
    {% endfor %}
    </form>

  <script type="text/javascript">

        $(document).ready(function () {
            $('#mainDD').on('change', function() {
              var thisType = "type" + $(this).val();
              for(i=1; i<6; i++) {
                  var thisId = "type" + i;
                  if(thisType !== thisId) {
                    $("#"+thisId).hide();
                  }
                  else {
                    $("#"+thisId).show();
                  }
              }

            }).trigger('change');

        });


  </script>

     <form id="postform" action="http://localhost:8000/app/test_view" method="POST">
      {% csrf_token %}
      <input type="submit" value="SEND">
     </form>
     <script type="text/javascript">
            $('[name=type]').change(function() {
            var array1 = [];
            var array2 =[];
                $('[name=main] option:selected').each(function() {
                array1 = $(this).text();
                console.log(array1);
            });

                $('[name=type] option:selected').each(function() {
                array2 = $(this).text();
                console.log(array2);
            });
        });
        $.ajax({
            url: 'test.html',
            dataType: 'html',
            timeout:3000,
            async: true,
            success: function(html) {
                $('.newsarea').html(html).fadeIn(5000);
            },
            error: function() {
                alert('Error');
            }
        });
     </script>
  </body>

I wanna send selected i & value's variables to test.html.Now when I put send button,nothing is shown in test.html. I wrote test.html like

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>RESULT</title>
</head>
<body>

<h2>TOPIC</h2>
<div class="newsarea">
</div>

</body>
</html>

I wanna show selected i & value's values in this place of .What is wrong in my code?How can I fix this?

1
When should the value be sent to test.html? How do you know that test.html is being visited by user? - guest271314
I wanna send these value to test.html when I put send button.I cannot understand ur 2nd question.Could u write more in detail? - user8681386
When is test.html visited by user? - guest271314
@guest271314 it is when user put send button - user8681386
You really should read about what you're doing and not copy random code from internet without understanding. - user5014677

1 Answers

0
votes

If the document originates from same origin as original document and is opened by user action you can pass the parameters as a query string.

  document.querySelector("input[type=button]").onclick = e => {
    // pass key, value pairs to `test.html`
    const test = window.open(`test.html?${key}=${value}`, "_blank");
  }

at opened window get and parse location.search

  onload = () => {
    // do stuff with key, value pairs passed from `window.opener`
    console.log([...new URLSearchParams(location.search).entries()])
  }

plnkr http://plnkr.co/edit/MlCQZpkfBXFwxdR6gz5x?p=preview