如何检查PHP中按下了哪个输入按钮?

时间:2014-03-31 08:07:33

标签: php html post textarea

所以我是php的新手,我在这个html页面上有两个按钮(id值包含在url中):

<!DOCTYPE html>
<head>

<title>StoryBlox is a Social Story Builder Tool</title>

<meta charset="utf-8">
<!-- these support the header/footer formatting -->
<link type="text/css" rel="stylesheet" href="css/main.css">
<link type="text/css" rel="stylesheet" href="css/header_footer.css">
<script src="js/header.js"></script>
<?php //include_once 'confirm_login.php' 
    include_once 'story_manager.php';
    include_once 'open_connection.php';
    //include_once 'functions.php';
    //sec_session_start();
    if(isset($_GET['id'])){
        $str_id = $_GET['id'];
        $draft_id = get_story_attribute($str_id, 'draft');
    }else{
        echo "Invalid story id.";
        echo "<br>";
    }
    ?>

</head>

<body>
<div id="wrapper_main">
    <div id="wrapper_content">

        <?php include_once 'header.php'; ?>

        <h1>Welcome to StoryBlox Create Story!</h1>

    </div>
    <!-- menu -->
    <!--<div id="inputs"> -->
    <form id="create_form" action="save_story.php?id=<?php echo $str_id?>" method="POST">
        <input type="text" name="storyTitle" id="title" placeholder="Enter title." autofocus/><br>
        <textarea rows="4" cols="50" name="storyDesc" id="description" placeholder="Enter description here."></textarea>

        <div id="footer">
            <input type="button" name="draftBtn" onclick="this.form.submit()" value="Save as Draft"/>
            <input type="button" name="finalBtn" onclick="this.form.submit()" value="Finished!"/>
        </div>
    </form>

    </div>
</div>
<?php include_once 'footer.php'; ?>
</body>

当我点击这两个按钮中的一个时,我会在这里看到这个php文档:

include_once 'open_connection.php';
include_once 'story_manager.php';
$mysqli = open_connection();

if($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST['draftBtn'])){
    $title = $_POST['storyTitle'];
    $desc = $_POST['storyDesc'];
    $str_id = $_GET['id'];
    update_story_title($str_id, $title);
    //update_story_description($str_id, $desc);
    header('Location: createStory.php');
}
elseif(isset($_POST['finalBtn'])){
    $title = $_POST['storyTitle'];
    $desc = $_POST['storyDesc'];
    $str_id = $_POST['storyID'];
    update_story_title($str_id, $title);
    //update_story_description($str_id, $desc);
    save_draft_as_completed($str_id);
    header('Location: ../home.php');
}else{ echo "failed";}
}?>

我总是在页面上打印出“失败”。我已经谷歌搜索了几个小时,我不明白我在哪里错了。如果有人可以提供帮助,那将是值得赞赏的。此外,如果有人能够了解相当于

的内容
<input type="textarea"> 

会很棒。谢谢!

2 个答案:

答案 0 :(得分:2)

使用

 <input type="submit" name="draftBtn" value="Save as Draft"/>

而不是带有onclick事件的按钮类型。

答案 1 :(得分:0)

尝试使用提交标记而不是按钮。 如果你想使用按钮标签,那么你可以通过隐藏字段传递值。在点击事件中设置隐藏字段的值。

相关问题