This is driving me mad. I am using jquery and ajax to run the php to update my database and that all works fine, but after the update (specifically adding records) I want to re-run the query to get all records so that the new one is displayed but nothing happens and in my error log I am getting an error regarding my class instantiation and objects (this is partly a jquery/ajax problem I suppose as well).
The main page (form shortened for ease of reading):
<?php
require_once('../../inc/core.php');
require_once('../../inc/class.faqs.php');
$objFAQ = new Faqs;
$cats = $objFAQ->get_faq_categories();
include('../../assets/layout/header.php');
?>
<div class="container">
<div class="success-notice"></div>
<div class="add-record">
<form action="admin-faq.php" method="post" id="add-faq">
<label for="question">Question</label>
<input type="text" name="question" id="question" value="">.
.
.
<input type="submit" name="do_add" value="Submit">
<button class="close-form" value="Cancel">Cancel</button>
</form>
<hr>
</div>
<div id="faq-records">
<?php include('faq-records.php'); ?>
</div>
</div>
<?php
include('../../assets/layout/footer.php');
?>
The faq-records.php file (form shortened for ease of reading - this all works fine on page load):
<?php
$faqs = $objFAQ->get_admin_faqs();
foreach ($faqs as $faq) {
?>
<div class="faq">
<p><?php echo $faq['question']; ?></p>
<div class="edit-record">
<form action="admin-faq.php" method="post" id="edit-faq">
<input type="hidden" name="id" value="<?php echo $faq['id']; ?>">
<label for="question">Question</label>
<input type="text" name="question" id="question" value="<?php echo htmlentities(stripslashes($faq['question'])); ?>">
.
.
<input type="submit" name="do_edit" value="Submit">
<button class="close-form" value="Cancel">Cancel</button>
</form>
</div>
<hr>
</div>
<?php } ?>
And the jquery / ajax (as I say this all works apart from the load function, however if I load a basic html file with a line of text that does work):
$("#add-faq").submit(function(e){
e.preventDefault();
var dataString = $("#add-faq").serialize();
var theDiv = $(this);
$.ajax({
type: "POST",
url: "admin-faq.php",
data: dataString,
success: function(data){
$('.success-notice').html('<p>'+data+'</p>');
theDiv.closest('.add-record').hide();
$('#faq-records').load('faq-records.php');
}
});
return false;
});
The errors I get are:
Undefined variable: objFAQ
And therefore
PHP Fatal error: Call to a member function get_admin_faqs() on a non-object
I don't want to have to call the include files and reinstantiate the object again within the faq-records.php file if I can help it but I am not sure if it is load function or the php that is causing the problem.
Any ideas please??