469,362 Members | 2,441 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

how to show this php code submit result at the same page

hello
I found this code somewhere and tried to create online dictionary with it. I managed to insert the code in to my template index.php. But after the user click submit button the result shows in other blank self created page something like index.php?act=sm. I need the results to show on the same page or somewhere with in the same template. I' ve tried every thing I can. Just I couldn't.how can I do that?

I add
Expand|Select|Wrap|Line Numbers
  1. action="<?php echo $PHP_SELF; ?>
but did not work out.

Thank you in advance
Best Regard's

this is the code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo("<a href=admin.php>ACP</a><br>");
  3.  
  4. if(isset($_GET['act']))
  5. {
  6. $do=$_GET['act'];
  7. switch($do)
  8. {
  9.  case 'form': show_form();break;
  10.  case 'sm' : submited();break;
  11.  default : show_form; break;
  12. }
  13.  
  14. }
  15. else
  16. {
  17. show_form();
  18. }
  19.  
  20. function show_form()
  21. {
  22. ?>
  23. <form name=frm method=POST action="index.php?act=sm">
  24.  
  25. Word : <input type=text name="word"> <input type=submit name="sbm" value="Submit">
  26. </form>
  27. <?
  28.  
  29. }
  30. function submited()
  31. {
  32.  
  33. require("dbconn.inc");
  34. //if(isset($_POST['sbm']))
  35. //{
  36.  $word=$_POST['word'];
  37.  
  38.  $sql="select * from words where eng like '".$word."'";
  39.  //echo($sql);
  40.  $result=mysql_query($sql,$link);
  41.  if(@mysql_num_rows($result)!=0)
  42.  {
  43.   while($rows=mysql_fetch_array($result))
  44.   {
  45.    $vie=$rows["vie"];
  46.   }
  47.   echo("Word<b>$word</b> in vietnamese is : <b>$vie</b><br>");
  48. ?>
  49. <form name=frm method=POST action="index.php?act=sm">
  50.  
  51. Word : <input type=text name="word"> <input type="submit" name="sbm" value="Submit">
  52. </form>
  53. <?
  54.  }
  55.  else
  56.   echo("Don't know that word !! My database is not update!");
  57. //}
  58. mysql_close($link);
  59.  
  60. }
  61. ?>
Mar 27 '11 #1
4 22425
dgreenhouse
250 Expert 100+
Although I didn't test this, this may work:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7. <body>
  8. <?php
  9. echo("<a href=admin.php>ACP</a><br>");
  10.  
  11. $script = $_SERVER['PHP_SELF'];
  12.  
  13. if(isset($_POST['sbm']) && isset($_POST['word']) ) {
  14.   submited();
  15. } else {
  16.   show_form();
  17. }
  18.  
  19. function show_form() {
  20. ?>
  21.   <form name=frm method=POST action="<?php echo $script ?>">
  22.     Word : <input type=text name="word"><br />
  23.     <input type=submit name="sbm" value="Submit">
  24.   </form>
  25. <?php
  26. }
  27.  
  28. function submited() {
  29.   require("dbconn.inc");
  30.   $word=$_POST['word'];
  31.   $sql="select * from words where eng like '".mysql_real_escape_string($word)."'";
  32.   $result=mysql_query($sql,$link); // I assume $link is defined in dbconn.inc
  33.  
  34.   if(@mysql_num_rows($result)!=0) {
  35.     $rows=mysql_fetch_array($result)
  36.     $vie=$rows["vie"];
  37.     echo("Word<b>$word</b> in vietnamese is : <b>$vie</b><br>");
  38.   } else {
  39.     echo("Don't know that word !! My database is not update!");
  40.   }
  41.  
  42.   // The connection will close automatically since
  43.   // this is near the end of the script.
  44.   mysql_close($link);
  45.  
  46.   show_form();
  47. }    
  48. ?>
  49. </body>
  50. </html>
  51.  
Mar 28 '11 #2
hello dgreenhouse
Thank you for replying.
It shows parse error in line 36
Expand|Select|Wrap|Line Numbers
  1. $rows=mysql_fetch_array($result)
  2.     $vie=$rows["vie"];
When I add while loop here it brings me to the same page after clicking submit button but the results are not showing.

best regard's
Mar 28 '11 #3
dgreenhouse
250 Expert 100+
$rows=mysql_fetch_array($result) is not terminated.

Should be:
$rows=mysql_fetch_array($result);
Mar 28 '11 #4
Thanks a million,You were very helpful Dgreanhouse.
It works!!
Mar 28 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Grey | last post: by
1 post views Thread by Andrew | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.