简体   繁体   中英

Posting form data Jquery

I am trying to build a mobile app but am having some trouble getting the basics of Jquery/Javascript. I am trying to make it so I can type in any value I want into the input field and then post it, it would post above and allow me to type more into the input field and it would post above the last post.

Here is my code so far. Stumped where to go next or if I am going in the right direction.

 <!DOCTYPE HTML>
    <HTML>
     <script src="http://code.jquery.com/jquery-latest.js"></script>

    <script>

        $('#commentForm').submit(function(){ //listen for submit event
        $.each(params, function(i,param){
            $('<input />').attr('type', 'show')
                .attr('value', param.value)
                .appendTo('#commentForm');
        });



        return true;
    });



    </script>
    <BODY>
    <form id="commentForm" method="POST">
        <textarea  cols="30" rows="6" name="comment" title="Enter a comment">
        </textarea>
        <input type="submit" value="Post"/>
        <input type="reset" value="Reset"/>
    </form>
    <div id="box">

    </div>

    </BODY>

    </HTML>

Give the submit button an id called "submit"

    function onSuccess(data, status) {
        data = $.trim(data);
           //make a div with id "notification" before running this code
        $("#notification").html(data);
        $.mobile.hidePageLoadingMsg(); //used on jquery mobile to hide a loader
    }

    function onError(data, status) {
        data = $.trim(data);
        $("#notification").html(data);
        $.mobile.hidePageLoadingMsg(); //used on jquery mobile to hide a loader
    }
    $("#submit").click(function() {
        $.mobile.showPageLoadingMsg(); //used on jquery mobile to show a loader
        var formData = $("#commentForm").serialize(); //get all data from form
          //do the POST thingies
        $.ajax({
            type: "POST",
            url: "url_to_your_php_interpreter",
            cache: false,
            data: formData,
            success: onSuccess,
            error: onError
        });

        return false;
    });

I'm using this script to login an user. PS: everything you will "echo" from php interpreter will be shown on div with id "notification" wich you will (probably) create

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM