custom search page using defined page with ajax
I am trying to write a custom search page for a client using ajax with php to kind of find all containing "XXX" then selcting a part in the table will give you details below, all is going well using define_page_3.php and a custom jquery script in jscript_parts_lookup,php.
I can enter a partial part number and i lists all results in a table, however when i select the row to show the details, I am having an issue.
Code:
$theProductId = $_POST['pn'];
global $db;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['pn']) === true && empty($_POST['pn']) === false) {
$sql = 'select * from ' . TABLE_PRODUCTS . ' where products_model like :modelID:';
$sql = $db->bindVars($sql, ':modelID:', '%' . $theProductId . '%', 'string');
$result = $db->Execute($sql);
echo '<table class="prodtable" id="prodtable">';
echo '<tr class="tbhr"><th width=\'15%\'>PN:</th><th width=\'35%\'> Desc</th><th width=\'5%\'> </th></tr>';
if ($result->RecordCount() > 0) {
while (!$result->EOF) {
echo '<tr>';
echo '<td>';
echo '<p>' . $result->fields['products_model'].'</p>';
echo '</td>';
echo '<td>';
$sql = 'select * from ' . TABLE_PRODUCTS_DESCRIPTION . ' where products_id = :productID:';
$sql = $db->bindVars($sql, ':productID:', $result->fields['products_id'] , 'string');
$result1 = $db->Execute($sql);
echo $result1->fields['products_description'];
echo '</td>';
echo '</tr>';
$result->MoveNext();
}
echo '</table>';
} else {
echo 'Sorry, no record found for product number ' . $theProductId;
}
} else {
echo 'Please enter a valid part number';
}
the jscript:
Code:
$("#prodtable tr").mouseover(function(){
$(this).removeClass('nothighlighted');
$(this).addClass('highlighted');
});
$("#prodtable tr").mouseout(function(){
$(this).removeClass('highlighted');
$(this).addClass('nothighlighted');
});
Code:
var tr = $('#prodtable').find('tr');
tr.bind('click', function(event) {
var partnum = '';
tr.removeClass('highlighted');
var tds = $(this).addClass('highlighted').find('td');
var ptxt = $(this).find('p');
$.each(ptxt, function(index, item) {
partnum = partnum + (item.innerText || item.textContent);
});
$.post('../includes/languages/english/html_includes/ojl/define_page_3.php', {partnum: partnum}, function(data){
$('#part_detail').html(data);
});
});
Now when selecting a row, i get this error:
Fatal error: Call to a member function bindVars() on a non-object in /home/content/xxx/html/includes/languages/english/html_includes/ojl/define_page_3.php on line 48
which is line:
PHP Code:
$sql = $db->bindVars($sql, ':modelID:', $tvalue, 'string');
Which doesnt make any sence to me because what i gather formt the error is that is doesnt see the $db object which is clearly used in the above section of code to make the table?
Any clues? ive been on this 2 days now and its greatly holding me up.:wacko::wacko::frusty::frusty:
Thanks in advance
Jeff
Re: custom search page using defined page with ajax
For security reasons you should NEVER make calls from the browser or ajax to any .php files under the /includes/ folder.
That's why the /includes/.htaccess file specifically blocks that.
Your problem is directly because the define_page_3.php file (which is nested below the /includes/ folder) doesn't know anything about anything else in Zen Cart itself.
Your $.post call should be to /index.php?main_page=page_3
Re: custom search page using defined page with ajax
Thank you that was absolutely it, now if i can get it to stop redrawing the entire page when it posts back lol.
Re: custom search page using defined page with ajax
Well, that's gonna be complicated, since all those parts are included in the drawing of the page. You should probably put the business logic in another file. It's REALLY not intended that define-pages would be used for the purpose you're attempting.
Re: custom search page using defined page with ajax
So... where would i put that file? Tutorial?
Re: custom search page using defined page with ajax
Given that your ajax handler file can't put located under the includes folder, putting it someplace above the includes folder (ie the root, which is the same folder where your includes folder is found) would be logical.
Re: custom search page using defined page with ajax
yes, it would be logical, just didnt know if it was allowed lol.
I made some custom search forms and sideboxes, simply recreating the standard search forms and renaming them, it works great.
I found in my endeavors that under ../includes/modules/pages, i can recreate a page, and use scripting from there, since this is my overall goal, is just a modified search page with some AJAX to show certain content according to a combo box selection.