Welcome Guest, Not a member yet? Register   Sign In
form_open() loses base url
#1

[eluser]mercy loving criminal[/eluser]
Hi there

Please excuse me if this is a simple setup / configuration problem, but I have searched high and low for a solution with no luck.

I have been working through the video tutorial to set up a blog, and have noticed a strange behaviour when trying to use form_open.

The following code...
Code:
<?=form_open();?>

...outputs
Code:
<form action="http://localhost/blogger/index.php" method="post">

BUT, the following code (as per the tutorial)...
Code:
<?=form_open('blog/comment_insert');?>

...outputs
Code:
<form action="index.php/blog/comment_insert" method="post">

i.e. losing the base url portion for the form action, which then means the form won't submit correctly.

I can't believe I am the first to have come across this problem, but can't see it reported anywhere else. Has anyone else come across this? Am I missing something obvious in my set up of CodeIgniter or Apache?

My config file includes the following lines:
Code:
$config['base_url']= "http://localhost/blogger/";
$config['index_page'] = "index.php";
$config['uri_protocol']    = "AUTO";

Any help would be MUCH appreciated.

Thanks.
#2

[eluser]mercy loving criminal[/eluser]
Or is this related somehow to the URL helper?

I've now got to the part where there is a call to the redirect function, which also loses the base url and therefore the redirect fails (it outputs index.php into the url twice, e.g. http://localhost/blogger/index.php/blog/...comments/1).

Any ideas?
#3

[eluser]Aken[/eluser]
I'm stumped. Your code looks fine. The form_open should have nothing to do with the URL helper.
#4

[eluser]Chad Fulton[/eluser]
[quote author="Aken" date="1252719930"]I'm stumped. Your code looks fine. The form_open should have nothing to do with the URL helper.[/quote]

That's true, but it does use the Config class's site_url() method, which is also what the URL helper uses.

Despite that, (and no offense meant) I'm a little skeptical about your problem. Here's why:

Here is the relevent part of the form_open() function:

Code:
$action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;

So, clearly, with both of the form_open() calls you made above, the string '://' is not present, so it's using $CI->config->site_url($action).

So, now lets take a look at the Config class's site_url function. Here's the relevant part (I've removed a bit about suffixes, to show the details better):

Code:
if ($uri == '')
{
    return $this->slash_item('base_url').$this->item('index_page');
}
else
{
    return $this->slash_item('base_url').$this->slash_item('index_page').trim($uri, '/');
}

So, case 1:
Code:
echo form_open();

As you can see, it passes $uri == '', so we get return $this->slash_item('base_url').$this->item('index_page');

Now, case 2:
Code:
echo form_open('blog/comment_insert');

This time, it doesn't pass $uri == '', so we get return $this->slash_item('base_url').$this->slash_item('index_page').trim($uri, '/');

The point is...
The only difference between what's actually being outputted is the very end of the url.

In other words, I don't believe the problem you reported could actually happen, unless the installation of CodeIgniter is corrupted somehow, or you overloaded the Config class, or your config file is messed up.

What version of CodeIgniter are you using? Where did you get it from?

If none of these things look like possible solutions, you might try downloading a fresh copy of CodeIgniter and copying your application folder into it.
#5

[eluser]mercy loving criminal[/eluser]
Thank you so much - I pretty much spent the whole day yesterday tearing my hair out trying to find a solution to this. I was using version 1.7.1, and since I have downloaded 1.7.2 all seems to be working as expected. So not sure if my copy was corrupted, or if there was some problem in the earlier release version.

Thank you.




Theme © iAndrew 2016 - Forum software by © MyBB