Submitted comment edit in textarea then save - php

SO, I am working on a short script that takes a user's "bug report" and saves it. Showing the comment at the bottom of the page with an Edit button for editing. I want for that button to take the user to another page with a textarea, their comment would be editable here and a Save button to save their report. I am not sure how to link to the exact comment the user would upload.
Like this:
This is what I have so far:
Bug Reports
<form method="POST" action="Bugs.php">
<p>Bug Name <input type="text" name="name" /><br />
Hardware Type <input type="text" name="hw" /><br />
OS <input type="text" name="os" /> <br />
Frequency of Occurence <input type="text" name="freq" /></p>
<p>Proposed Solutions<br />
<textarea name="sol" rows="6" cols="100"></textarea>
<input type="submit" name="submit" value="Create New Bug Report" /></p>
</form>
<?php
$Dir = "comments";
if (is_dir($Dir)) {
if (isset($_POST['submit'])) {
if (empty($_POST['name'])) {
$String = "Unknown Visitor";
}
else
$String = stripslashes($_POST['name']) . "|";
$String .= stripslashes($_POST['hw']) . "|";
$String .= stripslashes($_POST['os']) . "|";
$String .= stripslashes($_POST['freq']) . "|";
$String .= stripslashes($_POST['sol']);
$CurrentTime = microtime();
$TimeArray = explode(" ", $CurrentTime);
$TimeStamp = (float)$TimeArray[1] + (float)$TimeArray[0];
/* File name is " Comment.seconds.microseconds.txt" */
$SaveFileName = "$Dir/Comment.$TimeStamp.txt";
if (file_put_contents($SaveFileName, $String)>0)
echo "File \"" . htmlentities($SaveFileName) . "\" successfully saved.<br />\n";
else
echo "There was an error writing \"" . htmlentities($SaveFileName) . "\".<br />\n";
}
}
if (is_dir($Dir)) { //show submitted reports on page
$CommentFiles = scandir($Dir);
foreach ($CommentFiles as $FileName) {
if (($FileName != ".") && ($FileName != "..")) {
echo "From <strong>$FileName</strong><br />";
echo "<pre>\n";
$Comment = file_get_contents($Dir . "/" . $FileName);
echo $Comment . "<a href=edit.php>Edit</a>";
echo "</pre>";
echo "<hr />";
}
}
}
?>

You really need to put all this into a database it would be much easier.
However.. when you foreach all the comments in the folder add a $_GET variable with the file name like this:
echo $Comment . "Edit";
Then with edit section you can call urldecode($_GET['com_id']) to access the file name and edit the comment.

Related

Trying to send the value of a radio button in a form to a php variable.

I tried the following code, and I can't figure out what I'm doing wrong. I know I'm breaking rules by having the form spread out amongst two different divs, but I don't know any other way around it.
<?php
echo '<form name="form" method="POST">';
$directory = '/var/www/admin/html/content';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
echo 'Files<br>';
while($it->valid()) {
if(!$it->isDot()) {
echo '<input type="radio" name="file_picked" value="content/' . $it->getSubPathName() . ' " id="file_picked" />' . $it->getSubPathName() . '</br>';
}
$it->next();
}
echo '<input type="submit" name="pickedName" value="Edit File" /></div>
<div class="editor">
<h1>SS Code Editor</h1>';
$file_picked = $_POST['file_picked'];
$edit_field = $_POST['edit_field'];
if(isset($_POST['pickedName'])) {
//get file contents and display in textarea box
$theData = file_get_contents($file_picked);
echo "<textarea name=\"edit_field\" id=\"edit_field\" cols=\"100\" rows=\"60\">";
echo $theData;
echo "</textarea><br />";
}
if(isset($_POST['submitChanges'])) {
//grab new textarea contents and put into file.
$theData = file_put_contents($file_picked, $edit_field);
//redraw textarea with new contents
$theData = file_get_contents($file_picked);
echo "<textarea name=\"edit_field\" id=\"edit_field\" cols=\"100\" rows=\"60\">";
echo $theData;
echo "</textarea><br />";
}
?>
<input type="submit" name="submitChanges" value="Save">
</form>
You have an extra space at the end of the checkbox input value :
Replace :
value="content/' . $it->getSubPathName() . ' " id="...
with :
value="content/' . $it->getSubPathName() . '" id="...
So file_get_contents($file_picked = $_POST['file_picked'])) don't find any file (with space at the end) and returns false, which is displayed as "" in the textarea.
Your value should be stored in $_POST['file_picked']

