【问题标题】:How do I check which input button has been pressed in PHP?如何检查 PHP 中按下了哪个输入按钮?
【发布时间】:2014-03-31 08:07:33
【问题描述】:

所以我是 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"> 

那会很棒。谢谢!

【问题讨论】:

  • var_dump($_POST) 产生什么?我也知道有 2 个提交按钮确实有效,并且只会发送被点击的那个。
  • 将输入的类型更改为“提交”而不是“按钮”
  • “等同于 textarea”是什么意思?
  • @Brovoker,我只是想知道是否有一个多行输入类型可以像文本框一样通过 $_POST。
  • 那么 textarea 本身有什么问题?

标签: php html post textarea


【解决方案1】:

使用

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

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

【讨论】:

  • 我收到“无效的故事 ID”。执行此操作时出错,并且我不再被重定向到 save_story 页面。而且我知道我的数据库中有一个具有正确 ID 的条目。
  • 等待它的工作,我只是没有意识到由于我的 header() 它正在返回原始页面。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多