我正在寻找使用简单php的多个提交按钮的解决方案。
这是我的表单代码
<!-- Widget Starts -->
<div class="widget">
<div class="title js_opened">
<div class="icon"><img src="themes/<?php echo WEBSITE_ADMIN_PANEL_THEME; ?>/images/icons/navigation/pages<?php echo $retina_suffix; ?>.png" width="24" height="24" alt="" /></div>
<span>Fill The Fields Marked With *</span>
</div>
<div class="content">
<div class="form_row first">
<label>Title<span class="star"> *</span></label>
<div class="form_right"><input type="text" name="title" maxlength="100" value="<?php echo $session->getSession("pages_title") ;?>" /></div>
<div class="clear"></div>
</div>
<div class="form_row last">
<label>Description<span class="star"> *</span></label>
<div class="form_right"><textarea name="description" class="Editor"><?php echo $session->getSession("pages_description") ;?></textarea></div>
<div class="clear"></div>
</div>
</div>
</div>
<!-- Widget Ends -->
<!-- Widget Starts -->
<div class="widget">
<div class="title js_opened">
<div class="icon"><img src="themes/<?php echo WEBSITE_ADMIN_PANEL_THEME; ?>/images/icons/navigation/meta<?php echo $retina_suffix; ?>.png" width="24" height="24" alt="" /></div>
<span>Metadata Information</span>
</div>
<div class="content">
<div class="form_row first">
<label>Title</label>
<div class="form_right"><input type="text" name="meta_title" maxlength="250" value="<?php echo $session->getSession("pages_meta_title") ;?>" /></div>
<div class="clear"></div>
</div>
<div class="form_row">
<label>Keywords</label>
<div class="form_right"><textarea id="meta_keywords" name="meta_keywords"><?php echo $session->getSession("pages_meta_keywords") ;?></textarea></div>
<div class="clear"></div>
</div>
<div class="form_row">
<label>Description</label>
<div class="form_right"><textarea id="meta_description" name="meta_description"><?php echo $session->getSession("pages_meta_description") ;?></textarea></div>
<div class="clear"></div>
</div>
<div class="form_row">
<label>Robot</label>
<div class="form_right">
<select name="meta_robot">
<option value="">Please Choose An Option</option>
<option value="index, follow" <?php if ($session->getSession("pages_meta_robot")=="index, follow") echo "selected=\"selected\""; ?> >index, follow</option>
<option value="noindex, follow" <?php if ($session->getSession("pages_meta_robot")=="noindex, follow") echo "selected=\"selected\""; ?> >noindex, follow</option>
<option value="index, nofollow" <?php if ($session->getSession("pages_meta_robot")=="index, nofollow") echo "selected=\"selected\""; ?> >index, nofollow</option>
<option value="noindex, nofollow" <?php if ($session->getSession("pages_meta_robot")=="noindex, nofollow") echo "selected=\"selected\""; ?> >noindex, nofollow</option>
</select>
</div>
<div class="clear"></div>
</div>
<div class="form_row last">
<label>Author</label>
<div class="form_right"><input type="text" name="meta_author" maxlength="50" value="<?php echo $session->getSession("pages_meta_author") ;?>" /></div>
<div class="clear"></div>
</div>
</div>
</div>
<!-- Widget Ends -->
<div class="form_buttons">
<input type="submit" name="add" value="Save" /> <span class="no_mobile"> </span>
<input type="submit" name="add" value="Save & New" /> <span class="no_mobile"> </span>
<input type="reset" value="Clear" />
</div>
</form>
<!-- Form Ends -->
这是我的php处理页面代码(我省略了将表单结果保存到数据库中的部分。
<?php
$submit = $_POST["add"];
if ($submit == "Save")
{
header("location:pages_view.php?type=success&msg=" .urlencode($msg));
exit();
}
else
{
header("location:pages_add.php?type=success&msg=" .urlencode($msg));
exit();
}
?>
我想要实现的是,如果我按下表格中的第一个提交按钮,即保存,那么它应该保存表格并进入查看页面,如果我按下第二个提交按钮,即保存&amp;新的然后它应该保存表单数据并返回到同一页面,即添加页面。
请帮我找出解决方案。
答案 0 :(得分:0)
检查是否设置了提交按钮的名称,并根据它进行操作。
例如:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') // if form is submitted...
{
if(isset($_POST['save'])) // check whether it is the "Save" button that's being clicked
{
//code for saving
echo 'saved';
}
else // it's the "Save & New" button
{
//code for save and new
echo 'save and new';
}
}
?>
<html>
<body>
<form method="POST">
<input type="text" name="msg" />
<!-- and other input elements goes here.. -->
<input type="submit" name="save" value="Save" />
<input type="submit" name="savenew" value="Save & New" />
</form>
</body>
</html>
答案 1 :(得分:-1)
在表单的某处隐藏字段:
<input type="hidden" id="myHiddenField" value="" name="add">
制作两个按钮:
<button onclick="formSubmit(save)">Save!</button>
<button onclick="formSubmit(something)">Something else!</button>
然后应用一些Javascript:
<script language="javascript">
function formSubmit(tobesend) {
document.getElementById("myHiddenField").value(tobesend);
document.form.submit();
}
</script>
这样做是:当单击一个按钮时,hiddenfield的值被设置为某个值,然后提交表单。在你的PHP文件中,你可以检查$ _POST ['add']的值,并做你想做的任何事情。
注意:我对javascript很新,所以这可能是一个非功能性的代码。它只是为了展示如何实现你想要做的事情。