kristo5747 Posted September 2, 2011 Share Posted September 2, 2011 **Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.** Greetings. I am coding a basic form that uses a SELECT list I populate from my database. However, my user needs the form to be dynamic and wants to be able to select either `MasterTable1` or `MasterTable2` or `MasterTable3`... Instead of hardcoding the table name for the database query that populates the SELECT list, I attempted to implement a basic Ajax action (used example from w3schools)...and that's when I lost my sanity... I can output `<div id='txtHint'></div>` in my page and it shows the correct table name that was picked. But how do I pass the correct table name to my query that will populate my SELECT list??? I tried <select name="DBFilename" id="DBFilename" size="0"> <option value="">Select Filename</option> <?php $sql="select distinct filename from "."<div id='txtHint'></div>"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)){ ?> <option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option> <?php } ?> </select> But to no avail. This is confusing since I can do this... ... document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","gettable.php?q="+str,true); xmlhttp.send(); } </script></head> <body><form><select name="SrcTbl" id="SrcTbl" size="0" onchange="showTable(this.value)"> <option value="">Select Data Table</option> <option value=""> </option> <option value="MasterTable1">up to 31 days old</option> <option value="MasterTable2">62 days old</option> </select> </form><br /><div id="txtHint"><select name="tabList"><option></option></select> </div> </body></html> And the name of my table will be displayed in the SELECT list 'tablist'. That <div> supposed to end up being a table name that is returned by the Ajax script. How do I pass the correct table name to my query that will populate my SELECT list? Thanks!! Form code in pastebin.com Quote Link to comment Share on other sites More sharing options...
sunfighter Posted September 3, 2011 Share Posted September 3, 2011 Can I pass Ajax var result to PHP code? Yes! That's exacly what AJAX is for. In the second code group you published you have this line xmlhttp.open("GET","gettable.php?q="+str,true); Your running the gettable.php script and passing str to it. The str is where you tell the PHP script (and it has to be a php script) to select either `MasterTable1` or `MasterTable2` or `MasterTable3`; ie, var str = 'MasterTable2'; Then your php script gets that info: IF (isset($_GET['q'])){ opens and connects to your database <<<<=== of course this is not php but english explanation of it $thing = $_GET['q']; then query => SELECT * FROM $thing; make your <select><option> thing here and echo it } or better use heredoc. Put this in your php script and see what is sent back down and printed in your <div id='txtHint'></div> echo <<<EOD <table width="300px"><tr> <td align="left"> <input type="button" name="read_message" value="Read" onclick= "message_read()" /> </td> <td align="center"> <input type="button" name="action_message" value="Delete" onclick= "message_delete()" /> </td> <td align="right"> <input type="button" name="reply_message" value="Reply" onclick="message_reply()" /> </td> </tr> </table> EOD; Quote Link to comment Share on other sites More sharing options...
kristo5747 Posted September 6, 2011 Author Share Posted September 6, 2011 Thanks for your help. I think i need to rework the whole flow of the app. Thanks for taking the time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.