Rename PDF file name (shown as URL) w/ Submit button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Rename PDF file name (shown as URL) w/ Submit button

Post by diseman »

Hello Experts,

I'm building a mock website to teach myself PHP.

Right now I'm trying to learn how to upload a pdf file to my db, display it on webpage, delete it, and rename it. I've gotten as far as renaming the file, but am stuck with the logic and how to actually do it.

I basically have a Browse & Upload button at the top of my page. After uploading, it displays the pdf file name below in a URL fashion, which I can click and download. Then to the right I have the two buttons (Delete/Rename).

What I would like to do is click the RENAME button and the URL is converted to a <input type=text box> with the name of the pdf file inside, while at the same time a new SAVE button being presented to right of the two buttons. Then I would like to rename the pdf file and click the SAVE button and then have the SAVE button go away/hide/disappear.

If making the SAVE button appear and disappear is overly complicated, I wouldn't mind it being there, but maybe subdued until the RENAME button is clicked and it's needed.

I know it can be done, I just don't have a clue how to make it work.

Would be grateful if anyone could help.

Here is the code I have that works for everything except the RENAME:

Code: Select all


<?php

// ----------------------------------------------------------------------------------------------------------
// Connect to server then db and return error if not connected.
// ----------------------------------------------------------------------------------------------------------

include ("../_includes/dbconnect.php");

// ----------------------------------------------------------------------------------------------------------
// DELETE Button code:
// When DELETE button is presses, let's delete the record it's associated with. Code being placed on top of
// 'display record' code, so we don't have to refresh the page on DELETE with a 'HEADER' command. HEADER 
// command left in place for future example.
// ----------------------------------------------------------------------------------------------------------

$doc_id = $_POST["doc_id"];

if ($_POST['delete'])

{

$sql = "DELETE FROM documents WHERE id = " . $_POST['doc_id'];

mysql_query($sql,$con) or die("query: $query<br>" . mysql_error());

}

// ----------------------------------------------------------------------------------------------------------
// Pull all pdf document records by 'id' and sort ascending
// Display PDF documents by Name/URL> 
// Provide buttons to Rename and Delete
// ----------------------------------------------------------------------------------------------------------

$query = "SELECT id, name FROM documents ORDER BY name ASC";

$result = mysql_query($query,$con) or die('Error, query failed');

while(list($id, $name) = mysql_fetch_array($result))
{
?>
<form action="" method="post">
<table>
<tr>
<td width="300"><a href="../modules/documents.php?id=<? echo $id; ?>"><u><? echo $name; ?></u></a></td>
<td><input type="submit" value="Rename"	/></td>
<td><input type="submit" name="delete" value="Delete"	/></td>
<td><input type="hidden" name="doc_id" value="<? echo $id; ?>" /></td>

</tr>
</table>
</form>
<?
}

// ----------------------------------------------------------------------------------------------------------
// Close database connection
// ----------------------------------------------------------------------------------------------------------

mysql_close($con);

?>
I'm a real beginner (1 week), so if you could please keep replies to a beginner level I would appreciate it. Examples are very much appreciated as they really do help.

Thank you in advance...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Rename PDF file name (shown as URL) w/ Submit button

Post by requinix »

One week, eh? What you're looking for is called AJAX and it's not a first-week topic. I'd gloss over it in the second week and then actually look at it in the third.
With AJAX comes JavaScript. Anything that involves not reloading the page is a JavaScript thing. Again, not a first-week topic. Second week, sure.

How about, for now, you create a dedicated page to renaming files? If you still want to try this then okay, but it might be a bit much to learn on top of PHP.
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: Rename PDF file name (shown as URL) w/ Submit button

Post by diseman »

I've heard of AJAX, but don't know anything about it.

So, are you saying this has to be done with AJAX and cannot be done with PHP alone?

I see a lot of people staying away from putting JavaScript in their websites.

Also, this PHP stuff is hard enough without having to learn JavaScript and AJAX.

Thanks..
Post Reply