Hi I am a bit of a self taught novice coder so I may come across as not having a clue here! However my website has a php script which displays products from my database. I want the user to be able to select the type of product they are interested in and to use Ajax to refine the list on the fly. I have tried building a php script which uses a variable "q" which is assigned to the drop down box and an onChange event to trigger the Ajax but I am unsure how to make an Ajax script which will reprocess the php script and refresh the results. A: Is there a better way of doing this and perhaps just using javascript and ajax to filter the results rather than resending a variable to the phph script? B: If not then how do I get the Ajax to retrigger the script? Your help is much appreciated! Below is the PHP <?php $q=$_GET["q"]; mysql_connect("XXXXXXX", "XXXX", "XXXXX") or die (mysql_error());mysql_select_db("db268352658") or die (mysql_error()); if ($q==''){$mffresult = mysql_query ("SELECT mortgages.apr, mortgages.theindex, mortgages.longtype, mortgages.lastupdate, mortgages.details, mortgages.fee, mortgages.ltv, mortgages.monthly, mortgages.rate, mortgages.rev, mortgages.term FROM mortgages WHERE mortgages.ftb = 'y' ORDER BY mortgages.rate ASC");} else if ($q=='Buy to Let'){$mffresult = mysql_query ("SELECT mortgages.apr, mortgages.theindex, mortgages.longtype, mortgages.lastupdate, mortgages.details, mortgages.fee, mortgages.ltv, mortgages.monthly, mortgages.rate, mortgages.rev, mortgages.term FROM mortgages WHERE mortgages.btl = 'y' ORDER BY mortgages.rate ASC");} else if ($q=='Remortgages'){$mffresult = mysql_query ("SELECT mortgages.apr, mortgages.theindex, mortgages.longtype, mortgages.lastupdate, mortgages.details, mortgages.fee, mortgages.ltv, mortgages.monthly, mortgages.rate, mortgages.rev, mortgages.term FROM mortgages WHERE mortgages.rem = 'y' ORDER BY mortgages.rate ASC");} else if ($q=='House Purchase'){$mffresult = mysql_query ("SELECT mortgages.apr, mortgages.theindex, mortgages.longtype, mortgages.lastupdate, mortgages.details, mortgages.fee, mortgages.ltv, mortgages.monthly, mortgages.rate, mortgages.rev, mortgages.term FROM mortgages WHERE mortgages.pur = 'y' ORDER BY mortgages.rate ASC");};?> And heres the HTML I made div id="bestbuywrapper"><form action="" method="get" id="productselect" onChange="showResult(this.value)" > <label> <select name="select"> <option>Remortgages</option> <option>House Purchase</option> <option>First Time Buyer</option> <option>Buy to Let</option> </select> </label> </form> <div class="producttable"><div class="toprow"><div class="col1 white">Initial Rate</div><div class="col1 white">Initial Term</div><div class="col3 white">Fee</div><div class="col3 white"><a title="The percentage of the total property value that is being borrorwed">Max LTV</a></div><div class="col4 white">APR</div><div class="col4 white">More info</div><div class="col4 white">Apply</div></div><?php while($row = mysql_fetch_array( $mffresult )) { ?><div class="prodcontainer"><div class="productrow"><div class="col1 white2"><strong> <?php echo $row['rate']; ?>% <?php echo $row['longtype']; ?></strong></div><div class="col1 white2"><?php echo $row['term']; ?></div><div class="col3 white2">£<?php echo $row['fee']; ?></div><div class="col3 white2"><?php echo $row['ltv']; ?>% Ltv</div><div class="col4 white2"><?php echo $row['apr']; ?>%</div><div class="col4 white2"><div class="arrow"></div></div><div class="col4 white2"><a href="https://www.xxxxxxx/product-enquiry.php?recordID=<?php echo $row['theindex']; ?>" class="style2bold">Enquire »</a></div></div></div><div class="prodcontainer"><div class="productinfo"><h4 class="producth">Additional information - <strong>last checked: <?php echo $row['lastupdate']; ?></strong></h4><p>Reversion rate currently <strong><?php echo $row['rev']; ?>%</strong>. Monthly payment based on £100K borrowing = <strong>£<?php echo $row['monthly']; ?></strong></p><p><?php echo $row['details']; ?></p></div></div><?php }?></div></div></div></div> </div>
I don't think the question's very clear. Are you asking how to write the ajax script, or how to call it? <form action="" method="get" id="productselect" onChange="showResult(q.value)" > <select name="q"> ... </select> <form> <div id="searchresults"> <script> showResult(default.value) </script> </div> Code (markup): if the "showResult" function would populate the innerHTML property of the "searchresults" div with a table generated by a script on the server via ajax, something similar to that should work for live results updates with no page refresh. That's not all actual valid code, just a sample structure of course.
Hi, yeah question probably wasn't very clear (a downside to teaching yourself is you miss the appropriate terminology). I figured out how to get the Ajax to work and call a set of results from the sql database since I posted this and how to get it to display the results in innerHTML etc. The one problem I still have is now the PHP side. For some reason the IF statement doesn't seem to work and the results I get are therefore just an unfiltered list of all table results. Do you know if that is because an Array cannot accept multiple queries and if so how do I sort that out? Thanks Again
An array can accept multiple results and/or queries, a query is just a string. I'm not sure I understand that part of the question. <?php $q=$_GET["q"]; mysql_connect("XXXXXXX", "XXXX", "XXXXX") or die (mysql_error());mysql_select_db("db268352658") or die (mysql_error()); if ($q=='') {$mffresult = mysql_query ("SELECT mortgages.apr, mortgages.theindex, mortgages.longtype, mortgages.lastupdate, mortgages.details, mortgages.fee, mortgages.ltv, mortgages.monthly, mortgages.rate, mortgages.rev, mortgages.term FROM mortgages WHERE mortgages.ftb = 'y' ORDER BY mortgages.rate ASC");} else if ($q=='Buy to Let') {$mffresult = mysql_query ("SELECT mortgages.apr, mortgages.theindex, mortgages.longtype, mortgages.lastupdate, mortgages.details, mortgages.fee, mortgages.ltv, mortgages.monthly, mortgages.rate, mortgages.rev, mortgages.term FROM mortgages WHERE mortgages.btl = 'y' ORDER BY mortgages.rate ASC");} else if ($q=='Remortgages') {$mffresult = mysql_query ("SELECT mortgages.apr, mortgages.theindex, mortgages.longtype, mortgages.lastupdate, mortgages.details, mortgages.fee, mortgages.ltv, mortgages.monthly, mortgages.rate, mortgages.rev, mortgages.term FROM mortgages WHERE mortgages.rem = 'y' ORDER BY mortgages.rate ASC");} else if ($q=='House Purchase') {$mffresult = mysql_query ("SELECT mortgages.apr, mortgages.theindex, mortgages.longtype, mortgages.lastupdate, mortgages.details, mortgages.fee, mortgages.ltv, mortgages.monthly, mortgages.rate, mortgages.rev, mortgages.term FROM mortgages WHERE mortgages.pur = 'y' ORDER BY mortgages.rate ASC");} ;?> Code (markup): Well just scanning over the php above the query looks semantically valid. There's an extra semicolon at the end of the code, and 'elseif' is a single word. If that didn't cause errors it would make your whole script into a single 'if...else' statement. I also note that in the sample code the select is named 'select', not 'q'. If that's in the live code the same way then $_GET["q"] == '' every time. The GET variable will have the same name as the name of the control who's value you want to retrieve. If you have covered all of those bases, and still have the problem, it might be int he data, too. Is it possible that your records have a 'y' that is suited to more than one of your where clauses? Basically, if one of those doesn't get it, then more info would be required to come up with any reasonable answer.