【问题标题】:Calling a php function after onclick eventonclick 事件后调用 php 函数
【发布时间】:2016-11-08 12:19:02
【问题描述】:

我需要帮助。

这个 PHP 代码向我展示了一个包含 MySQL 数据库中人员姓名的表格:

if ($numRows>=1)
{
echo<<<END
<td align="center" bgcolor="e5e5e5">nazwisko</td>
</tr><tr>
END;
}
for ($i = 1; $i <= $numRows; $i++)
{
$row = mysqli_fetch_assoc($result);
$nazwisko = $row['nazwisko'];   

echo<<<END
<td align="center">$nazwisko</td>
</tr><tr>
END;

}

我希望此表仅在单击按钮后显示。 HTML 代码如下所示:

<form class="form-horizontal" role="form" method="POST" action="index.php">
<input id="button" name="submit" type="submit" value="Szukaj" class="btn btn-info">

我的努力是徒劳的。我将不胜感激。

【问题讨论】:

  • 这是 PHP/MySQL 101。我敢打赌网上有成千上万的教程。
  • 您不能直接从 JavaScript 调用 PHP 函数。您需要在它们之间使用 Ajax。

标签: php html mysql


【解决方案1】:

完整代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
            <script>

        function phprun(){
         $.ajax({
                    url:'/index.php',
                    type:'post',
                    dataType:'html',
                    success:function(data){
                        $('#htmlshow').append(data);
                    },
 error:function(request,status,error){


$('#htmlshow').append("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);}

                })
        }
            </script>
        <div id="htmlshow">
        </div>
            <form class="form-horizontal" role="form" method="POST" action="/index.php" onsubmit="return false;">
            <input id="button" name="submit" type="submit" value="Szukaj" class="btn btn-info"     onclick='phprun()'>

【讨论】:

    【解决方案2】:

    您的浏览器无法解释 PHP。您必须进行 Ajax 调用。

    request = $.ajax({
        url: "/index.php",
        type: "post"
    });
    
    request.done(function (response){
        // append the html somewhere on your page
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2011-05-13
      • 1970-01-01
      相关资源
      最近更新 更多