tags:

views:

41

answers:

3

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['message'] . "";
?>

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

A: 

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;
    });
});
Darin Dimitrov
i made the changes and when i click the button nothing happens, i also tried this on 3 different hosts, but thanks anyway.
Petre
Probably a stupid question but did you include the jQuery script in your page? Also I would recommend you using `FireBug` in order to see if a request is sent to the server and what exactly it contains and what does the server return as a result.
Darin Dimitrov
A: 

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

see if this selector is missing a "

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

sushil bharwani
+1  A: 

Hello,

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 mehtod. 
                        */
            return false;
        });
    });
});

Let me know please you are facing any issue.

Pukhraj Prajapat
yes, i just realized that the .val() doesnt work and i used .serialize(); like dimitrov told me, but it still did not work. i used .click() before the .submit() event because i thought the .submit() is like telling the browser to submit the data but i read that its actually an event triggerd by a type="submit" input. now i got it to work(i used the return false; too), but the text still remains in the input field, should i just clear it in another way? and is it ok to use the return false; to block the browser in general? thanks for the help!
Petre
Yes, using .serialize() method is a good way to do collect value for all of the elements of a form. there is no issue if you call use firing the submit event directly. But see there are two kind of things, regarding events one is Event Propagation and second is Default Actions. Submit of a form comes in Default Action. If we wish to halt both, we can return false from our event handler.which is shortcut for calling .stopPropagation() and .preventDefault() on the event. Definitely you are making mistake somewhere in the code, please copy and paste mine which I posted here.
Pukhraj Prajapat