Variable Persistance

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
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Variable Persistance

Post by iG9 »

I'm trying to write a CMS. I have a drop down box with the names of all the different pages the user could edit. If they choose one and hit submit an edit box appears preloaded with the current content for that page. The user should be able to edit it and hit submit to update.

My problem is that since they're not selecting the page and submitting the content in the same action by the time they go to update the content the script has forgotten what page they wanted to edit.

I tried this:

Code: Select all

 
if (isset($_GET["title"])) {
    $title = mysql_real_escape_string($_GET["title"]);
}
 
but it seems to forget $title by the time the content is submitted.

I could have them select the page and input the content in one fell swoop, but then it won't pull in the existing data, which would be annoying if you only wanted to change the content instead of replacing it.

Any help would be greatly appreciated. I need this to work tonight so I can do the design side of things tomorrow.

ps Sorry if this is obvious, it's very late here and I've been working on this project all day. :banghead:
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Variable Persistance

Post by mattpointblank »

Do you want the $title variable so you can output "The page called '$title' was updated" or something? You can access it if it's through a form using $_POST['title'].
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: Variable Persistance

Post by iG9 »

I need it for the MySQL query string, "update $table set content = '$newContent' where title = '$title'". I know what you mean about using the superglobal, the problem is I'm not submitting it at the same time as the new content. First I'm submitting the title, which pulls in the content of the page for editing. Then I'm submitting the content at which point the script has forgotten about the title.

The only way I can think of to do it is to make a table on the MySQL server to hold the last submitted title, then pull it in when I need it, but that seems like a sloppy way to do it and I'd like to find a better way.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Variable Persistance

Post by mattpointblank »

Ah. The most typical way to do it is to store a unique identifier (usually ID number) in a hidden form field in the part where the user edits content, then you can access it as part of the $_POST array.
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: Variable Persistance

Post by iG9 »

Good idea. Any idea how I can have it update to the last submitted title?
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Variable Persistance

Post by mattpointblank »

Well, I'd typically structure it like so:

They access a URL like: editarticle.php?id=123

You then query your database for an article with ID number 123.

Then output your edit article form after assigning the query results to variables, including the values like so:

<input name="title" value="<?php echo $title; ?>" />

Make sure to include the article ID number as a hidden field, like I said.

Send the user to your form processing pages, clean up the data they entered (try mysql_real_escape_string, htmlentities(), strip_tags(), etc) and assign these cleaned up values to new values. Then you can just run a query to UPDATE articles_table SET title = '$title' WHERE articleID = '$articleID'";
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: Variable Persistance

Post by iG9 »

Thanks. :)
Post Reply