[ Switching external functions with form buttons and if statements ]
I've been searching for an answer to this all day. I've got three functions that all have forms in them. I'm trying to call those forms from my index page using a form. What's happening is that I can call up the functions and have them display when I click the appropriate button but when I try submit the forms in those functions from the index page the function forms themselves disappear and nothing gets posted. I'm not even sure if what I'm trying to do is possible but I thought I'd ask.
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>';
and..
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;
I'm updating this to avoid a bit of confusion. What I'm doing works with one function if I call it like this with no conditional stuff: posts();
The posts function shows up when index.php is loaded and the edit, delete, and new all work just dandy. If I use a form to call it, it shows up but doesn't work as it did before.
The odd thing is that as far as I'm aware, it should be working as it's all getting posted to index.php.
The clicked posts button showing that the form was submitted and the correct response given.
</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>
Now after having clicked the edit button in the posts function.
</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>
Poof! Gone!
Answer 1
This seemed to be a rather unworkable problem so I'm refactoring the code to use just one function for file display and operations. The following isn't quite complete and I'm sure it's not that pretty but it solved my major issue of displaying different file types based on a button click for that file type. I'm posting this just in case it might be of use to someone.
$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;