Page 1 of 1

Variable Persistance

Posted: Tue Jan 27, 2009 3:23 am
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:

Re: Variable Persistance

Posted: Tue Jan 27, 2009 4:31 am
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'].

Re: Variable Persistance

Posted: Tue Jan 27, 2009 9:44 am
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.

Re: Variable Persistance

Posted: Tue Jan 27, 2009 9:55 am
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.

Re: Variable Persistance

Posted: Tue Jan 27, 2009 10:00 am
by iG9
Good idea. Any idea how I can have it update to the last submitted title?

Re: Variable Persistance

Posted: Tue Jan 27, 2009 10:20 am
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'";

Re: Variable Persistance

Posted: Tue Jan 27, 2009 10:29 am
by iG9
Thanks. :)