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

问题描述:

所以我新的PHP和我这里有这个网站页面上有两个按钮(被包含在URL中的id值):如何检查在PHP中按下哪个输入按钮?

<!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";} 
}?> 

而且我总是在我的页面上打印出“失败”。我一直在Google上搜索几个小时,并且我不明白我在哪里会出错。如果有人能帮助,将不胜感激。此外,如果任何人可以阐明什么相当于

<input type="textarea"> 

会是很好的。谢谢!

+0

var_dump($ _ POST)产生了什么?另外我知道有2个提交按钮确实有效,只会发送哪个被点击。 – Class

+0

将您输入的类型更改为“提交”而不是“按钮” – Fadey

+0

“等同于textarea”是什么意思? – Brovoker

使用

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

代替按钮类型与他们的onclick事件。

+0

我收到了“invalid story id”。错误,当我这样做,我不再重定向到save_story页面。 我知道一个事实,我有一个正确的ID在我的数据库中的条目。 – user3349697

+0

你在哪一行得到这个错误? – Brovoker

+0

等待它的作品,我只是没有意识到它是由于我的头()返回到原来的页面。 – user3349697

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