发送表单POST后重新加载页面不起作用,但GET是

发送表单POST后重新加载页面不起作用,但GET是

问题描述:

thanks for reading my problem and sorry if my english is not perfect.

So, my problem is when i send a form via ajax with the method post, my page bug when reloading. After some test, i found that if i use the method get, everything works fine. I don't get it. Also, if i comment the select part, it works even with post.

Here is my code: The ajax function:

    function showDispo(){

            validate($('#frmUpdateTaux'));
        }
    function validate(form) {

            $.ajax({
                url: "updateDispo.php"
                , data: $(form).serialize()
                , datatype: 'json'
                , type: 'post'
                , error: function(error, errmsg){
                    alert(errmsg + ' :=: ' + error.responseText );
                }
                , success: function(Data) {
                    location.reload(true);
                }
            });
        }

and my html code:

<form method="get" id="frmUpdateTaux">
        <table border="0" cellspacing="0" cellpadding="2">

            <tr>
                <td align="left">
                    <label class="lbl" id="lblMois" name="lblMois" for="lstMois">Mois :</label>
                </td>
                <td align="left">
                    <select id="lstMois" name="lstMois" size="1">
                        <option value="1">Janvier</option>
                        <option value="2">Février</option>
                        <option value="3">Mars</option>

                    </select>
                </td>
            </tr>
            <tr>

                <td align="left"  colspan="2">
                <input type="button" id="btnGenerate" name="btnGenerate" value="Mettre à jour" onclick="showDispo()">
                </td>
            </tr>
    </form> 

UpdateDispo.php

require_once("BD.php"); $month= $_POST["lstMois"]; /*query */ $insertNewIndispo ='...' //execution of the query 

//QUERY INSERT $insertNewIndispo ='...'

$exec = BD::run($insertNewIndispo, BD::QUERY_INSERT, false, false);

FYI: I want to reload the page to display the results of a query run in this page and new values are inserted in the DB table by UpdateDispo.php

Do you have an idea where the problem comes from?

Thanks.

perhaps the problem you have is the following:

<form method="get" id="frmUpdateTaux">

How can you get $_POST if you are sending $_GET?

If you want to use ajax, then try:

<form onsubmit="return false;" id="frmUpdateTaux">

Then you can use your ajax code to catch the variables you are sending.

Btw: For ajax I use the following code to catch form data:

    // First declare your variables by their ID.
    var lstMois = document.getElementById('lstMois').value;
    var ajax = ajaxObj("POST", "put here your filename(can be the same as the one you are currently browsing)");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            if(ajax.responseText == "success"){
                // Do something.
                } else {
                // Something went wrong, alert them.
                alert("Oops! Something went wrong. Please try again.")
            }
        }
    }
    ajax.send("lstMois="+lstMois);
}

Then you should have php code in the page you are referring to, which will have something like this:

$lstMois = $_POST['lstMois'];
// Do whatever you want with it.
// If everything went well:
echo "succes";
exit();

The ajax code will see php has sent 'succes' to it, and will do whatever you say in

if(ajax.responseText == "success"){

}

PS. Sorry for my English, I'm not a native speaker.