【问题标题】:php form cannot submit which access by ajaxphp表单无法通过ajax提交哪个访问
【发布时间】:2014-06-23 20:06:24
【问题描述】:

我尝试在通过 ajax 访问的 php 文件中使用 isset($_POST(['submit'])) 提交表单,但它不起作用。请在下面找到代码sn-ps:

<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 中的表单无法正确提交。请帮忙。谢谢

【问题讨论】:

  • 旁注:您可以将此 if(isset($_POST['submit']) &amp;&amp; !empty($_POST['submit'])) { 缩短为 if(isset($_POST['submit'])){,因为没有文本/输入“传入”它。
  • 我认为您的 ajax 代码中的 url 错误。必须是B.php../B.php
  • 您没有向其发布任何 数据,这就是未设置变量的原因

标签: php


【解决方案1】:

这是因为您没有通过 post 传递任何数据。

<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/

【讨论】:

  • 感谢您的回复。我是ajax的新手。它仍然不起作用。只是为了澄清我没有将变量从 A.php 传递给 B.php。我只想将 B.php 提交到同一页面。
猜你喜欢
  • 2021-03-29
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
相关资源
最近更新 更多