2013-08-31 30 views
0

我想用jquery將數據發佈到process.php,並返回process.php文件中發生的所有事情。

我已經設法用ajax加載文件。如果我輸入

echo "Hello world!"; 

它確實返回Hello world!但它不返回我使用$ _POST的回聲。

形式

<form class="calc" method="POST" action="process.php"> 
<input id="calc-average" type="text" name="calc-average" placeholder="Goal"/> 
<input id="calc-weight" type="text" name="calc-weight" placeholder="Weight"/> 
<input id="calc-calculate" type="hidden" name="calc-calculate" value="1" /> 
<input type="submit" class="btn btn-primary calc-submit" value="Calculate"> 
</form> 

腳本

$(".calc").submit(function(event) { 
      event.preventDefault(); 
      $.post('process.php', $(".calc").serialize(), 
       function(data) { 
        $(".result").append(data); 
       } 
      ); 
}); 

Process.php

if(isset($_POST["calc-calculate"])&&isset($_POST["calc-weight"])&&isset($_POST["calc-average"])){ 
    if(!$_POST["calc-calculate"]==""&&!$_POST["calc-weight"]==""&&!$_POST["calc-average"]==""){ 
     if($_POST["calc-calculate"]==$subjectid){ 
      $a = $_POST["calc-weight"]; 
      $y = $_POST["calc-average"]; 
      $q = $weights; 
      $z = $average; 
      $tobe = (-($a*$y+$q*$y-$q*$z)/$a)*-1; 
      if($tobe>10 or $tobe<0){ 
       echo " | Not possible yet"; 
      }else{ 
       echo " | The grade you need is: " . round($tobe,1); 
      } 
     } 
    } 
} 
echo "Hello wordl!"; 

如果我刪除了,如果返回語句,它們是身份不明。

因此,有誰知道我可以如何得到這個工作?

謝謝!在jQuery代碼

$(".calc").submit(function(event) { 
      event.preventDefault(); 
      $.post('process.php', $(".calc :input").serialize(), 
       function(data) { 
        $(".result").append(data); 
       } 
      ); 
}); 
+0

檢查控制檯是否有錯誤 –

+0

的確如此。但它可能是序列化不工作我以爲... – Ahmed

+0

@TusharGupta XHR完成加載:「http:// localhost:8080 /等級/ process.php」。那裏沒有錯。 – Ahmed

回答

0

喜能不能請你這個該代碼爲我工作的罰款。

嘗試在post請求後添加return false

$(".calc").submit(function(event) { 
     event.preventDefault(); 
     $.post('process.php', $(".calc").serialize(), 
      function(data) { 
       $(".result").append(data); 
      } 
     ); 
     return false; 
}); 
+0

對不起。 ) – Ahmed

0

+0

它沒有工作。感謝CronosS我知道這是因爲同一類的多個表單。不管怎麼說,還是要謝謝你 ;) – Ahmed