首页 > 解决方案 > 如何使用 PHP 检测按钮单击并打开新页面?

问题描述

我想知道是否有人可以帮助我。我正在尝试编写一些 PHP 代码,以便当我单击一个页面中的按钮 upload.php 时,检测到按钮单击并将我重定向到另一个页面 processing.php。我正在关注另一个教程,并进行了三次检查,我没有看到我做错了什么,但是没有像视频中那样检测到按钮单击。

这是我的 upload.php 文件的代码:

<?php include_once('includes/header.php');?>

<?php include_once('includes/classes/VideoDetailsFormProvider.php');?>

<div class="column">

<!-- //calling PHP function to create upload form -->
<?php

//create variable and assign value
$formProvier = new VideoDetailsFormProvider($con);

//call function
echo $formProvier->createUploadForm();



?>

</div>

<?php include_once('includes/footer.php');?>

这是我的附加类 VideoDetailsFormProvider.php 中的相关代码:

class VideoDetailsFormProvider{

    private $con;

    //create constructor and pass $con variable to it
    public function __construct($con){
        $this->con = $con;
    }

    //creating a function to create the upload form
    public function createUploadForm(){
        $fileInput = $this->createFileInput();
        $titleInput = $this->createTitleInput();
        $descriptionInput = $this->createDescriptionInput();
        $privacyInput = $this->createPrivacyInput();
        $categoryInput = $this->createCategoryInput();
        $uploadButton = $this->createUploadButton();
        return "
            <form action='processing.php' method='POST'>
                $fileInput
                $titleInput
                $descriptionInput
                $privacyInput
                $categoryInput
                $uploadButton
            </form> 
        ";
    }

private function createUploadButton(){
        $html = "<button name='uploadButton' class='btn btn-primary'>Upload Video</button>";
        return $html;
    }

这就是我在 processing.php 文件中的内容:

<?php include_once('includes/header.php');

//check for submission of the form or button is pressed
if(!isset($_POST['uploadButton'])){
    echo "No form data has been set";
    
}else{
    
}


?>

当我单击按钮对象时,没有任何反应。在视频中我被转移到 processing.php 并且没有显示回显消息。或者至少我应该是,但这不会发生。我确实尝试在这里查看是否可以找到一些答案,但我尝试过的一些事情没有成功。有人对我可能遗漏的东西有任何想法吗?提前致谢

标签: phphtml

解决方案


问题在于您的上传按钮,当您使用 html 表单时,它必须提交到 php 处理页面,请尝试:-

private function createUploadButton(){
    $html = "<button type='submit' name='uploadButton' class='btn btn-primary'>Upload Video</button>";
    return $html;
}

推荐阅读