2014-05-07 27 views
0

我嘗試在php文件中使用isset($ _ POST(['submit']))提交表單,通過ajax訪問它,但它不起作用。請從下面的代碼片段:php表單無法提交通過ajax訪問哪個

<html> 
<script> 
$(document).ready(function() { 
$.ajax({ 
    url: './B.php', 
    type: 'POST', 
    success: function(data) 
    { $("#showB").html(data);} 
    }); 
}); 
</script> 
<body> 

<table style="width: 100%"> 
       <tr> 
        <td colspan="2"><div id="showB"></div></td> 
       </tr> 

      </table> 
      </td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
     </tr> 
    </table>  
</body> 
</html> 

B.php:

<html> 
    <?php 
if(isset($_POST['submit']) && !empty($_POST['submit'])) { 

$message= "halo"; 
    } 
?> 
<head> 
<title>Untitled 1</title> 
</head> 
<body> 
<table> 

     <tr> 
      <td style="height: 30px"></td> 
<td style="height: 30px"><?php if(!empty($message)) { echo  $message; } ?></td> 
      <td style="height: 30px"></td> 
      <td style="height: 30px; width: 298px;"></td> 

     </tr> 
<form method="post" action="halo.php"> 
     <tr> 
      <td>&nbsp;</td> 
      <td> 
&nbsp;<input name="submit" type="submit" value="Save Changes"/></td> 
      <td>&nbsp;</td> 
      <td style="width: 298px"> 
      &nbsp;</td> 
     </tr> 

     <tr> 
<td style="width: 244px; height: 26px;"><h5>Effective Width&nbsp;:&nbsp;</h5></td> 
<td style="width: 262px; height: 26px;"><input name="effectivewidth" type="text"/></td> 
      <td style="width: 261px; height: 26px;"></td> 
      <td style="width: 298px; height: 26px;"></td> 

     </tr> 
     </form> 
    </table> 

</body> 

</html> 

在B.php形式不能與沒有錯誤被提交。請幫忙。謝謝

+0

旁註:您可以縮短這個'如果(isset($ _ POST [ '提交'])&&空($ _ POST [ '提交' ])){'只是'if(isset($ _ POST ['submit'])){'因爲在'它沒有文本/輸入被傳遞'。 –

+0

我認爲你的ajax代碼中的url是錯誤的。它必須是'B.php'或'../ B.php' – Prateek

+0

你不會發布任何**數據**,這就是爲什麼你的變量沒有設置 –

回答

0

它是因爲你沒有通過任何數據傳遞。

<script> 
$(document).ready(function() { 
$.ajax({ 
    url: './B.php', 
    type: 'POST', 
    data: { submit: "submit"}, 
    success: function(data) 
    { $("#showB").html(data);} 
    }); 
}); 
</script> 

傳遞相應data$.ajax

https://api.jquery.com/jQuery.ajax/

+0

感謝您的回覆。我在Ajax方面很新穎。它仍然無法正常工作。只是爲了澄清,我沒有把變量從A.php傳遞給B.php。我只想提交B.php到同一頁面。 – user3610424