使用表单按钮和if语句切换外部函数

时间:2015-03-21 06:02:56

标签: php function post submit

我一整天都在寻找答案。我有三个功能,都有形式。我试图使用表单从索引页面调用这些表单。发生的事情是我可以调用这些函数并在单击相应的按钮时显示它们,但是当我尝试从索引页面提交这些函数中的表单时,函数表单本身就会消失,并且不会发布任何内容。我甚至不确定我尝试做的事情是否可行,但我想我会问。

index.php
if (isset($_POST['posts'])) {
    posts();
}
if (isset($_POST['styles'])) {
    styles();
   }
if (isset($_POST['templates'])) {
    templates();
}

echo PHP_EOL."\t\t".'<span class="switch">'.PHP_EOL;
echo "\t\t\t".'<form class="mode" method="post" action="'.htmlentities($_SERVER['PHP_SELF']).'">'.PHP_EOL;
echo "\t\t\t\t".'<button class="edit" name="posts" type="submit" value="posts">Posts</button>'.PHP_EOL;
echo "\t\t\t\t".'<button class="edit" name="styles" type="submit" value="styles">Styles</button>'.PHP_EOL;
echo "\t\t\t\t".'<button class="edit" name="templates" type="submit" value="templates">Templates</button>'.PHP_EOL;
echo "\t\t\t".'</form>'.PHP_EOL;
echo "\t\t".'</span>';

和..

posts.php
if (isset($_POST['fileselect'])) {
    $fileselect = $_POST['fileselect'];
    $firstline = trim(fgets(fopen($fileselect, 'r')));
    $firstline = strip_tags($firstline);
    $strip = substr($firstline, 0, 110);
    echo PHP_EOL."\t\t".'<article class="edit">'.PHP_EOL;
    echo "\t\t\t".'<form class="edit" method="post" action="'.htmlentities($_SERVER['PHP_SELF']).'">'.PHP_EOL;
    echo "\t\t\t\t".'<span class="filename">Filename: '.basename($fileselect).'</span>'.PHP_EOL;
    echo "\t\t\t\t".'<span class="post">'.$strip.'</span>'.PHP_EOL;
    // DON'T CHANGE, READFILE IS PICKY.
    echo "\t\t\t\t".'<textarea class="editor" id="code" name="content">';
    readfile($fileselect);
    echo '</textarea>'.PHP_EOL;
    // DON'T CHANGE, READFILE IS PICKY.
    echo "\t\t\t\t".'<span class="edit">'.PHP_EOL;
    echo "\t\t\t\t\t".'<button class="save" name="save" type="submit" value="'.$fileselect.'">Save</button>'.PHP_EOL;
    echo "\t\t\t\t\t".'<button class="cancel" name="cancel" type="cancel">Cancel</button>'.PHP_EOL;
    echo "\t\t\t\t".'</span>'.PHP_EOL;
    echo "\t\t\t".'</form>'.PHP_EOL;
    echo "\t\t".'</article>'.PHP_EOL;
};

echo PHP_EOL."\t\t".'<aside class="delete">'.PHP_EOL;
echo "\t\t\t".'<form class="choose" method="post" action="'.htmlentities($_SERVER['PHP_SELF']).'">'.PHP_EOL;
echo "\t\t\t\t".'<button class="new" name="newfile" type="submit">Create New</button>'.PHP_EOL;
echo "\t\t\t\t".'<ul id="archives">'.PHP_EOL;
$filenames = glob('../posts/*.post');
rsort($filenames);
foreach ($filenames as $filename) {
    $firstline = trim(fgets(fopen($filename, 'r')));
    $firstline = strip_tags($firstline);
    $entry = preg_replace('^../posts/^', 'Filename: ', $filename);
    echo "\t\t\t\t\t".'<p class="entry">'.$entry.'</p>'.PHP_EOL;
    echo "\t\t\t\t\t".'<li>'.PHP_EOL;
    echo "\t\t\t\t\t\t".'<span class="title">'.$firstline.'</span>'.PHP_EOL;
    echo "\t\t\t\t\t\t".'<button class="edit" name="fileselect" type="submit" value="'.$filename.'">Edit</button>'.PHP_EOL;
    echo "\t\t\t\t\t\t".'<button class="delete" name="filedelete" type="submit" value="'.$filename.'">Delete</button>'.PHP_EOL;
    echo "\t\t\t\t\t".'</li>'.PHP_EOL;
};
echo "\t\t\t\t".'</ul>'.PHP_EOL;
echo "\t\t\t".'</form>'.PHP_EOL;
echo "\t\t".'</aside>'.PHP_EOL;

