php/mysql/javascript - 页面加载后需要设置 GET 变量(包括在 url 中)

发布于 2024-10-03 02:52:25 字数 967 浏览 3 评论 0原文

这是一个很难描述的问题。

大局: ajaxed 对话框提交到页面以创建新的内容集记录。创建内容集记录后,这个新创建的内容集应该是选定的内容集,并且所有创建或修改的内容都需要附加新创建的内容集id。内容集 ID 是在页面加载期间使用 MySQL 中的自动增量字段创建的。

问题详情: 我有一个页面使用 ajax 来拉出内容集创建对话框。该对话框提交到一个空白 URL,因为它可能从多个位置调用,并且我希望该对话框提交到它被调用的任何 url。对话框提交到调用它的页面后,页面上的某些 PHP 在 MySQL 数据库中创建内容集记录。我需要使用 GET 变量将新创建的记录的插入 id 从一个页面传递到另一个页面(最简单的方法,因为我已经通过这种方式传递分页变量),但由于页面必须在我获取插入 id 之前加载,所以我没有填充 GET 变量需要驻留的所有位置的方法(在页面链接、php $_GET 变量、url 上)。

需要考虑的一些事情:

  • 我将通过会话变量传递新创建的插入 id,除非当有人点击后退按钮时这会导致问题,并且会话变量不会恢复到其先前的值。
  • 我会使用 Javascript 和 PHP 来更改使用此插入 id 变量的各个位置,除非它们拉出另一个提交到空白 url 的 ajaxed 对话框,空白 url 当然不会有新的 GET 变量。而且,这看起来非常草率,维护起来将是一场噩梦。
  • 创建记录后,我会在添加新的 GET 变量后重定向回同一页面,除非我无法传递 POST 变量,从而使错误和成功消息变得非常痛苦。我不想使用 GET vars 告诉页面要显示什么错误/成功消息,因为 GET vars 使用 $_SERVER['QUERY_STRING'] 以及空白链接在页面之间传递。
  • 我根本无法使新创建的内容集在创建后自动加载。这使得用户需要自行加载新创建的内容集,这是我想避免的额外步骤。

有什么优雅的解决方案吗?由于网站的设置方式,这个问题可能非常独特,但一定有人处理过类似的问题。我希望不必为了解决这个小问题而对网站设计进行太多改变。欢迎任何想法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静若繁花 2024-10-10 02:52:25

我必须同意这里基于会话的解决方案。请记住,您应该能够创建一种机制,以便在用户回击时恢复到旧值。为此,您需要三个会话变量,一个包含 id 的当前值,一个包含 id 的先前值,一个包含您所在的最后一页的 url。这样,您应该能够通过检查当前 url 是否等于会话变量中存储的最后一个 url 来检查用户是否点击了后退按钮,如果是,则恢复为旧 id。

请注意,这只是一个一般概念,我相信您会遇到一些技术问题,需要在这样的系统正常工作之前解决。

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.

一口甜 2024-10-10 02:52:25

很确定您想要更改 URL 哈希字符串(无论它叫什么)。因为你无法更改 URL。基本上看看像 Facebook 这样的网站是如何拥有它们的 URL 的。

Pretty sure you want to change the URL hash-string (whatever it's called). Because you can't change the URL. Basically look at how sites like Facebook have their URLs.

天生の放荡 2024-10-10 02:52:25

我理解您不愿意使用会话,但这可能是使用它的好时机。

另一个想法是在页面中使用隐藏输入。

在第一个对话框之后,向 PHP(通过 ajax)发出请求,该请求将一条记录插入到数据库中,并获取插入该记录的 id。正确的?

在 ajax 回调中,您可以:

a) 创建一个隐藏类型的输入来保存 id,在表单中进行此操作。 。

b) 执行更复杂的 javascript,将“&id=id”插入到每个链接/按钮 url 字符串中 jquery 在这里肯定有用。这很困难,因为它可以是锚链接、按钮操作,无论

我真的不明白你坚持把它放在 GET 中,但你可以通过一些额外的工作来实现它。你不能把它放在ajax之后的url中而不需要重新加载,但是你可以确保你之后的任何地方都有它。

I understand you're relucatance towards session, but this might be a good time to use it.

Another idea is to use hidden input in your pages.

After the first dialog a request is made to a PHP (thru ajax) which inserts a record to db, and gets you the id where this was inserted. Correct?

In the ajax callback you can:

a) make an input of type hidden to hold id, make this within the form. id in every post

b) perform trickier javascript to insert "&id=id" into every link/buttons url string. jquery would certainly be useful here. thisa is tough bc it can be anchor links, button actions, whatever

I don't really understand you're insistence on putting it in GET, but you can make it happen with some extra work. You can't put it in the url after ajax without a reload, but you can ensure that everywhere you afterwards has it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文