I'm trying to access my function from class using if condition where my function that contains SQL Statement what I want to do is access it from another php file using if condition if it's existing it will return true. Codes below.
main.html
< script type = "text/javascript" src = "https://code.jquery.com/jquery-1.12.3.min.js" > < /script> <script> $(document).on("click", "#addBooksId", function() { $.get("addBooks.html", function(data) { $( "#myform" ).empty(); $('#myform').append(data); / / Receive with value alert('This is add books section'); }); }); < /script> body { padding: 0; margin: 0; overflow-y: scroll; font-family: Arial; font-size: 15px; } #container { background-color: #707070; } #menu { width: 1250px; margin: 0 auto; text-align: left; } #menu ul { list-style-type: none; /*Remove bullets*/ padding: 0; margin: 0; position: relative; } #menu ul li { display: inline-block; } #menu ul li:hover { text-decoration: none; color: black; } #menu ul li a, visited { color: #CCC; /*Color of text*/ display: block; /*Takes up the full width available*/ padding: 15px; text-decoration: none; } #menu ul li a:hover { color: #CCC; text-decoration: none; } #menu ul li:hover ul { display: block; } #menu ul ul li { display: block; } #menu ul ul { display: none; position: absolute; background-color: #707070; min-width: 140px; /*Width when hover*/ } #menu ul ul li a:hover /*Color of text when hover*/ { color: #099; } <div id="container"> <div id="menu"> <ul> <li> <a href="#">Manage Books</a> <ul> <div id="addBooksId"> <li><a href="#">Add Books</a> </li> </div> </ul> </li> </ul> </div> </div> <div id="myform"> <p></p> </div> This is my home page where the user can choose an option. After clicking the option in drop down menu. It will redirect me to the addBooks.html where the user will fill up the following textfield.
addBooks.html
<style type="text/css"> .addBooks { clear: both; width: 800px; margin: 0 auto; margin-top: 10%; } .addBooks label { float: left; width: 150px; text-align: right; } .addBooks input { text-align: left; margin-left: 100px; } .addBooks select { text-align: left; margin-left: 100px; } </style> <div id="container"> <div class="addBooks"> <h1>Add Books</h1> <hr> <form method="post" action="../process/bookProcess.php"> <fieldset> <div class="form-field"> <label for="name">Book Name:</label> <input type="text" id="name" name="bookName" required> </div> <br /> <div class="form-field"> <label for="edition">Edition:</label> <input type="text" id="edition" name="bookEdition" required> </div> <br /> <div class="form-field"> <label for="pages">Pages:</label> <input type="text" id="pages" name="bookPages" required> </div> <br /> <div class="form-field"> <label for="language">Language:</label> <input type="text" id="language" name="bookLanguage" required> </div> <div class="form-field"> <input type="submit" id="submitBooks" value="Submit"> </div> </fieldset> </form> </div> <!--- end of addBooks ---> </div> <!--- end of container ---> connection.php
<?php //PHP CONSTANTS DEFINE ('host', 'localhost'); DEFINE ('username', 'root'); DEFINE ('password', ''); DEFINE ('database', 'librarysystem'); function getConnection() { $myDB = mysqli_connect(host,username,password,database); if (!$myDB) { trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error()); } else { return $myDB; } } ?> I created a connection so I will just call it from every file that I want to use. Here in my booksCRUD.php I created a protected string so I can access it anywhere. I already use this method in my login. So this is the second time I'll use this in my INSERT statement.
bookCRUD.php
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); class booksCRUD{ private $accessMyDB; public function __construct(){ $this->accessMyDB = getConnection(); } public function createBook($dbBookName,$dbEdition,$dbPages,$dbLanguage){ $statement = $this->accessMyDB->prepare("INSERT INTO bookmodule (bookName, bookEdition, bookPages, bookLanguage) VALUES (?, ?, ?, ?)"); $statement->bind_param("ssss", $dbBookName,$dbEdition,$dbPages,$dbLanguage); if($statement->execute()){ $statement->close(); return true; }else{ echo "Failed: " . $this->accessMyDB->error; return false; } } } ?> bookProcess.php
<?php session_start(); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require_once('../connection/connection.php'); require_once('../crud/bookCRUD.php'); if(isset($_POST['submitBooks'])){ $dbBookName = $_POST['bookName']; $dbEdition = $_POST['bookEdition']; $dbPages = $_POST['bookPages']; $dbLanguage = $_POST['bookLanguage']; if(!empty($dbBookName) && !empty($dbEdition) && !empty($dbPages) && !empty($dbLanguage)){ $obj = new booksCRUD(); if(method_exists($obj,'createBook')){ if($obj->createBook($dbBookName,$dbEdition,$dbPages,$dbLanguage)){ echo "Successful!"; }else{ echo "error"; } }else{ // method doesn't exist } }else{ echo "all fields are required"; } } ?> I created a object in my if condition but I cannot access my function correct me if I'm doing it wrong.