user1538196
user1538196

Reputation: 21

Form submit fails the firstime and works upon refreshing the page

I'm running a django web app, when I first submit a form it fails, I need to refresh and submit it again to send the data within, or doing back forward on the web browser. I'm using a javascript program (editarea) for auto-coloration code on a textarea

Here is my HTML file:

<html>
<head>
    <title>Test</title>
    <script language="javascript" type="text/javascript" src="/static/edit_area/edit_area_full.js"></script>
    <script type="text/javascript">
        function load() {
            var combo = document.getElementById('selSeaShells1').value;
            editAreaLoader.init({
                id: "textarea_1"        // textarea id
                    , syntax: combo                // syntax to be uses for highgliting
                    , start_highlight: true     // to display with highlight mode on start-up
            });
        }
    </script>
</head>
<body onload="load();">
    <form id="codeid" method="post" enctype="application/x-www-form-urlencoded" name="code" action="DestinationAddress/function">
    <h3>Choose a language</h3>
    <select name="sellang1" id="selSeaShells1" onchange="load();">
        <option value="python">Python</option>
        <option value="perl">Perl</option>
        <option value="sql">SQL</option>
        <option value="cpp">C++</option>
        <option value="c">C</option>
        <option value="java">Java</option>
        <option value="css">Css</option>
    </select>
    <br>
    <textarea id="textarea_1" name="content" cols="80" rows="15" type="text"></textarea>
    <input id="thebutton" type="button" value="Submit" onclick="document.forms.codeid.submit();" />
    </form>
</body>
</html>

and here the views.py:

def function(request):

encoded_data = urllib.urlencode(request.POST)

url=urllib2.urlopen('http://webappAddress:8000/function/?' + encoded_data)

tml= url.read()

return HttpResponse(tml)

Upvotes: 0

Views: 230

Answers (1)

idlehands23
idlehands23

Reputation: 326

Your javascript failing.
var combo = document.getElementById('selSeaShells1').value;
Combo is going to be undefined because you are calling load() before the page finishes loading.
I would use jquery and do this:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> //download and get a local copy of jquery don't use googles.***<script>

$(document).ready(function() {
    load();
});

function load()
    {     
        var combo = $('#selSeaShells1 option:selected).val();

            editAreaLoader.init({
            id : "textarea_1"       // textarea id
            ,syntax: combo                // syntax to be uses for highgliting
            ,start_highlight: true      // to display with highlight mode on start-up
            });


    }

</script>

Remove onload="load();" on body tag
This will get the selection after the page loads.

Upvotes: 1

Related Questions