Jump to content

Converting W3S's ASP script to PHP


aja

Recommended Posts

Am trying the W3S's AJAX tute, which uses an ASP script, and says "It could easily be rewritten in PHP, or some other server language." I'd like to finish this, but my PHP script's still not working, although it's probably 90% functional. Can anyone help get it up and running?Here's the source page:http://www.w3schools.com/ajax/ajax_source.aspThe script is called "gethint.asp." and is in the 3rd CODE section, under the Head "The AJAX Server Page."Here's my PHP version so far:

<?php$a = array();$a[1] = "Anna";$a[2] = "Brittany";$a[3] = "Cinderella";$a[4] = "Diana";$a[5] = "Eva";$a[6] = "Fiona";$a[7] = "Gunda";$a[8] = "Hege";$a[9] = "Inga";$a[10] = "Johanna";$a[11] = "Kitty";$a[12] = "Linda";$a[13] = "Nina";$a[14] = "Ophelia";$a[15] = "Petunia";$a[16] = "Amanda";$a[17] = "Raquel";$a[18] = "Cindy";$a[19] = "Doris";$a[20] = "Eve";$a[21] = "Evita";$a[22] = "Sunniva";$a[23] = "Tove";$a[24] = "Unni";$a[25] = "Violet";$a[26] = "Liza";$a[27] = "Elizabeth";$a[28] = "Ellen";$a[29] = "Wenche";$a[30] = "Vicky";$q = strtoupper($_GET("q"));if ( strlen($q) > 0) {  $hint = "";  for ($i = 1; $i <= 30; $i++) {    if ($q = strtoupper( substr( a[$i], 1, strlen($q))) {      if ($hint = "" ) {        $hint = $a[i];      }      else {        $hint = $hint . " , " . $a[i];      }    }  }}}if ($hint = "") {  echo "no suggestion";}else {  echo $hint;}?>

Some probable problems are 1. I'm unsure if ECHO's the right way to output the response.2. Number of braces -- didn't check these yet, may be off.Any help would be appreciated, thx.

Link to comment
Share on other sites

2 hours later I figured this out haha. I've changed a few things but the main error was it was missing a ). Although I don't understand the application of this, here is the fixed version:

<?php$a = array();$a[1] = "Anna";$a[2] = "Brittany";$a[3] = "Cinderella";$a[4] = "Diana";$a[5] = "Eva";$a[6] = "Fiona";$a[7] = "Gunda";$a[8] = "Hege";$a[9] = "Inga";$a[10] = "Johanna";$a[11] = "Kitty";$a[12] = "Linda";$a[13] = "Nina";$a[14] = "Ophelia";$a[15] = "Petunia";$a[16] = "Amanda";$a[17] = "Raquel";$a[18] = "Cindy";$a[19] = "Doris";$a[20] = "Eve";$a[21] = "Evita";$a[22] = "Sunniva";$a[23] = "Tove";$a[24] = "Unni";$a[25] = "Violet";$a[26] = "Liza";$a[27] = "Elizabeth";$a[28] = "Ellen";$a[29] = "Wenche";$a[30] = "Vicky";$q = strtoupper($_GET['q']);$qlen = strlen($q);if ($qlen > 0) {     $hint = "";     for ($i = 1; $i <= 30; $i++) {          if ($q == substr($a[$i], 1, $qlen)) {               if ($hint == "" ) {$hint = $a[$i];}               else{$hint = $hint." , ".$a[$i];}          }     }}if ($hint == "") {echo "no suggestion";}     else {echo "$hint";};?>

Link to comment
Share on other sites

thanks, calvin182...

2 hours later...
sorry if you spent that much time on it. The reason I posted it was hoping someone could just dash it off. I guess the tutorial should have said something more like:
"It could easily be rewritten in PHP, or some other server language if you have a lot of patience, don't mind changing hundreds of niggly syntax details, and have a spare hour or two to work out all the nits! (?) :) "
...Although I don't understand the application of this...
If you check the link, it was part of a tutorial on AJAX at the w3schools.com site. Sometimes it seems a little odd to me that they post so many of the server-side examples in ASP when you consider that more people and servers use PHP than ASP, but I guess that's just what the authors use most themselves.Anyhow, your version works a lot better than the one I posted, thanks. It still has a couple of minor errors that keep it from working like the ASP version, but it inspired me to go back and finish debugging my own. :)
Link to comment
Share on other sites

no problem. yea it took me a while but im learning and it was one of those things where you get started and it just bugs you if you dont figure it out, so like i said, no problem.yea i didnt really check over the link to see the application for it, that probably would have helped me fix some of the errors a little easier, oh well, im glad i could have helped :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...