[英]Calling a PHP function in a dynamically created HTML table
$sql = "SELECT `ID`,`SaveName`, `CourseNumber`, `FormType`, `POSTString`, `DateModified` FROM `SavedFormsTable` WHERE 1";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Save File Name</th>
<th>Course Number</th>
<th>Date Modified</th>
<th>Load Old Form</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['SaveName'] . "</td>";
echo "<td>" . $row['CourseNumber'] . "</td>";
echo "<td>" . $row['DateModified'] . "</td>";
echo '<td><button type="button" onclick="LoadFormJS('.$row['ID'].');">Load Form!</button>';
echo "</tr>";
}
echo "</table>";
My problem is on the 4th line from the bottom. 我的问题是从底部开始的第四行。 in the HTML <button>
在HTML <button>
I am trying to create a button IN THE HTML TABLE that when clicked will call a PHP function. 我试图在HTML表格中创建一个按钮,单击该按钮将调用PHP函数。 But i have to echo the HTML in order to create the table from the mySQL database. 但是我必须回显HTML,以便从mySQL数据库创建表。
The PHP function I need to call: 我需要调用的PHP函数:
function LoadFormPHP($ID){
$con=mysqli_connect("","User636626","EasyPassword","pansyc5_SavedForms");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = sprintf("SELECT `POSTString` FROM `SavedFormsTable` WHERE `ID`=%d",$ID);
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
//mysql_close($con);
$_POST = unserialize(base64_decode($row["POSTString"]));
}
The JS function i tried (and failed) to call the PHP function from: 我尝试过(但失败了)的JS函数从以下位置调用PHP函数:
<script>
function LoadFormJS(ID){
alert(ID);
<?php LoadFormPHP(ID) ?>
}
</script>
You just have some syntax issues - 您只是有一些语法问题-
echo '<td><button type="button" onclick="LoadForm(' .$row['ID'] .')">Load Form!</button>';
What you could do to call the PHP function is to surround your button with a form using a hidden input to hold the value of $row['ID']. 您可以调用PHP函数的方法是,使用隐藏的输入将表单周围的按钮隐藏起来,以保存$ row ['ID']的值。 Clicking on the button would load the proper form at that point and you could ditch the inline JS in favor of a form action. 单击该按钮将在此时加载正确的表单,并且您可以放弃内联JS以支持表单操作。
You cannot mix html (client side) and php (server side) together. 您不能将html(客户端)和php(服务器)混合在一起。 For this kind of cases AJAX is a good tool. 对于这种情况,AJAX是一个很好的工具。 The onclick can contain a request to a javascript function that fires the request back to your server, and returns the desired HTML. onclick可以包含对javascript函数的请求,该请求会将请求激发回您的服务器,并返回所需的HTML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.