doushu5451 2019-03-11 05:24
浏览 92
已采纳

jQuery.post修改了html地址

I have the following excerpt from the log-in form with javascript and jquery used for posting to the server for processing. The code is reduced to the bare minimum for clarity. The file is called 'profile.php' and when run the form data is appended to the address line as follows: 'profile.php?email=test%40gmail.com&pword=test123'. The return from the server is not processed and nothing appears in the console. The script is as follows:

  var type = 1;
  function process() {
    var saveDat = $("#AWForm" + type).serialize();
    saveDat += "&formType=" + type;
    $.post('php/test.php', saveDat, function(result) {
      console.log(result);
    }, "text");
  }
<!--          LOGIN-->
          <form id="AWForm1" class="form" role="form" onsubmit="process()">
            <legend><p id="subHeading">Enter your Login Data</p></legend>
            <div>
              <label for="email">Your Email:</label>
              <input class="form-control input-lg" name="email" placeholder="Enter Email" type="email" autofocus  required autocomplete="email"/>
            </div>
            <label for="pword">Your Password:</label>
            <div>
              <input class="form-control input-lg" name="pword" placeholder="Enter Password" type="password" autofocus  required autocomplete="current-password"/>
            </div>
            <div>
              <p class="msg" id="msg1"></p>
              <button class="btn btn-lg btn-block" type="submit" >log me in</button>
            </div>
          </form>

The php has been reduced to the following simple code for demonstaration purposes:

if (array_key_exists('formType', $_POST)) { exit ("Success"); }

Any help gratefully accepted.

</div>
  • 写回答

1条回答 默认 最新

  • douyun1860 2019-03-11 06:20
    关注

    Hi to solve your problem. Just put return keyword on your form submit.

    <form id="AWForm1" class="form" role="form" onsubmit="return process(); ">
    

    Then dont forget to return your javascript function to stop the form from submitting.

    function process() {      
        var saveDat = $("#AWForm" + type).serialize();
        saveDat += "&formType=" + type;
        $.post('index2.php?xxx=1', saveDat, function(result) {
          console.log(result);      
        }, "text");
        return false;
      }
    

    You can also event.preventDefault() function to achieve this.

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测