|
jpadie (TechnicalUser) |
3 Sep 11 4:17 |
remember that php web applications (and most web apps) are stateless. you cannot interrupt output to the browser to wait for user input. http://en.wikipedia.org/wiki/Stateless_serverdata sent to the server by ajax is accessible either in the $_POST or $_GET superglobal depending on the method employed. i have cleaned up your code a bit and added some ajax functionality for the display of the final results. hopefully you can see how things work and thus take it on from here. your code is weak on security and exposed to normal attack vectors so don't deploy this anywhere vulnerable and when you're ready read up on security generally. CODE <?phpini_set('error_reporting', true );error_reporting(E_ALL ^ E_NOTICE );define(MYDIR , $_SERVER['DOCUMENT_ROOT']."/filedepot");$host= 'localhost';$user = 'vatroot';$pwd = 'ork';$db = 'dev';$con = mysql_connect($host, $user, $pwd);if (!$con) { die('Could not connect: '.mysql_error());}mysql_select_db($db, $con) or die(mysql_error());function check_search() { $errors = $results = null ; if (isset($_POST['submit'])) { if (! empty($_POST['DBFilename'])) { $results = doFileSearch(); } } else $errors = "Please select an option before you hit SUBMIT."; return Array($results, $errors);}function doFileSearch($table=null , $DBFilename=null ) { $result = '; if ( empty($DBFilename) && empty($_POST['DBFilename']))return null ; if ( empty($table) && empty($_POST['SrcTbl'])) return null ; $DBFilename = isAjax() ? $DBFilename : $_POST['DBFilename']; $table = isAjax() ? $table : $_POST['SrcTbl']; //add some escaping. this is imperfect $table = '`' . $table . '`'; $mydir = MYDIR ; $dir = opendir($mydir); //must escape $DBFilename to prevent sql injection $sql = "select filename from $table where filename='".mysql_real_escape_string($DBFilename)."'"; $getfilename = mysql_query($sql); if(!$getfilename) return false ; while ($row = mysql_fetch_array($getfilename)) { $filename = $row['filename']; $result .= '<tr><td><a href="'.basename($mydir).'/'.$filename.'" target="_blank">'.$filename.'</a></td></tr>'; } if ($result) { $result = "<table><tbody><tr><td>Search Results.</td></tr> $result</table>"; } return $result;}//create a function to retrieve the files namesfunction getFileNames($table){ $options = array(); $sql="select distinct filename from $table"; $result = mysql_query($sql); if(!$result) return false ; while ($row = mysql_fetch_array($result)): $options[] = array( 'text'=>$row['filename'], 'value'=>$row['filename']); endwhile; return $options; }function isAjax(){ return isset($_REQUEST['a']) && $_REQUEST['a'] == 1;}if(isset($_REQUEST['action'])): if (isAjax()): switch ($_REQUEST['action']): case 'getFileNames': $table = trim($_GET['q']); $options = getFileNames($table); if($options === false ): $return = array('result'=>false ); else: $return = array('result'=>true , 'options'=>$options); endif; echo json_encode($return); die; //should always expressly kill the process with ajax calls break; case 'getFileList':
|
jpadie (TechnicalUser) |
3 Sep 11 6:29 |
oops. slight error there. replace the relevant bit of code with the following CODE //remove all but the first option from the element for (var i=elem.length-1; i >= 1; i--){ elem.options[i] = null; } for more help on the javascript nature of your query, check out the javascript forum. |
|
Thanks for your help. I need to rework the whole flow of the app. Thanks for taking the time. |
|
|
 |
|
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close