I'm stuck and my eyeballs are about to pop out and go splat on my keyboard. I seem to be having some strange problem with a very simple function that I have used many times before, drupal_goto(). I am wondering if there are some cases where it doesn't work quite right. Though, the case with which I am getting the error is used successfully elsewhere in the module. I'm not really sure what's going on.
I'm running drupal 5.6, and all I'm trying to do is redirect the user to the page they were on before they decided to use the little edit form I wrote. It goes like this: The user is on a page with some text links. The user can click on these links to edit them. Once clicked, the user is taken to a simple form so they can edit the term (not a taxonomy term, just a word stored in a table). I have a form function, and a submit function which edits the database entry. Then, once the db update is complete, the user should go back to the page they started on--the one with the list of words. The form works just fine, and the submit function updates the database record without error. But on the next line of the function, something goes wrong. Instead of the user going to the page where they are supposed to go, they get a "Page Not Found" error.
Here is the submit function in question:
function edit_maincategory_form_submit($form_id, $form_values)
{
$category_edit = $form_values['edit'];
$main_cat_id = $form_values['main_cat_id'];
// update the database record
db_query("update {business_main_categories} set main_cat_name = '%s' where main_cat_id = %d", $category_edit, $main_cat_id);
drupal_set_message('Main category edited.');
drupal_goto('business/categories/administer');
}
business/categories/administer is an actual page that works just fine, takes no arguments, and causes no problems anywhere. I've disabled and enabled the module many times, tried to point drupal_goto to other pages, and nothing works. It's as if something in this little function is killing the drupal_goto function. Oh, and even the drupal_set_message works. It shows up on the "Page not found" page... None of the "category" words are nodes or anything.
Anyone have any thoughts on this? It's probably something simple that I'm not seeing...
Comments
...
Fascinating!
And what exactly do you see in the browser's address bar on that "Page not found" page?
Do you see http://example.com/business/categories/administer?
Perhaps you have a spelling mistake? perhaps in your hook_menu you typed 'buisness' instead of 'business'?
(BTW, you shouldn't do drupal_goto() in hook_submit, as it breaks drupal_execute(). Return the string instead.)
I thought that, too...
I thought I had misspelled something, too, either in the callback or in the function call. It seems like I didn't. I checked it all three times, and then I even copied and pasted directly form a function that has the same drupal_goto() call (exactly the same call, but in a regular, non-submit function), and got the same results. I also did a copy and paste from my menu callback, just to be thorough, and still got the same results.
And about the drupal_goto, I did a little digging because there was something about a return statement nagging the back of my mind. And I found the page in the api docs that say to use return instead. So, I used this statement: return 'business/categories/administer'; and got the exact same result.
I get a page not found error, along with my drupal_set_message() message, and the URL looks a bit odd. For example, if the category term I am editing is "Recreation," and I want to edit it to "Recreation Outdoors," the URL on the page not found error looks like this:
example.com/R
I've tried a bunch of different categories/terms, thinking that the one I was working on was anomalous. Well, the url always ends up being example.com/[the first letter of whatever my edit was]. So, editing the word "Travel" would result in a url of example.com/T. Further, if I edit "Travel" to something completely different, like "Foo,", the url is example.com/F.
I hope that sheds some light on the issue, which is quite strange and has boggled my mind. It's boggling right now.
Thanks,
Caleb
http://www.education-finder.net
More strangeness
In a supporting module I wrote, I have another form submit function that is almost an exact duplicate of the one in my first post. The last line of the function is a return statement to the main page of this little module. It works just fine. It works how the submit function in the main module is supposed to work.
So, here I have two functions, inside their respective modules, and both are supposed to do the exact same thing except to different tables. One returns the proper url and sends the user to the right page, and the other one sends the user to some weird page that doesn't exist. Aside from misspelling a url, which I have checked numerous times, what could be causing this?
Thanks,
Caleb
http://www.education-finder.net
Progress Report
OK, I have discovered what is NOT the problem. It's not the spelling or url or anything...there's something else going on here. I copied the path from the hook_menu function and pasted it into the url. As I suspected, I landed on the correct page. Then, I pasted the same path into the return statement at the end of the submit function. As usual, the database was updated, but the page returned was not found and there was that funny url (see previous posts for a description of "funny url"). So, I took out the return path and stuck in another one I knew worked: return 'admin';
Well, I got the exact same page not found error. Clearly, there's something other than the return statement screwing up here. I would say that the return statement appears to be totally fine. What, then, is causing this error?
Thanks,
Caleb
http://www.education-finder.net
Look, I did it again
I did it again. And by "it," I mean, got the same Page not found error with a section of seemingly simple code pointing to the correct url path. Here is the code, which will be followed by short explanation:
As you can see, I have a simple page that calls a short form, which in turn passes the form contents on to a submit function. The submit function does indeed update the database record, but then takes be to a page not found page, with the url of example.com/L (replace "L" with any letter...it will be the first letter of my submitted edit).
HELP! This just isn't making any sense to me.
Thanks,
Caleb
http://www.education-finder.net
...
I see. I don't know why it happens.
I'd try to replace 'edit' with some other name. 'edit' had a meaning in old Drupals, but perhaps it still triggers something.
Excellent! That was it. I
Excellent! That was it. I changed 'edit' to something else, and everything went fine.
Thanks,
Caleb
http://www.education-finder.net
...
I opened an issue:
http://drupal.org/node/212840
Thanks chaoscube!
Chaoscube - me and the guys in IRC were wondering what was going on with my site. I tried nearly everything, then I found your post and bug report.
Thank you for finding this! I didn't think of the 'edit' variable being a culprit, but that's what changed for me too