我对下面的代码有疑问。单击保存按钮时不会调用我的函数。
这是以下保存功能的代码,
if(isset($_POST['Save'])) // If the submit button was clicked
{
echo "hgfd";
$post['ProductSegmentCode'] = $_POST['ProductSegmentCode'];
$post['ProductSegment'] = $_POST['ProductSegment'];
$post['ProductGroup'] = $_POST['productgroup'];
// This will make sure its displayed
if(!empty($_POST['ProductSegment'])&&!empty($_POST['ProductSegmentCode'])&&!empty($_POST['productgroup']))
{
echo "SAVE";
$news->addNews($post);
?>
<script type="text/javascript">
alert("Created Sucessfully..!!");
</script>
<?
}
else
{
?>
<script type="text/javascript">
alert("Enter Mandatory Fields");
</script>
<?
}
}
以下是html中的按钮格式,
<div style="width:70px; height:32px; float:left; margin-top:16px; margin-left:4px;">
<input name="Save" type="button" class="button" value="Save">
</div>
最佳答案
您的按钮是type="button"
;要获取要提交的表单,它需要是 type="submit"
。尝试用此更新它,它应该可以工作(也等待您的表单有 action="post"
,或者没有指定操作;默认为 post
):
<input name="Save" type="submit" class="button" value="Save" onclick="Save" />
此外,您在按钮中使用了onclick="Save"
。这表明您有一个名为 Save()
的相应 JavaScript 函数 - 不过,根据您的代码示例,您没有显示该函数。我假设这是错误的并且可以安全地删除( value="Save"
也可以删除,因为您只需要检查 isset($_POST['Save'] )
而不是它的实际值)。所有就地更改应该为您提供:
<input name="Save" type="submit" class="button" />
如果您确实有一个名为 Save()
的 JavaScript 函数,请发布其代码,我可以修改。
关于php - 在php中使用按钮保存功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12721012/