dougutuo9879 2014-01-28 15:32
浏览 39
已采纳

发送表单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.

  • 写回答

1条回答 默认 最新

  • dongpu9481 2014-01-28 16:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化