0

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'.

How do I pass the correct table name to my query that will populate my SELECT list? Thanks!!

(Pastebin =>form code)

Chris
  • 1,667
  • 6
  • 34
  • 52
  • 6
    Your understanding of how HTTP works is flawed: http://stackoverflow.com/questions/7016701/creating-jquery-ajax-requests-to-a-php-function/7016795#7016795 – NullUserException Sep 02 '11 at 23:20
  • 2
    `$sql="select distinct filename from "."
    ";` isn't going to result in anything like usable SQL. What is that `
    ` supposed to end up being? A table name? Just code the table name.
    – Marvo Sep 02 '11 at 23:24
  • Yes, it is supposed to be the table name that is returned by the Ajax script. – Chris Sep 02 '11 at 23:26

2 Answers2

2

m8, ajax is mainly used for user experience and populating a select list is so easy mode that it shouldnt be bothered with ajax in the first place!

You should use ajax if you want to use some methods on user submitted data and create an illusion of seamless data exchange between the server and the client, and based on the return results render a corresponding view or an element of the view.

unless you load every element of the view with ajax, you should populate your html with php from the start!

<select name="DBFilename" id="DBFilename" size="whatever since style belongs to css"> 
<option selected="selected">Select Filename</option> 
<?php 
$sql="SELECT DISTINCT filename from 'wherever'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>  
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename'];?>
</option> 
<?php } ?> 
</select>
omasdg
  • 116
  • 3
  • Thanks for your help. I need to rework the whole flow of the app. Thanks for taking the time. – Chris Sep 06 '11 at 17:23
0

Create a separate php script which returns a list of select options -> values depending on the table name given to it. You must remember to protect against sql injection. Then use ajax to retrieve the list and insert it into the select.

Ivo
  • 5,378
  • 2
  • 18
  • 18