2

Is it possible to use an Ajax call to submit a form to a PHP file, where it then using functions from within a separate PHP file that are not declared within the file being called by Ajax?

In my case, I am using an init.php file which holds the MySQL Connection and using include() to include functions for MySQL, Page Logic, and Handling users.

init.php

session_start();
$servername = "";
$username = "";
$password = "";
$database = "";
$conn = true;

$conn = mysqli_connect($servername, $username, $password, $database);

//Include functions for MYSQL
include('functions/fn_mysql.php');
//Include functions for Page Logic
include('functions/fn_pages.php');
//Include functions to handle users
include('forms/users.php');

For example, A simple HTML Form.

<form id="fileUpload" method="post" action="" enctype="multipart/form-data">
    <input type="hidden" name="dispatch" value="file_upload" />
    <label for="fileType">What kind of document is this?</label>
    <select id="fileType" name="fileType">
        <option value="PDF">PDF</option>
        <option value="DOC">DOC</option>
    </select>
    <input type="submit" name="submit_upload" value="Submit" />
</form>

script.js

File used to submit form.

$("#fileUpload").on('submit', function(e) {
    e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'users.php',
            data: new FormData(this),
            dataType: 'json',
            contentType: false,
            cache: false,
            processData: false,
        });

users.php

This file accepts the Ajax call and using a function from a separate PHP file called fn_mysql.php will insert to the DB.

if ($_POST['dispatch'] == 'file_upload') {
   // process to DB
   $sql = "INSERT INTO doc_attributes (?u)";
   $insert_array = array ();
   $add = db_insert($sql, $insert_array);
   if (!empty ($add)) {
       // Added
   }

Now, when the user submits this form the data is then sent to the users.php file. But, is unable to now access the functions detailed in the fn_mysql.php file as they are undefined.

Without the Ajax call this works as intended. Is using this method of separating files to keep functions separate from forms not possible with Ajax?

2
  • users.php will require that you use include "/path/to/file.php" for all required files also Commented Apr 2, 2020 at 6:08
  • Then why is it that if I remove the ajax call completely, there are no errors? The files are included within the init.php file. That's why i'm confused @RamRaider Commented Apr 2, 2020 at 6:11

1 Answer 1

2

Simply include the "library" (PHP file defining the functions you require):

include "/path/to/library.php";
Sign up to request clarification or add additional context in comments.

3 Comments

This does not solve the issue. All I receive is a Fatal Error stating that you cannot redeclare the methods.
I am already fully aware of how to include a library using include( "/path/to/library.php") and it is ALREADY included within the init.php file. I took the time to grab the file path using echo getcwd(); within the fn_mysql.php file. Attempting to include this within the users.php file only creates more errors. This does not resolve any issues for any future individuals searching for a similar problem.
that means the file is already included before – it would seem we need to know more about the entire setup

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.