我更新了这个以避免一点混乱。如果我这样称之为没有条件的东西,那么我正在使用一个函数:posts();

当加载index.php并且编辑,删除和新的所有工作都花哨时,会显示posts函数。如果我使用表单来调用它,它会显示但不像以前那样工作。

奇怪的是,据我所知,它应该正常工作,因为它全部发布到index.php。

点击的帖子按钮显示表单已提交并提供了正确的答案。

</header>
<aside class="delete">
    <form class="choose" method="post" action="/nibble/admin/admin.php">
        <button class="new" name="newfile" type="submit">Create New</button>
        <ul id="archives">
            <p class="entry">Filename: 2015-03-20-02:28:38.post</p>
            <li>
                <span class="title">PHP Breadcrumb</span>
                <button class="edit" name="fileselect" type="submit" value="../posts/2015-03-20-02:28:38.post">Edit</button>
                <button class="delete" name="filedelete" type="submit" value="../posts/2015-03-20-02:28:38.post">Delete</button>
            </li>
...snip...
        </ul>
    </form>
</aside>
<span class="where">Editing templates.</span>
<span class="switch">
    <form class="mode" method="post" action="/nibble/admin/admin.php">
        <button class="edit" name="posts" type="submit" value="posts">Posts</button>
        <button class="edit" name="styles" type="submit" value="styles">Styles</button>
        <button class="edit" name="templates" type="submit" value="templates">Templates</button>
    </form>
</span>
<footer>

现在点击了posts功能中的编辑按钮。

</header>
<span class="switch">
    <form class="mode" method="post" action="/nibble/admin/admin.php">
        <button class="edit" name="posts" type="submit" value="posts">Posts</button>
        <button class="edit" name="styles" type="submit" value="styles">Styles</button>
        <button class="edit" name="templates" type="submit" value="templates">Templates</button>
    </form>
</span>
<footer>

噗!不见了!

1 个答案:

答案 0 :(得分:0)

这似乎是一个相当不可行的问题,所以我重构代码只使用一个函数进行文件显示和操作。以下内容并不完整,我确定它并不是那么漂亮,但它解决了我根据该文件类型的按钮单击显示不同文件类型的主要问题。我发布这个以防万一它可能对某人有用。

$buttons = <<< NAV

        <span class="switch">
            <form class="mode" name="switch" method="post" action="{$_SERVER['PHP_SELF']}">
                <button class="edit" name="posts" type="submit" value="0">Posts</button>
                <button class="edit" name="styles" type="submit" value="1">Styles</button>
                <button class="edit" name="templates" type="submit" value="2">Templates</button>
                <button class="edit" name="php" type="submit" value="php">PHP</button>
            </form>
        </span>
NAV;
    echo $buttons;
    $filenames = null;
    if (isset($_POST['posts'])) {
        $filenames = glob('../posts/*.post');
        rsort($filenames);
    }
    elseif (isset($_POST['styles'])) {
        $filenames = glob('../css/*.css');
        sort($filenames);
    }
    elseif (isset($_POST['templates'])) {
        $filenames = glob('../gui/*.gui');
        sort($filenames);
    }
    elseif (isset($_POST['php'])) {
        $filenames = glob('../*.php');
        sort($filenames);
    }
    $liststart = <<< LIST

        <aside class="delete">
            <form class="choose" method="post" action="{$_SERVER['PHP_SELF']}">
                <button class="new" name="newfile" type="submit">Create New</button>
                <ul id="archives">
LIST;
    echo $liststart;
    foreach ((array) $filenames as $filename) {
        $firstline = trim(fgets(fopen($filename, 'r')));
        $firstline = strip_tags($firstline);
        $entry = preg_replace('^../posts/^', 'Filename: ', $filename);
        $listmid = <<< LIST
                    <p class="entry">{$entry}</p>
                    <li>
                        <span class="title">{$firstline}</span>
                        <button class="edit" name="fileselect" type="submit" value="{$filename}">Edit</button>
                        <button class="delete" name="filedelete" type="submit" value="{$filename}">Delete</button>
                    </li>
LIST;
        echo $listmid;
    };
    $listend = <<< LIST
                </ul>
            </form>
        </aside>
LIST;
    echo $listend;