donglan6967 2013-09-25 02:06
浏览 54
已采纳

PHP的jQuery / AJAX执行无法正常工作

I am trying to use jQuery/AJAX to run a PHP file on a server. This PHP simply adds a row with some constants to a database. Here is my code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Submit Application</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
    $.get("http://.../submitApp.php");
}
</script>

</head>

<body>
<form id="form1" name="form1" method="post" action="javascript:doSomething()">
  <p>
    <label for="programName"></label>
    <input type="text" name="programName" id="programName" />
  </p>
  <p>
    <label for="greQuant"></label>
    <input type="text" name="greQuant" id="greQuant" />
  </p>
  <p>
    <label for="greVerbal"></label>
    <input type="text" name="greVerbal" id="greVerbal" />
  </p>
  <p>
    <input type="submit" name="submitApp" id="submitApp" value="Submit" />
  </p>
</form>
</body>
</html>

Upon pressing the submit button on the above form, nothing seems to happen. I should mention I am running this locally via DreamWeaver. I know for a fact that the code is reaching the JavaScript method and that the PHP code is functional. Anyone know what's wrong?

  • 写回答

1条回答 默认 最新

  • dtmbc1606 2013-09-25 02:26
    关注

    Use POST instead of GET to do this work.

    function doSomething() {
       var programName = $('#programName').val();
       var greQuant = $('#greQuant').val();
       var greVerbal = $('#greVerbal').val();
       $.ajax({
          type: "POST",
          url: "submitApp.php", //URL that you call
          data: { programName: programName, greQuant:greQuant, greVerbal:greVerbal } //var in post: var from js
       }).done(function(msg) {
          alert(msg);//change to something to indicate action
       }
    });
    

    and with your php, handle like this

    <?php
    
    $programName = $_POST['programName'];
    $greQuant = $_POST['greQuant'];
    $greVerbal = $_POST['greVerbal'];
    
    //do something important
    
    ?>
    

    this is just a simple example, you need apply some security to this php code

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog