Pass value from html form to php without submitting the form

前端 未结 5 981

I have a text box. I want to take user input from there and pass it to PHP without using submit button. I would prefer a normal button and onclick should pass d

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 21:16

    Several ways to do that...

    It sounds like you're after a non-redirecting solution, so I'd recommend jQuery (it's my fave, but there are plenty other solutions to implementing AJAX) and doing something like the following:

    Assume you have a text box and button like this:

    
    
    

    Then do something like this

    $(document).ready(function () {
        $("#btn_send").click(function () {
    
            // Do stuff BEFORE sending... //
            callAFunction();
            var test = "asdfasdf";
    
            // Send the text to PHP //
            $.post("test.php", { input: $("#txt_input").val()},
                 function(data) {
                     alert("test.php returned: " + data);
                 }
            );
    
            // Do more stuff after sending.... //
            callAnotherFunction();
        });
    });
    

    I believe that'll get what your after. For more on jQuery and further options with $.post, check here: http://api.jquery.com/jQuery.post/

    Hope that helps!

提交回复
热议问题