ktsirig Posted April 25, 2015 Share Posted April 25, 2015 Hi, I have an input form for a user to submit some input and then this input is processed by a Perl script, which is called through the PHP index page... My problem is that this external script takes usually more than 2-3 minutes so there is a risk of the page to hang. How can I modify my PHP page to stay in index.php, but periodically refresh it so, when the perl command is done, it then outputs the results. Here is the index page (the refresh I wrote on top did not work. <?php $max_seqs = 1000; $page = $_SERVER['PHP_SELF']; $sec = "10"; header("Refresh: $sec; url=$page"); ?> <html> <head> <style> body { font-family: arial; color: #003965; font-size: 14px; } textarea { width: 620px; height: 120px; font-family: courier; } .whole { width: 1000px; border-collapse: collapse; margin-top:20px; margin-bottom:20px; text-align: center; background-color: #e8e8e8; font-size: 12px; } .browse_cell { background:#d8dada; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; padding: 3; border-spacing: 2; text-align: center; font-weight: bold; color:#424d5d; } .browse_cell_change1 { background:#BFBFF6; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; padding: 2; } .browse_cell_change2 { background:#D9D9D9; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; padding: 2; border-spacing:4px; } .headcell { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 16px; font-weight:bold; line-height: 15px; text-align: center; color: #000144; } </style> </head> <body> <?php if( isset ( $_POST['sequence'] )) #run part { function readSeqs($text) { if( !preg_match( '/^>/m', $text ) ) return readSeqs( ">\n$text" ); $seqs = array(); $text = preg_replace( '/[\n\r]+/', "\n", $text ); preg_match_all( '/^>([^\n]*)\n([^>]*)/ms', $text, $matches ); foreach( $matches[1] as $k=>$name1 ) { $seqs_number++; $seq = $matches[2][$k]; $name1 = trim($name1); $name=substr($name1, 0, 50); #keep only the first 40 characters (if the name is longer) $name=preg_replace('/\s+/', ' ', $name); $name=preg_replace('/[\|\s\/\\\%\*&\#\@\(\)\[\]\$\^\!]/', '_', $name); $seq = preg_replace( '/\s+/', '', $seq ); $seqs[] = array( 'name'=>$name, 'seq'=>$seq ); } return $seqs; } $uniq_number = uniqid(); $input_folder = './tmp_files/myfolder_'.$uniq_number; mkdir($input_folder); $input_file = $input_folder.'/'.'input_seqs'; $seqs = readSeqs($_POST['sequence']); $run_mode = $_POST['mode']; $signal=''; $phmms=''; if($_POST[sp_pred] == 'on') { $signal = $_POST['sp_pred']; } else { $signal = 'off'; } if($_POST['pfams'] == 'on') { $phmms = $_POST['pfams']; } else { $phmms = 'off'; } $seqs_number = count($seqs); $prior_info1 = $_POST['prior']; if (preg_match('/Example/', $prior_info1)) { $prior_info='--'; $prior_info_final='NO_INFO'; } else { $prior_info=$prior_info1; $prior_info_final=str_replace(';', 'A', $prior_info); } if( $seqs_number == 0 ) { echo "<em>You did not submit any sequences.</em>"; } elseif( $seqs_number > $max_seqs ) { echo "<em>You can input no more than $max_seqs sequences at a time.</em>"; } elseif ( $seqs_number > 1 and $seqs_number <= $max_seqs ) { $input = ''; foreach($seqs as $v) #create input file for run { $input.= ">".$v['name']."\n".$v['seq']."\n"; } #write each FASTA seq in input file file_put_contents( $input_file, $input ); #(if we will allow >1 proteins) #echo "perl Whole_Program.pl $input_folder $seqs_number $prior_info_final $run_mode $signal $phmms<br>"; exec ("perl Whole_Program.pl $input_folder $seqs_number $prior_info_final $run_mode $signal $phmms", $res_total, $ret); if($ret==0) { $text_file = $input_file.'.txt_res'; $zip_file = 'tmp_files/'.$uniq_number.'.tgz'; echo "<table class=\"whole\">\n"; echo "<tbody>\n"; echo "<tr><td class=\"headcell\" align=\"right\" colspan=\"7\"><br>Results<br><br></td></tr>\n"; echo "<tr>\n"; echo "<td width=\"14%\" class=\"browse_cell\">Entry</td>\n". "<td width=\"12%\" class=\"browse_cell\">Length</td>\n". "<td width=\"14%\" class=\"browse_cell\">Signal peptide</td>\n". "<td width=\"14%\" class=\"browse_cell\">β-score</td>\n". "<td width=\"38%\" class=\"browse_cell\">OMPdb family</td>\n". "<td width=\"14%\" class=\"browse_cell\">#TM</td>\n". "<td width=\"12%\" class=\"browse_cell\">Topology image</td>\n"; echo "</tr>\n"; foreach ($res_total as $line_entry) { $counter++; if ($counter % 2) {$colour_class="browse_cell_change1";} else {$colour_class="browse_cell_change2";} $all_data = preg_split("/\t/", $line_entry); $id=$all_data[0]; $seq_len=$all_data[1]; $signal=$all_data[2]; $score=$all_data[3]; $family_name=$all_data[4]; $family_number=$families_array[$family_name]; $tm_num=$all_data[5]; if($tm_num==0) {$tm_num='--';} $topo_img_link=$all_data[6]; echo "<tr>\n"; echo "<td width=\"14%\" align=\"left\" class=\"".$colour_class."\">".$id."</td>\n". "<td width=\"12%\" class=\"".$colour_class."\">".$seq_len."</td>\n". "<td width=\"14%\" class=\"".$colour_class."\">".$signal."</td>\n". "<td width=\"14%\" class=\"".$colour_class."\">".$score."</td>\n". "<td width=\"38%\" align=\"left\" class=\"".$colour_class."\">"; if($family_name == "--") { echo "$family_name</td>\n"; } else { echo "<a target=\"_blank\" href=\"http://www.ompdb.org/OMPdb/view_family.php?fam_id=".$family_number."\">". $family_name."</a></td>\n"; } echo "<td width=\"14%\" class=\"".$colour_class."\">".$tm_num."</td>\n"; if($topo_img_link=='--') { echo "<td width=\"12%\" class=\"".$colour_class."\">--</td>\n"; } else { echo "<td width=\"12%\" class=\"".$colour_class."\"><a target=\"_blank\" href=\"".$topo_img_link."\"> Download</a></td>\n"; } echo "</tr>\n"; } echo "</tbody></table>\n"; } else { echo "<br /><br />Program error!"; } } echo "<br /><a target=\"_blank\" href=\"".$text_file."\">Download</a> results in plain text format\n"; echo "<br /><a target=\"_blank\" href=\"".$zip_file."\">Download</a> all files related to this submission as a tarball\n"; echo "<br /><br /><a href=\"index.php\">Run again</a><br />"; } else #index initial page { echo "Please paste up one or more ". " - formatted sequences in the textarea below: <br /><br />\n"; echo "<form method=post name=\"seq_form\">". "<textarea name=\"sequence\"></textarea> <br /><br /> Select mode: <input type=\"radio\" name=\"mode\" value=\"1\" checked /> MSA-version (1 sequence) <input type=\"radio\" name=\"mode\" value=\"2\" /> Single-sequence version (up to $max_seqs sequences) <br /><br /> Run signal-peptide prediction using <a target=\"_blank\" href=\"http://www.compgen.org/tools/PRED-TAT/\">PRED-TAT</a>: <input type=\"checkbox\" name=\"sp_pred\" checked> <br /><br /> Use library in prediction: <input type=\"checkbox\" name=\"pfams\" checked> <br /><br /> Prior information (constrained prediction): <input type=\"text\" name=\"prior\" size=\"33\" style=\"color:#888;\" value=\"Example: I:1-15;M:20-25;O:30-30\" onfocus=\"inputFocus(this)\" onblur=\"inputBlur(this)\" /> <br /><br /> <input type=\"submit\" value=\"Run prediction\" /> <input type=\"reset\" value=\"Clear fields\" /> <input type=\"button\" value=\"Generate sample input\" onclick=\"example_fill();\"> </form>"; echo "<br /><hr>"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/295868-need-help-in-refreshing-a-page-periodically-for-a-slow-loading-results-page/ Share on other sites More sharing options...
nik_jain Posted April 26, 2015 Share Posted April 26, 2015 Wouldn't AJAX / javascript be the way to handle it ? Send periodic aja requests to your server, to check if the perl script has done its thing. if it has return a response via AJAX and show the results. Quote Link to comment https://forums.phpfreaks.com/topic/295868-need-help-in-refreshing-a-page-periodically-for-a-slow-loading-results-page/#findComment-1509984 Share on other sites More sharing options...
ktsirig Posted April 26, 2015 Author Share Posted April 26, 2015 Hi, thanks for your answer... My problem is that I have absolutely no idea how to do this, can you point me to some place? Quote Link to comment https://forums.phpfreaks.com/topic/295868-need-help-in-refreshing-a-page-periodically-for-a-slow-loading-results-page/#findComment-1510001 Share on other sites More sharing options...
nik_jain Posted April 26, 2015 Share Posted April 26, 2015 If you are not handy with Javascript it will require some learning...Basically you set code to send requests with javascript(ajax) every 1 second or soThe requests (simple GET queries ) are sent to the server, site.com/ajaxHandler.php?check=1Where the script ajaxhandler will check for whatever thing you need to checkand echo out a response, which the javascript code will see and perform an action based on that.For syntax , just google.. 'ajax beginner' or something like that Quote Link to comment https://forums.phpfreaks.com/topic/295868-need-help-in-refreshing-a-page-periodically-for-a-slow-loading-results-page/#findComment-1510017 Share on other sites More sharing options...
7blake Posted April 27, 2015 Share Posted April 27, 2015 (edited) basic ajax/php relationship below. You could set it up, and then have it on a timed loop function, if you need it to run during a specific set of time. It just needs some tweaking depending on the sort of data you are sending, but the overall base structure is there. $.ajax({ method: "POST", // Or GET url: 'path', // URL to your server page data: {parameter1: value1}, // parameters -- can be multiple. pareameter1 is what you target with $_POST['parameter1'] in PHP - value1 is your data. success: function(data) { // PHP's echoed data // Do something with the echoed data } //php if(isset($_POST['param1])){ $retrievedData = $_POST['parameter1']; echo $retrievedData; } }); Edited April 27, 2015 by 7blake Quote Link to comment https://forums.phpfreaks.com/topic/295868-need-help-in-refreshing-a-page-periodically-for-a-slow-loading-results-page/#findComment-1510060 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.