php sending form data between files

I am new to php and form development and here's what I am trying to achieve:
Firstly i have a simple form to input just two text values:
Form1
<br>
<form action="gather.php" method="post">
Catalog:
<input type="text" name="folderName" maxlength="50">
<br>
File Name:
<input type="text" name="fileName" maxlength="50">
<br>
<input type="submit" name="formSubmit" value="Submit">
</form>
And now I have the second file called gather.php where i get theese two lines and use them to count files inside catalog etc.
<?php
if(isset($_POST['formSubmit'])){
$folderName = $_POST['folderName'];
$fileName = $_POST['fileName'];
$numberOfImages = count(glob($folderName . "/*.jpg"));
for($i = 1; $i <= $numberOfImages; $i++){
echo "<br><input type=\"text\" name=\"imie" . $i . "\"><br/>\n";
echo "<img src=\"" . $folderName . "/0" . $i . ".jpg\" height=\"50px\" width=\"50px\"><br><br>\n";
}
echo "\n<br>" . $folderName . "<br>" . $fileName . "\n";
}
?>
<br>
Final form
<br>
<form action="build.php" method="post">
<input type="submit" name="finalSubmit" value="Submit">
</form>
And this should get me to build.php file which looks more less like this:
<?php
if(isset($_POST['finalSubmit'])){
//loop and other stuff
$temp = $_POST['imie1'];
echo $temp;
}
?>
So the thing is that in this final file I'd like to get all the data that was put into text fields in the gather.php file. But I get the undefined index error on build.php saying there's nothing in the $_POST['imie1']. Can you tell me why is that? Is tehre a way to get this data from second file to the third file?
Edit: thx for the answers, as I can accept only 1 and multiple are the same I choose the user with least rep just to support her :)
You need to add the input inside the form tag, it won't be sent otherwise.
<br>
Final form
<br>
<form action="build.php" method="post">
<?php
if(isset($_POST['formSubmit'])){
$folderName = $_POST['folderName'];
$fileName = $_POST['fileName'];
$numberOfImages = count(glob($folderName . "/*.jpg"));
for($i = 1; $i <= $numberOfImages; $i++){
echo "<br><input type=\"text\" name=\"imie" . $i . "\"><br/>\n";
echo "<img src=\"" . $folderName . "/0" . $i . ".jpg\" height=\"50px\" width=\"50px\"><br><br>\n";
}
echo "\n<br>" . $folderName . "<br>" . $fileName . "\n";
}
?>
<input type="submit" name="finalSubmit" value="Submit">
</form>
Replace your gather.php with
<br>
Final form
<br>
<form action="build.php" method="post">
<?php
if(isset($_POST['formSubmit'])){
$folderName = $_POST['folderName'];
$fileName = $_POST['fileName'];
$numberOfImages = count(glob($folderName . "/*.jpg"));
for($i = 1; $i <= $numberOfImages; $i++){
echo "<br><input type=\"text\" name=\"imie" . $i . "\"><br/>\n";
echo "<img src=\"" . $folderName . "/0" . $i . ".jpg\" height=\"50px\" width=\"50px\"><br><br>\n";
}
echo "\n<br>" . $folderName . "<br>" . $fileName . "\n";
}
?>
<input type="submit" name="finalSubmit" value="Submit">
</form>
you was echo'ing the input boxes outside the form so now it will work
I think the <form> on the second form needs to come at the top of the file - it'll only submit elements inside the tag, so because you're generating your HTML and then opening the form, it's not being submitted.
<br>
Final form
<br>
<form action="build.php" method="post">
<?php
if(isset($_POST['formSubmit'])){
$folderName = $_POST['folderName'];
$fileName = $_POST['fileName'];
$numberOfImages = count(glob($folderName . "/*.jpg"));
for($i = 1; $i <= $numberOfImages; $i++){
echo "<br><input type=\"text\" name=\"imie" . $i . "\"><br/>\n";
echo "<img src=\"" . $folderName . "/0" . $i . ".jpg\" height=\"50px\" width=\"50px\"><br><br>\n";
}
echo "\n<br>" . $folderName . "<br>" . $fileName . "\n";
}
?>
<input type="submit" name="finalSubmit" value="Submit">
</form>

