Search code examples
phpjavascriptmysqlgetmysql-insert-id

php/mysql/javascript - Need to set GET variable all around (including in url) after the page has loaded


This is a very difficult problem to describe.

The big picture: An ajaxed dialog submits to a page to create a new Content Set record. After creation of the Content Set record, this newly created Content Set should be the selected content set, and all Content that is created or modified needs to have the newly created Content Set id attached. The Content Set id is created using an auto increment field in MySQL during page load.

Problem details: I have a page that uses ajax to pull up a Content Set creation dialog. The dialog submits to a blank URL, because it may be called from more than one location, and I want the dialog to submit to whatever url it was called from. After the dialog submits to the page it was called from, some PHP on the page creates a Content Set record in the MySQL database. I need the newly created record's insert id to be passed from page to page using a GET variable (easiest method since I already pass pagination variables this way), but since the page has to load before I can get the insert id, I have no way of populating all of the places that the GET variable needs to inhabit (on page links, php $_GET variable, the url).

Some things to consider:

  • I would pass the newly created insert id around via a session variable, except this causes problems when someone hits the back button, and the session variable does not revert to its previous value.
  • I would use Javascript and PHP to change the various places this insert id variable is used, except if they pull up another ajaxed dialog that submits to a blank url, the blank url will of course not have the new GET variable. Also, this seems very sloppy and would be a nightmare to maintain.
  • I would, after creation of the record, redirect back to the same page after adding the new GET variable, except I would not be able to pass POST vars thus making error and success messages a complete pain in the ass. I wouldn't want to use GET vars to tell the page what error/success message to display because GET vars get passed around page to page using $_SERVER['QUERY_STRING'] as well as blank links.
  • I could simply not make the newly created Content Set autoload after creation. This makes the user go the extra step of loading the newly created Content Set themselves, which I would like to avoid.

Is there any sort of elegant solution to this? This problem may be very unique due to the way the website is set up, but someone has to have dealt with something similar. I am hoping not to have to alter the site design too much in order to solve this one little issue. Any ideas are welcome.


Solution

  • I would have to agree with a session based solution here. Keep in mind that you should be able to create a mechanism to revert back to the old value if the users hits back. For this you would need three sessions variables one with the current value of the id, one with the previous value of the id and one that holds the url of the last page you were on. This way you should be able to check whether a user has hit the back button by checking if the current url is equal to the last url stored in your session variable and if so revert back to the old id.

    Please note that this is just a general concept and I am sure you will come across some technical issues that need figuring out before a system like this works properly.