0

I already know that this is a server error and I am unable to figure out why this is happening.

File structure:

project_manager
    >ajax
        >register.php
    >classes
        >project_manager.php
    >config
        >config.php
    >js
        >scripts.js
    >views
        >register.php
    >index.php

My JavaScript is as follows:

if(valid){
    var data = $('#register-form').serializeArray();
    request = $.ajax({
        url: "ajax/register.php",
        type: "post",
        data: data,
        cache: false,
        datatype:'json',
        headers:{"cache-action":"no-cache"}
    }).done(function(data) {
        if(data.success){
            alert('class function worked');
        }
    }).fail(function(data){
        alert('fail')
    });
}

My PHP ajax file is as follows:

<?php
    include_once 'config/config.php';

    echo $project_manager->registerUser($_POST);
?>

My PHP class file where the function is located is as follows:

class project_manager{

    public $connection;

    public function registerUser($params){
        // Prepare and Bind
        $stmt = $this->connection->prepare('call addUser(?,?,?,?,?,?)');
        $stmt->bind_param("ssssss", $firstname, $surname, $displayname, $email, $password, $role);

        $firstname = $params['firstname'];
        $surname = $params['surname'];
        $displayname = $params['firstname'] + $params['surname'];
        $email = $params['email'];
        $password = $params['password'];
        $role = "test";

        $stmt->execute();
        $stmt->close();

        return json_encode(array("success"=>true));
    }

}

The following is in my config file:

<?php
    include "classes/project_manager.php";
    $connection = mysqli_connect($servername, $username, $password, $dbname);

    // Check to see if the connection failed
    if($connection === false){
        die("ERROR: Could not connect. ". mysqli_connect_error());
    }

    session_start();

    $project_manager = new project_manager();
    $database = new database();
    $database->connection = $connection;
    $project_manager->connection = $connection;
?>

My register view code is as follows:

<?php
    $params['firstname'] = 'test';//change with expected values
    $params['email'] = 'test';//change with expected values
    $params['surname'] = 'test';//change with expected values
    $params['password'] = 'test';//change with expected values

    //echo $project_manager->registerUser($_POST);
    echo $project_manager->registerUser($params);
?>



<form id="register-form" class="user-form">
     <h2 style="text-align: center; margin-bottom: 30px;">Register an account</h2>
     <div class="col-sm-12">
         <div class="col-sm-6 form-group">
            <input type='text' name="firstname" class="form-control input-md" placeholder="Firstname..."/>
        </div>
        <div class="col-sm-6 form-group">
            <input type='text' name="surname" class="form-control input-md" placeholder="Surname..."/>
        </div>
        <div class="col-sm-6 form-group">
            <input type='password' name="password" class="form-control input-md" placeholder="Please Enter A Password"/>
        </div>
        <div class="col-sm-6 form-group">
             <input type='password' class="form-control input-md" id="password-confirm" placeholder="Please Confirm Password"/>
        </div>
        <div class="col-sm-12 form-group">
            <input type="email" name="email" class="form-control input-md" placeholder="Please Enter An Email Address"/>
        </div>
        <div class="col-sm-12">
            <button type="button" id="register" class="btn btn-default pull-right">Register</button>
        </div>
    </div>
</form>

Any sort of help will be much appreciated!

  • 1
    I don't see you instantiating the class? – bos570 Feb 16 '18 at 19:37
  • Try to set the `$stmt` variable and the bind preparation after you store locally the data of `$params` – Yulio Aleman Jimenez Feb 16 '18 at 19:37
  • 500 error php problem, eg, namespacing is wrong, htaccess is fubar, class no exist, etc – Andrew Feb 16 '18 at 20:10
  • Your register view doesn't include the project_manager class – adprocas Feb 16 '18 at 20:14
  • This user insulted me when I tried to help. It's likely that he has a file path issue and is not explaining his entire process, and refuses to take troubleshooting advice. – adprocas Feb 16 '18 at 20:26
  • @adpro I wasn't calling you a dummy, i meant dummy data like test data. Sorry if that was misinterpreted – Lewis Blundell Feb 16 '18 at 20:28
  • You still aren't taking my advice by testing your register.php output in the browser, or by adding logging of some sort. You still need to include more information. – adprocas Feb 16 '18 at 20:28
  • As far as I can tell you've added the code I explained that you should test with to another view file that you're using for input. The other register.php file output needs to be tested in the actual browser to see if it's actually returning something valid for your ajax request, as that's likely where the 500 internal server error is coming up. You are likely running into a path issue with your php files, since it is in a different folder. I don't know enough to continue helping, and I don't have time. Provide more information for someone else to help. – adprocas Feb 16 '18 at 20:30
  • @adpro I added that my page echoed out success:true which is when i was testing it in the browser from the view like you asked. Im currently looking up how to log as it is something reletively new, was doing this whilst taking your advice – Lewis Blundell Feb 16 '18 at 20:30
  • @adpro the best way i can explain it is: - each view is included into the index file depending which one they want to view - WHen they submit the register form this goes from view->scripts.js->ajax file->php class and then returns – Lewis Blundell Feb 16 '18 at 20:31
  • I am not talking about testing it in your view file. You have url: ajax/register.php - point your browser to that actual php file, but modify it first to include the test data. Does that make sense? – adprocas Feb 16 '18 at 20:32
  • @adpro i think i understand what you mean – Lewis Blundell Feb 16 '18 at 20:33
  • @adpro okay so i typed the direct address for the ajax file into the browser which returns an error as you said could be the cause. The error returned register.php:1 GET http://project_manager/ajax/register.php 500 (Internal Server Error) – Lewis Blundell Feb 16 '18 at 20:37
  • See my most recent answer edit. – adprocas Feb 16 '18 at 20:40