Can't explain why "if else" in PHP doesn't work properly

Here's the form handling
if(isset($_POST['clear'])) {
mysql_query("DELETE FROM md5dictionary");
}
if(isset($_POST['submit'])) {
if(strlen($_POST['keywords']) < 1 || ctype_space($_POST['keywords']) == true) {
$errors['blank'] = "<span class='error'> Please insert a word </span>";
} else {
$newword = $_POST['keywords'];
$db_checkExist = mysql_query('SELECT * FROM md5dictionary WHERE keywords="' . $newword . '"');
echo mysql_num_rows($db_checkExist) . "<br />";
if(mysql_num_rows($db_checkExist) > 0) {
echo "outside else " . mysql_num_rows($db_checkExist);
$finalResult = "<span class='error'> Failed!!! </span>";
} else {
echo "inside else " . mysql_num_rows($db_checkExist);
$md51 = md5($newword);
$md52 = md5($md51);
mysql_query('INSERT INTO md5dictionary(keywords, mdFive, mdFive2) VALUES ("' . $newword . '", "' . $md51 . '", "' . $md52 . '")');
$finalResult = "<span class='success'> Thank you! Wanna add another one? </span>";
}
}
}
Here's the form
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<fieldset> Input Form </fieldset>
<label for="keywords"> Input new Keywords into dictionary: </label>
<?php echo $finalResult; ?>
<?php echo $errors['blank']; ?>
<input name="keywords" type="text" />
<input name="submit" type="submit" value="Add!!" />
<input name="clear" type="submit" value="Clear Database" />
</form>
<h1> Datas: </h1>
<?php
$result = mysql_query("SELECT * FROM md5dictionary");
$rows = mysql_num_rows($result);
for ($i = 0; $i < $rows; ++$i) {
echo "Data " . $i . "<br />";
echo "keywords: " . mysql_result($result, $i, "keywords") . "<br />";
echo "md5: " . mysql_result($result, $i, "mdFive") . "<br />";
echo "md5_2: " . mysql_result($result, $i, "mdFive2") . "<br />";
}
?>
Here's the result: http://md5dictionary.hoangminhdat.com
Question: Why it keeps saying "Failed!" Why it has successfully insert information into my database?
There should be no spelling mistake
I know it will be time-consuming go through my dumb question but plss, i can't explain it myself!!
I've tested the weblink you provided.
If I insert a word then I get 'inside else' and it looks like it's inserted.
If I quickly enter this word again then I get 'failed'.
So I see no problem, isn't this what you want to achieve ?
Otherwise please rethink your question and refine what is not working for you and when.
EDIT:
If you want the successful message then you need another:
echo $finalResult;
after you defined $finalResult.
I'm not sure about what you mean when you say it inserts and it gives "fail".
Do you actually mean "INSERT INTO md5dictionary..." is executed, AND you have the " Failed!!! " code showing?
If this is so, I recommend you investiguate and revise your form flows since it is most probable that calling itself, the form tries to run twice, the second time having null values.
This is what I coming up with.
Have a good day,
S.

PHP File Uploading

