开发者

can I use jQuery's serialize() and an additional variable in a $.post request?

Currently, I have this line:

$.post("submitmail.php", $("#contactform").seri开发者_运维百科alize(), recvMailStatus);

I'm new to jQuery so this may be an absurdly stupid question.

But I'm making this project work with or without javascript, so if a form is submitted with this function, I want submitmail.php to just echo whether or not it was successful. If the form just submits and redirects to submitmail.php, I want it to display something other than a white screen with black text saying "mail sent successfully." To do this I figured I would just post an extra variable "fromjs" or something so that the page can render itself accordingly. What's the best way to do that?


You may append it to the address you are posting to:

$.post(
    "submitmail.php?formjs=some_value", 
    $("#contactform").serialize(), 
    recvMailStatus
);

or:

$.post(
    "submitmail.php", 
    $("#contactform").serialize() + '&formjs=some_value', 
    recvMailStatus
);


The best way is to use $.ajax function, like this :

$.ajax({
   type: "POST",
   url: "submitmail.php",
   success: function(msg){
     alert(msg);
   }
 });

You can modify it in order to display other kind of window, JQuery has some pretty plugins about alert customization. Also, you need to catch the submittion of the form and avoid your page from being redirected.


I know this is old but another way would be for example:

When the user clicks on a button it would serialize a form and add 2 variables to the list that it would then send to PHP. After which PHP would respond and send the info back to be showed as an alert. Something like:

$('#send').click(function() {
    var regname = $('#regname').val();
    var regnameid = $('#regnameid').val();
    $.post('ext/engine.php', $('#regform').serialize() + {regnameid: regnameid, regname: regname}, function(data) { alert(data); });

});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