Welcome Guest, Not a member yet? Register   Sign In
jQuery Poll & PHP in codeigniter - help needed.
#1

[eluser]SteveBluck[/eluser]
Hello,

I have created a jQuery poll and it works 100% fine on my localhost - Once I upload it online, it doesn't work anymore. By "doesn't work" I mean the PHP part.

The javascript file works fine but when it trys to get the information from my poll controller - nothing seems to happen. At the moment the poll controller is selecting and updating a database (i know its bad mvc practise).

Hopefully someone knows whats going on:
Code:
<?php
class Poll extends MY_Controller
{
    function Poll()
    {
        parent::Controller();
    }
    
    function index()
    {
        //get post data
        $selected = $_POST['choice'];
        
        //update table
        mysql_query("UPDATE results SET votes = votes + 1 WHERE choices = '$selected'");
        
        //query the database
        $query = mysql_query("SELECT * FROM results");
        
        //loop through and return results
        for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++)
        {
            $row = mysql_fetch_assoc($query);
                
            //make array
            $json[$x] = array("choice" => $row["choices"], "votes" => $row["votes"]);
        }
        
        //echo results
        echo json_encode($json);
    }
}
/* End of file poll.php */
/* Location: ./system/application/controllers/poll.php */
the view:
Code:
&lt;!-- Poll --&gt;
<div class="grid_4">
     <div class="dark_box">
        <h3>Vote</h3>
         <div id="pollContainer"></div>
        [removed]
            $("#pollContainer").jPoll({ pollHeading: "Please choose your favourite browser:", groupIDs: ["Firefox", "Chrome", "IE", "Opera", "Safari"] });
        [removed]
    </div>
</div>
Js code (only the nessesary parts)
Code:
//define jPoll object with some default properties
    $.jPoll = {
        defaults: {
            ajaxOpts: {
                url: "poll"
            },
            groupName: "choices",
            groupIDs: ["choice0", "choice1", "choice2", "choice3", "choice4"],
            pollHeading: "Please choose your favourite:",
            rowClass: "row",
            errors: true
        }
    };

Here is the url it's hosted on, www.iprowar.com - look at the bottom and click vote without selecting anything and you'll see the view contact the js file fine. When you select something and click vote - the php doesn't seem to do anything.

Cheers
#2

[eluser]slowgary[/eluser]
When I visited:
Code:
http://www.iprowar.com/poll

I got the error:
Code:
Fatal error: Call to undefined function: json_encode() in /home/sites/iprowar.com/public_html/system/application/controllers/poll.php on line 31

json_encode() is new to PHP5. So I guess your local server is PHP5, but your live server is PHP4. Maybe.
#3

[eluser]slowgary[/eluser]
BTW, you're putting $_POST data directly into your mysql query, which is dangerous. Basically, the client can write your mysql query if they want to. You may have omitted it for readability, but for any noobs reading this is good to mention that you should sanitize user input before trusting it with something like your database.
#4

[eluser]SteveBluck[/eluser]
I see, normally when i get this problem I place

SetEnv DEFAULT_PHP_VERSION 5

into my htaccess file, but it still doing the same thing. I have other websites on this host using PHP5 and are working fine.

Anything else you can think of?
#5

[eluser]slowgary[/eluser]
So is it PHP4? or 5? a quick call to phpinfo() will tell you. Did you delete the folder? All I'm getting is a 404 page not found.

It has to be PHP4, or the function would be there. If that's not the problem I would guess it's a pathing issue due to your htaccess, or maybe the wrong database credentials.

It has to be something like that if it works okay on localhost.
#6

[eluser]SteveBluck[/eluser]
It's using PHP Version 4.4.9 So this is the problem. Isnt there somthing I can do in the htaccess file?
#7

[eluser]slowgary[/eluser]
Not really. Putting this:
Code:
SetEnv DEFAULT_PHP_VERSION 5

into your htaccess only tells the server to use PHP5, it won't install it. Does your host have PHP5 installed?

Check out:
Code:
http://us.php.net/json_encode

If you scroll down to the comments, the 2nd or 3rd one has a homemade version of this function for PHP4 users. Try it and see if it at least eliminates the problem. I would not consider it a final fix, though. You should talk to your host about PHP5 support.
#8

[eluser]SteveBluck[/eluser]
i'm certain it has php5 installed as I have other websites using it. But I shall try to figure out whats going on. Thanks for your support!
#9

[eluser]TheFuzzy0ne[/eluser]
Please see http://php.net/json_encode and http://php.net/json_decode. On this page you will find some alternatives that should work with PHP 4 if PEAR is installed:

Code:
&lt;?php
if ( !function_exists('json_decode') ){
    function json_decode($content, $assoc=false){
                require_once 'Services/JSON.php';
                if ( $assoc ){
                    $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
        } else {
                    $json = new Services_JSON;
                }
        return $json->decode($content);
    }
}

if ( !function_exists('json_encode') ){
    function json_encode($content){
                require_once 'Services/JSON.php';
                $json = new Services_JSON;
                
        return $json->encode($content);
    }
}
?&gt;
#10

[eluser]SteveBluck[/eluser]
Hello again, i have changed php version now to 5 as you can see here http://www.iprowar.com/test.php
for some reason www.iprowar.com/poll is showing a 404 error in ie and in firefox it says "no input file specified"

So whats going on here? As I said before it works completely fine on localhost.




Theme © iAndrew 2016 - Forum software by © MyBB