1 Answers1

2

Since you are using these paths like this you will need to modify includes.

You are testing that the methods in project_manager.php from the view file, which gets included in your index file. So, you're likely not running into a path issue there because the path is being referenced from the same place your index file is located.

But, when your ajax tries to read something from ajax/register.php, the file paths are no longer based on the same place as the index file, which is why you should test this file directly.

You'll need to do the following in your register.php file, and likely change around how you handle the other include stuff as well in your other files, since this may not work because you're now referencing files based on the ajax directory.

include_once '../config/config.php';

This will break anything else that references it (the index), but you'll need to do this to get the ajax register php page working temporarily:

include "../classes/project_manager.php";

You can take a look at the following post for ideas about how to handle your includes:

PHP include relative path

And let PHP post errors and warnings to the browser for you, so you can read them instead of seeing that 500 internal server error page.

Showing all errors and warnings

adprocas
  • 1,863
  • 1
  • 14
  • 31
  • i've added the code from my config file, sorry for missing it out – Lewis Blundell Feb 16 '18 at 19:42
  • Ok, what do you get when you go to your register.php page? Remove the Ajax side of it first to see where the problem might be. Are you getting what you're expecting? There are a few variables at play here (database, params, stmt, etc.) that need to be checked to be working properly first. Please post what the register.php page outputs when you navigate to it in the browser. – adprocas Feb 16 '18 at 19:46
  • Im unsure as to what your asking, ive called the function directly from my index and the function inserts to my db as expected. – Lewis Blundell Feb 16 '18 at 19:50
  • You have your register.php page, correct? Can you navigate to it in the browser? You can set some default values for your project_manager class in the meantime and just make sure that the register.php page is actually displaying the json you're intending. Well, right now it will just be {"success":true} - if that's working by giving it some default test values (probably best to mimic the $_POST array) then you know all of your methods and database queries and stuff are working. – adprocas Feb 16 '18 at 19:53
  • Also, turn on PHP errors, and add logging as well. If you log errors you'll be able to see what the 500 internal server error actually is. 500 internal server error is just a server error. PHP will give you more details. When using AJAX it's more difficult to see what the error is without using some debugging methods. So, logging is a simple way to see what errors are happening and where they're coming from. – adprocas Feb 16 '18 at 19:54
  • i run my php pages through the index as an include and it displays correctly. – Lewis Blundell Feb 16 '18 at 19:56
  • But you aren't checking the actual register.php page. It's always good to check the actual page to ensure that you aren't missing something on there. – adprocas Feb 16 '18 at 19:58
  • I edited my answer to give you a tip on how to test it. – adprocas Feb 16 '18 at 19:59
  • Done as you explained, it returned success true and inserted into the db correctly – Lewis Blundell Feb 16 '18 at 20:09
  • Ok, where are you getting the 500 internal server error then? On the page where your ajax is? Can you include that code? – adprocas Feb 16 '18 at 20:12
  • @adpoc My ajax call is on a seperate scripts.js file, the js included is the js that page uses anything above just validates inputs – Lewis Blundell Feb 16 '18 at 20:14
  • you have php in your register view class, but you aren't including anything. That will return a 500 internal server error. – adprocas Feb 16 '18 at 20:15
  • The config file (holds the class) is included withn the index which also includes the view file. It access the class through this – Lewis Blundell Feb 16 '18 at 20:17
  • Ok, so where is the 500 internal server error? On what page? – adprocas Feb 16 '18 at 20:18
  • POST http://project_manager/ajax/register.php 500 (Internal Server Error) – Lewis Blundell Feb 16 '18 at 20:19
  • So, my comments about testing register.php have brought you to getting a 500 internal server error? You aren't including all of the pieces of the puzzle here. Ajax calls aren't that difficult. It seems your path in your ajax is not correct. Does the register.php file exist in ajax/register.php like it is referenced in your ajax, or project_manager/ajax/register.php like you just mentioned? – adprocas Feb 16 '18 at 20:21
  • Wait - are you saying you're testing the $project_manager->registerUser from a different file, and that register view file you're referring to isn't the same as register.php referenced in your ajax method? Please test the output of the ajax/register.php file you have in your ajax method. Actually point your browser to that file to see what you get, and use the test data I supplied. – adprocas Feb 16 '18 at 20:24
  • dummy? Find someone else to help – adprocas Feb 16 '18 at 20:25
  • I can't tell what that is, but if you turn on errors in PHP you'll be able to see what error is being returned. I'll let you look up how to do that instead of find it for you. – adprocas Feb 16 '18 at 20:48
  • Let me know if those file paths are correct. Please include them in your question. I think your ajax is likely wrong. I updated my answer. Also, enable php errors. – adprocas Feb 16 '18 at 20:52
  • attempting to do php error logging but it goes on about a php.ini file? – Lewis Blundell Feb 16 '18 at 20:59
  • Yep - or you can do it within PHP. I updated my answer. You need to learn about paths (see the link above) and more about how php works. My suggestion would be to watch some online php tutorials. There's a lot of information out there about enabling logging in php. You are not restricted to editing the ini file. You can do it in code or in your htaccess files. But, this is definitely a path issue. Have fun learning! – adprocas Feb 16 '18 at 21:02
  • Also, just a tip. You have asked two questions, but haven't selected an answer for those questions. Give credit where credit is due if the person has helped you resolve the issue. If you find that my answer fixes this for you, mark it as the accepted answer. The same goes with your other question. – adprocas Feb 16 '18 at 21:22
  • only just got around to sorting it today, the issue was with the file path. It was corrected by using $_SERVER['DOCUMENT_ROOT] before calling my config and class files. The error logging that you recommended also helped a lot, thanks :) – Lewis Blundell Feb 26 '18 at 21:26