0

シンプルな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">&nbsp;*</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">&nbsp;*</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" />&nbsp;<span class="no_mobile">&nbsp;</span>
                    <input type="submit" name="add" value="Save &amp; New" />&nbsp;<span class="no_mobile">&nbsp;</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();
}
?>

私が達成したいのは、フォームの最初の送信ボタン、つまり保存を押すと、フォームを保存してページを表示し、2番目の送信ボタン、つまり保存と新規を押すと、フォームデータを保存し、同じページに戻ります。つまり、ページを追加します。

解決策を見つけるのを手伝ってください。

4

2 に答える 2

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>
于 2012-07-28T18:07:39.977 に答える
-1

フォームのどこかに隠しフィールドを作成します。

<input type="hidden" id="myHiddenField" value="" name="add">

2 つのボタンを作成します。

<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 にかなり慣れていないため、これは機能しないコードである可能性があります。やろうとしていることを達成する方法を示すだけです。

于 2012-07-28T18:02:33.350 に答える