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!

14
  • 1
    I don't see you instantiating the class? Commented Feb 16, 2018 at 19:37
  • Try to set the $stmt variable and the bind preparation after you store locally the data of $params Commented Feb 16, 2018 at 19:37
  • 500 error php problem, eg, namespacing is wrong, htaccess is fubar, class no exist, etc Commented Feb 16, 2018 at 20:10
  • Your register view doesn't include the project_manager class Commented Feb 16, 2018 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. Commented Feb 16, 2018 at 20:26

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

24 Comments

i've added the code from my config file, sorry for missing it out
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.
Im unsure as to what your asking, ive called the function directly from my index and the function inserts to my db as expected.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.