Welcome Guest, Not a member yet? Register   Sign In
$_POST problem
#1

[eluser]lampard789[/eluser]
Hello,

I am new to CI. I have a problem with $_POST. In my view I created a form
Code:
<form action="<?=base_url()?>/portal/search/" method="post" >
<tr>
    <td align="right" colspan="2">
        &lt;input type="text" id="search" name="search" size="50" /&gt;
        &lt;input type="submit" name="submit" id="submit" value="Search" /&gt;
        <br /><br />
    </td>
</tr>
&lt;/form&gt;

And in my controller:
Code:
function search() {
    echo "POST vars: ".$_POST['search'];
    //... some other stuffs here
}

The result is an error as below:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: search

Filename: controllers/portal.php

Line Number: 92

I tried this in my local environment and it worked well. But when I uploaded it into my hosting, this error pops up. I'm wondering what is going wrong there.

Thanks!
#2

[eluser]zimco[/eluser]
When you uploaded to your host did you remember to change the base_url in the config file?

Code:
$config['base_url'] = "http://yourhost.com/";
#3

[eluser]Colin Williams[/eluser]
Perhaps your hosting does something weird and renames or disables the $_POST variable. Read through their docs. You can check in your code too:

Code:
if (isset($_POST))
{
   print "<pre>". htmlspecialchars(print_r($_POST, TRUE)) ."</pre>";
}
else
{
   print '$_POST not set!';
}

Also, I recommend using CI's Input library and use $this->input->post()
#4

[eluser]lampard789[/eluser]
[quote author="zimco" date="1215227904"]When you uploaded to your host did you remember to change the base_url in the config file?

Code:
$config['base_url'] = "http://yourhost.com/";
[/quote]
Yeah I changed it already. The rest of my script seems to work fine.
#5

[eluser]lampard789[/eluser]
[quote author="Colin Williams" date="1215229152"]Perhaps your hosting does something weird and renames or disables the $_POST variable. Read through their docs. You can check in your code too:

Code:
if (isset($_POST))
{
   print "<pre>". htmlspecialchars(print_r($_POST, TRUE)) ."</pre>";
}
else
{
   print '$_POST not set!';
}

Also, I recommend using CI's Input library and use $this->input->post()[/quote]
I am not sure about my hosting, but when not using CI, all the POST vars work well. Only when using CI this error occurs.
I tried your code and the output is:
Code:
Array
(
)

Any idea what might be wrong?
#6

[eluser]Carl_A[/eluser]
[quote author="lampard789" date="1215222780"]
Code:
&lt;form action="&lt;?=base_url()?&gt;/portal/search/" method="post" &gt;
<tr>


I tried this in my local environment and it worked well. But when I uploaded it into my hosting, this error pops up. I'm wondering what is going wrong there.

Thanks![/quote]

I had a similar problem.. it was because my hosting server didnt recognize the alternative php syntax..

Code:
&lt;form action="&lt;?php echo base_url()?&gt;/portal/search/" method="post" &gt;
<tr>

You might want to try the CI way of doing it, with the form_helper. Makes these sort of errors disappear

might work for you too..
#7

[eluser]lampard789[/eluser]
[quote author="Carl_A" date="1215242874"]
I had a similar problem.. it was because my hosting server didnt recognize the alternative php syntax..

Code:
&lt;form action="&lt;?php echo base_url()?&gt;/portal/search/" method="post" &gt;
<tr>

You might want to try the CI way of doing it, with the form_helper. Makes these sort of errors disappear

might work for you too..[/quote]
Actually i tried both that way and the CI form-helper way. It still doesn't work.
I really don't know what's wrong with my simple code :blank:
#8

[eluser]charlie spider[/eluser]
let's see some more of your controller

side note:

if you have your base url written as such:
$config['base_url'] = "http://yourhost.com/";

then you should remove a forward slash from this:
&lt;form action="&lt;?php echo base_url()?&gt;/portal/search/" method="post" &gt;

and make it this:
&lt;form action="&lt;?php echo base_url()?&gt;portal/search/" method="post" &gt;

otherwise your url will end up being:
http://yourhost.com//portal/search/

shouldn't affect anything, but i just think it looks ugly :\
#9

[eluser]lampard789[/eluser]
[quote author="charlie spider" date="1215257912"]let's see some more of your controller

side note:

if you have your base url written as such:
$config['base_url'] = "http://yourhost.com/";

then you should remove a forward slash from this:
&lt;form action="&lt;?php echo base_url()?&gt;/portal/search/" method="post" &gt;

and make it this:
&lt;form action="&lt;?php echo base_url()?&gt;portal/search/" method="post" &gt;

otherwise your url will end up being:
http://yourhost.com//portal/search/

shouldn't affect anything, but i just think it looks ugly :\[/quote]
Ah yeah i should remove that slash! thx ;-)

So here is my original controller (some are not yet completed):
Code:
class Portal extends Controller {

    function Portal() {
        parent::Controller();
        $this->load->helper('url');
        $this->load->helper('form');
        
    }
    
    function index() {
        $data['note'] = "Testing page";
        $this->load->view("portal/test", $data);
    }
    
    function category() {
        # This method is okay, not using any $_POST
        $this->load->model('portal/categories');
        $this->load->model('portal/headlines');
        $this->load->model('portal/latestnews');
        
        $maxHeadlines = 5;
        
        $category = $this->uri->segment(3);
        $page = $this->uri->segment(4);
        
        $data['headlines'] = $this->headlines->getHeadlines($maxHeadlines, $category, 45);
        
        $preloadImages = "";
        $first = true;
        foreach ($data['headlines'] as $headline) {
            if (!$first) $preloadImages .= ",";
            $image = "";
            if ($headline->imagelink != NULL) $image = $headline->imagelink->image;
            $preloadImages .= "'".$image."'";
            $first = false;
        }
        $data['preloadImages'] = $preloadImages;
        $data['maxHeadlines'] = $maxHeadlines;
        $data['categories'] = $this->categories->getCategories();
        $data['latestnews'] = $this->latestnews->getNews($page, 20, $data['headlines'], $category);
        $data['category'] = $category;
        $data['page'] = $page;
        
        $this->load->view("portal/index", $data);
    }
    
    function search() {
        # this method fails to catch the $_POST values
        $this->load->model('portal/searchresults');
        $displayperpage = 20;

        //testing printing $_POST value
        //print_r($_POST);
        //echo $_POST['search'];

        $str = $this->input->post('search');
        $data['results'] = $this->searchresults->getResults($str, $displayperpage);
        $data['querystring'] = $str;
        
        $this->load->view("portal/search_results", $data);
    }
}
#10

[eluser]Derek Jones[/eluser]
Do you have any .htaccess in play? If you are doing a rewrite that redirects or a regular redirect, you will lose POST data between page requests.




Theme © iAndrew 2016 - Forum software by © MyBB