开发者

is the browser or server ignoring the code?

开发者 https://www.devze.com 2023-01-07 08:31 出处:网络
i made this form: <form id=\"form\" name=\"msgform\" method=\"\" action=\"\"> <input type=\"text\" size=\"40\" id=\"msg\" name=\"message\"/>

i made this form:

<form id="form" name="msgform" method="" action="">
<input type="text" size="40" id="msg" name="message"/>
<input type="submit" id="button" name="clicker" value="click" />
</form>

and this jquery script:

$(document).ready(function(){

$("#button").click(function(){

$("#form).submit(function(){


var submision= $("#form).val();

$.post("txt/process.php", submision, function(data){
alert(data);

});

});
});
});

and this is the process.php file:

<?php
echo $_POST['mess开发者_如何学JAVAage'] . "";
?>

now when i click the button the form is submited, but it sends it using the GET method because i can see it in the adress bar, but it never gets sent to the php file, and i checked to see if the names are correct and if i specify the POST method it still doesnt go to the php file. is the server or browser ignoring the code? or am i doing the whole thing wrong?

thanks


Please find the following code, it works and please go through with the documentation, it will tell you that what the mistake was being done.


$(document).ready(function(){
    $("#button").click(function(){
        $("#form").submit(function(){
            /* var submision= $("#form).val();
                THIS DOESN'T WORK TO GET ALL OF THE ELEMENTS IN 
                                FORMAT TO PASS TO $.post EVENT, 
                We can do this as I did in following example 
                        */
            $.post("txt/process.php", { msg: $("#msg").val() }, function(data){
                alert(data);
            });
            /* Also you didn't put return false statement just at the end
                           of submit event which stops propagating this event further.  
                           so it doesn't get submitted as usually it can be without ajax, 
                           So this stops sending the form elements in url. This was because 
                           by default if you define nothing in method property for form 
                           then it consider it as GET method. 
                        */
            return false;
        });
    });
});

Let me know please you are facing any issue.


You don't need to register the submit event for the form inside the click handler of the button. As it is a submit button it will automatically try to submit the form for which you register the corresponding handler:

$(function() {
    $('#form').submit(function() {
        // Get all the values from the inputs
        var formValues = $(this).serialize();
        $.post('txt/process.php', formValues, function(data) {
            alert(data);
        });
        // Cancel the default submit
        return false;
    });
});


$("#form).submit(function(){

see if this selector is missing a "

$("#form").submit(function(){

0

精彩评论

暂无评论...
验证码 换一张
取 消