使用表单按钮和 if 语句切换外部函数
Switching external functions with form buttons and if statements
我整天都在寻找这个问题的答案。我有三个函数,它们都有形式。我正在尝试使用表单从我的索引页面调用这些表单。发生的事情是,当我单击相应的按钮时,我可以调用函数并让它们显示,但是当我尝试从索引页面提交这些函数中的表单时,函数表单本身消失了,并且没有任何内容被发布。我什至不确定我正在尝试做的事情是否可行,但我想我会问。
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 时会显示帖子功能,并且编辑、删除和新建都可以正常工作。如果我使用一个表单来调用它,它会显示但不能像以前那样工作。
奇怪的是,据我所知,它应该可以正常工作,因为它已全部发布到 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>
点击帖子功能中的编辑按钮后。
</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>
噗!走了!
这似乎是一个不太可行的问题,所以我正在重构代码以仅使用一个函数来进行文件显示和操作。以下内容不是很完整,我敢肯定它不是那么漂亮,但它解决了我的主要问题,即根据对该文件类型的按钮单击显示不同的文件类型。我张贴这个只是为了以防它可能对某人有用。
$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;
我整天都在寻找这个问题的答案。我有三个函数,它们都有形式。我正在尝试使用表单从我的索引页面调用这些表单。发生的事情是,当我单击相应的按钮时,我可以调用函数并让它们显示,但是当我尝试从索引页面提交这些函数中的表单时,函数表单本身消失了,并且没有任何内容被发布。我什至不确定我正在尝试做的事情是否可行,但我想我会问。
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 时会显示帖子功能,并且编辑、删除和新建都可以正常工作。如果我使用一个表单来调用它,它会显示但不能像以前那样工作。
奇怪的是,据我所知,它应该可以正常工作,因为它已全部发布到 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>
点击帖子功能中的编辑按钮后。
</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>
噗!走了!
这似乎是一个不太可行的问题,所以我正在重构代码以仅使用一个函数来进行文件显示和操作。以下内容不是很完整,我敢肯定它不是那么漂亮,但它解决了我的主要问题,即根据对该文件类型的按钮单击显示不同的文件类型。我张贴这个只是为了以防它可能对某人有用。
$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;