To help learn PHP, AJAX, and JQuery I'm developing a little recipe management system. On the main page recipesystem.php
are two DIVs:
DIV1 has input fields for entering a new recipe's name and category, and a button to create the new record in a backend PostgreSQL database table.
That same button also calls the JQuery .load(url, data)
with the url
variable pointing to a PHP file that loads additional HTML input fields and buttons into DIV2 (for adding things like ingredients, etc, to the new recipe in a second step to the workflow).
The problem I've run into is that none of the DIV2 content can see the objects on the page or DIV1.
This article helped me understand better the difference between changing the innerHTML and manipulating the DOM, but I'm still not sure how to add new HTML content to DIV2 while preserving its ability to access objects in DIV1 and the rest of the original page.
Here's the code from DIV1 that populates the database with the new recipe and then populates DIV2 (#divRightColumn
) with HTML from the call to phpRecipeDetails.php
:
// Create the initial recipe namd and category
$( "#createrecipebutton" ).click(function() {
var varRecipeName = $('input:text[name=recipename]').val();
var varRecipeCategory = $('input:text[name=recipecategory]').val();
insertNewRecipe(varRecipeName,varRecipeCategory);
var url = 'phpRecipeDetails.php';
var data = 'recipename='+ varRecipeName;
$('#divRightColumn').html('<h4>Loading...</h4>').load(url, data);
return false;
})
The contents of phpRecipeDetails.php
has gotten quite complicated because it has to return back to #divRightColumn
not only HTML structure but also the Javascript that the structure leverages. When I tried having those Javascript functions in the DIV1 or the page, the DIV2 content would ignore them. So clearly I'm heading down the wrong path with this.
How do I develop DIV2 so that it has new content that can access functions and objects that are in DIV1 and the original page?
Thanks for your help!