I'm getting index not found errors on a processing page for $_FILES. So far as I know my code is technically correct (at least the two other people who've looked at it can't find any errors either).
So first, the function that is called that displays the form with the file upload:
function portfolioEditor($p) {
echo "<form method=\"post\" action=\"" . siteurl . "/manage/update.php\">";
echo '<input type="text" name="name" id="name" class="grid4 first" value="' . $p['name'] . '" />';
echo '<input type="text" name="posttype" id="posttype" class="grid4" value="' . $p['posttype'] . '" />';
echo "\n<br />\n";
echo '<textarea name="content" id="content" class="grid8 first">' . $p['content'] . '</textarea>';
echo "\n<br />\n";
echo '<input type="hidden" name="MAX_FILE_SIZE" value="30000" />';
echo '<input name="file" value="' . $p['image'] . '" id="file" type="file" />';
echo '<input type="submit" value="Submit" name="submit" id="submit" />';
echo '<input type="hidden" value="true" id="fileup" name="fileup" />';
echo '</form>';
}
(take it as a given that the page with the form calls portfolioEditor($p) with details for $p filled in, or blanks for a new item.)
This is the update page (without the database insert yet)
$p = $_POST;
$p['url'] = str_replace(" ", "-", $p['name']);
foreach ($p as $k => $v) {
$p[$k] = addslashes($v);
//echo $v;
}
// FILE UPLOAD IF NEEDED
if(isset($p['fileup']) && $p['fileup'] == "true") {
$loc = sitepath . "/files";
$loc = $loc . basename( $_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $loc);
}
I have no idea why this isn't working, every resource I've seen on writing your own upload script uses almost the exact same code.
You need to add this to your form:
enctype='multipart/form-data'
So your form tag becomes:
echo "<form method=\"post\" enctype='multipart/form-data' action=\"" . siteurl . "/manage/update.php\">";
add as form tag attribute encytype="multipart/form-data"
echo "<form method=\"post\" action=\"" . siteurl . "/manage/update.php\" encytype=\"multipart/form-data\">";
When you are submitting a file, the form should have enctype="multipart/form-data" defined such as
echo "<form
method=\"post\"
enctype=\"multipart/form-data\"
action=\"" . siteurl . "/manage/update.php\">";

Implementing Gravatar into custom Commenting System

I'm in the process of coding my very first blog. With the help of various tutorials, and other forums I have managed to gather a semi-working code.
Right now I have a code that takes and displays the comment, but the problem is I wish to display Gravatars beside each comment. I was just wondering how exactly I would go about implementing the code that they provided on their website.
Here is my current comment form:
<?php
}
$commenttimestamp = strtotime("now");
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$timestamp = date("l F d Y", $row['timestamp']);
printf("<hr />");
print("<p>" . stripslashes($row['comment']) . "</p>");
printf("<p>Comment by %s # %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
printf("<hr />");
}
?>
<form method="post" action="process.php">
<p><input type="hidden" name="entry" id="entry" value="<?php echo $id; ?>" />
<input type="hidden" name="timestamp" id="timestamp" value="<?php echo $commenttimestamp; ?>">
<strong><label for="name">Name:</label></strong> <input type="text" name="name" id="name" size="25" /><br />
<strong><label for="email">E-mail:</label></strong> <input type="text" name="email" id="email" size="25" /><br />
<strong><label for="url">URL:</label></strong> <input type="text" name="url" id="url" size="25" value="http://" /><br />
<strong><label for="comment">Comment:</label></strong><br />
<textarea cols="25" rows="5" name="comment" id="comment"></textarea></p>
<p><input type="submit" name="submit_comment" id="submit_comment" value="Add Comment" /></p>
</form>
If you wish for me to post the php that processes each comment here as well just ask below.
My code now:
<?php
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
$url = 'http://www.gravatar.com/avatar/';
$url .= md5( strtolower( trim( $email ) ) );
$url .= "?s=$s&d=$d&r=$r";
if ( $img ) {
$url = '<img src="' . $url . '"';
foreach ( $atts as $key => $val )
$url .= ' ' . $key . '="' . $val . '"';
$url .= ' />';
}
return $url;
}
}
$commenttimestamp = strtotime("now");
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$timestamp = date("l F d Y", $row['timestamp']);
printf("<hr />");
print("<p>" . stripslashes($row['comment']) . "</p>");
printf("<p>Comment by %s # %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
echo $imagetag = "<img src='" . get_gravatar($email) . "' />";
printf("<hr />");
}
?>
You want an image tag whose src comes from the gravatar function.
Something like:
$imagetag = "<img src='" . get_gravatar($email_address) . ' />";
(You'll need to echo this variable where you want it to display.)
The only required parameter of the get_gravatar function is the email address, so just pass this to get_gravatar and you get the url of the gravatar image.
You also may use the Libravatar service that provides a gravatar-compatible but open source and federated alternative.
It has a nice PHP library - Services_Libravatar - that's easy to use:
<?php
require_once 'Services/Libravatar.php';
$sla = new Services_Libravatar();
$imgUrl = $sla->getUrl('foo#example.org');

Categories