in reply to Call Perl script from Javascript

Hi guys, Thanks alot for your ideas. This is my php script:
<?php $username = "root"; $password = "root"; $hostname = "127.0.0.1:3306"; $dbname = "AI"; $word = $_GET['word']; $sql = "SELECT crps_word, crps_probability FROM arabic_cor +pus WHERE crps_word like '$word%' ORDER BY crps_probability LIMIT 10 +"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); // Set Encoding Set to utf8 mysql_query("SET character_set_results=utf8", $dbhandle); mb_language('uni'); mb_internal_encoding('UTF-8'); mysql_select_db($argDB, $dbhandle); mysql_query("set names 'utf8'",$dbhandle); //select a database to work with $selected = mysql_select_db($dbname,$dbhandle) or die("Error selecting database"); //execute the SQL query and return records $result = mysql_query($sql, $dbhandle); if($result) { //fetch tha data from the database while ($row = mysql_fetch_array($result)) { $word = $row{'crps_word'}; $prob = $row{'crps_probability'}; $str = $prob."----".$word; echo "<option value = '$word'></option>"; } } //close the connection mysql_close($dbhandle); ?>

Replies are listed 'Best First'.
Re^2: Call Perl script from Javascript
by tangent (Parson) on Mar 19, 2014 at 22:55 UTC
    To see what you are getting back from your script add an alert() to your javascript:
    $.get("suggest.pl", {word: $(this).val()}, function(data){ alert(data); $("#myWord").empty(); $("#myWord").html(data); });
    You say earlier that you already have the script that deals with the db, so instead of the echo in PHP you just need print in Perl, something like:
    print $q->header; while (my $row = $sth->fetchrow_hashref) { my $word = $row->{'crps_word'}; my $prob = $row->{'crps_probability'}; my $str = $prob."----".$word; print qq|<option value="$word">$str</option>|; }