如何从html表单操作调用php函数?
我想在FormAction中调用php函数,并将id作为参数传递。我正在做的是,在html表单中,数据库列值将显示在文本框中,如果我编辑这些值并单击“更新”按钮,则数据库中的值应更新,“记录更新成功”消息应显示在同一页面中。我尝试了下面的代码,但没有工作。让我知道解决办法。提前谢谢如何从html表单操作调用php函数?,php,html,mysql,Php,Html,Mysql,我想在FormAction中调用php函数,并将id作为参数传递。我正在做的是,在html表单中,数据库列值将显示在文本框中,如果我编辑这些值并单击“更新”按钮,则数据库中的值应更新,“记录更新成功”消息应显示在同一页面中。我尝试了下面的代码,但没有工作。让我知道解决办法。提前谢谢 <html> <head> <link rel="stylesheet" type="text/css" href="cms_style.css"&g
<html>
<head>
<link rel="stylesheet" type="text/css" href="cms_style.css">
</head>
<?php
$ResumeID = $_GET['id'];
$con = mysql_connect("localhost", "root", "");
mysql_select_db("engg",$con);
$sql="SELECT * from data WHERE ResumeID=$ResumeID";
$result = mysql_query($sql);
$Row=mysql_fetch_row($result);
function updateRecord()
{
//If(!isset($_GET['id']))
//{
$NameoftheCandidate=$_POST[NameoftheCandidate];
$TelephoneNo=$_POST[TelephoneNo];
$Email=$_POST[Email];
$sql="UPDATE data SET NameoftheCandidate='$_POST[NameoftheCandidate]', TelephoneNo='$_POST[TelephoneNo]', Email='$_POST[Email]' WHERE ResumeID=$ResumeID ";
if(mysql_query($sql))
echo "<p>Record updated Successfully</p>";
else
echo "<p>Record update failed</p>";
while ($Row=mysql_fetch_array($result)) {
echo ("<td>$Row[ResumeID]</td>");
echo ("<td>$Row[NameoftheCandidate]</td>");
echo ("<td>$Row[TelephoneNo]</td>");
echo ("<td>$Row[Email]</td>");
} // end of while
} // end of update function
?>
<body>
<h2 align="center">Update the Record</h2>
<form align="center" action="updateRecord()" method="post">
<table align="center">
<input type="hidden" name="resumeid" value="<? echo "$Row[1]"?>">
<? echo "<tr> <td> Resume ID </td> <td>$Row[1]</td> </tr>" ?>
<div align="center">
<tr>
<td> Name of the Candidate</td>
<td><input type="text" name="NameoftheCandidate"
size="25" value="<? echo "$Row[0]"? >"></td>
</tr>
<tr>
<td>TelephoneNo</td>
<td><input type="text" name="TelephoneNo" size="25" value="<? echo "$Row[1]"?>"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="Email" size="25" value="<? echo "$Row[3]"?>">
</td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" name="submitvalue" value="UPDATE" ></td>
</tr>
</div>
</table>
</form>
</body>
</html>
这样试试
HTML
编写表单action=“”,然后按如下方式编写php代码
注意:使用form方法作为get
<?php
if(isset($_GET['id']))
{
call your function here
}
?>
在函数中,使用$_GET['fieldname']]访问所有值。简单的方法是在同一页面上执行“提交”和“更新”操作
if(isset($_POST['update']))
{
//perform update task
update($var1,var2,$etc); // pass variables to function
header('Location: http://www.example.com/');// link to your form
}
else if(isset($_POST['submit']))
{
//perform update task
submit($var1,$var2,$etc);// pass variables to function
header('Location: http://www.example.com/'); // link to next page after submit successfully
}
else
{
// display form
}
请正确格式化,不要根据用户提供的参数执行任意函数!这些都不是好的解决方案,因为如果页面上有多个表单,那么它可能就是不可管理的。在调用函数的地方编写代码比在全局范围内依赖代码更为简洁。据我所知,一个表单只会触发一个操作。-一次只提交一个表单。这是事实,但可能仍然不是最佳做法,因为您最终会遇到一系列if(isset($\u GET['action']==submitform1){do This}if(isset($GET[action']==submitform2){do that}..这不是很优雅。最好直接调用函数。
<?php
if(isset($_GET['id']))
{
call your function here
}
?>
if(isset($_POST['update']))
{
//perform update task
update($var1,var2,$etc); // pass variables to function
header('Location: http://www.example.com/');// link to your form
}
else if(isset($_POST['submit']))
{
//perform update task
submit($var1,$var2,$etc);// pass variables to function
header('Location: http://www.example.com/'); // link to next page after submit successfully
}
else
{
// display form
}