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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dagmawi michael
    New Member
    • Mar 2011
    • 5

    #1

    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=s m. 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
    Code:
    action="<?php echo $PHP_SELF; ?>
    but did not work out.

    Thank you in advance
    Best Regard's

    this is the code
    Code:
    <?php
    echo("<a href=admin.php>ACP</a><br>");
    
    if(isset($_GET['act']))
    {
    $do=$_GET['act'];
    switch($do)
    {
     case 'form': show_form();break;
     case 'sm' : submited();break;
     default : show_form; break;
    }
    
    }
    else
    {
    show_form();
    }
    
    function show_form()
    {
    ?>
    <form name=frm method=POST action="index.php?act=sm">
    
    Word : <input type=text name="word"> <input type=submit name="sbm" value="Submit">
    </form>
    <?
    
    }
    function submited()
    {
    
    require("dbconn.inc");
    //if(isset($_POST['sbm']))
    //{
     $word=$_POST['word'];
     
     $sql="select * from words where eng like '".$word."'";
     //echo($sql);
     $result=mysql_query($sql,$link);
     if(@mysql_num_rows($result)!=0)
     {
      while($rows=mysql_fetch_array($result))
      {
       $vie=$rows["vie"];
      }
      echo("Word<b>$word</b> in vietnamese is : <b>$vie</b><br>");
    ?>
    <form name=frm method=POST action="index.php?act=sm">
    
    Word : <input type=text name="word"> <input type="submit" name="sbm" value="Submit">
    </form>
    <?
     }
     else
      echo("Don't know that word !! My database is not update!");
    //}
    mysql_close($link);
    
    }
    ?>
    Last edited by dagmawi michael; Mar 27 '11, 10:32 PM. Reason: Title edited (misspell)
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    Although I didn't test this, this may work:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <?php
    echo("<a href=admin.php>ACP</a><br>");
    
    $script = $_SERVER['PHP_SELF'];
    
    if(isset($_POST['sbm']) && isset($_POST['word']) ) {
      submited();
    } else {
      show_form();
    }
     
    function show_form() {
    ?>
      <form name=frm method=POST action="<?php echo $script ?>">
        Word : <input type=text name="word"><br />
        <input type=submit name="sbm" value="Submit">
      </form>
    <?php
    }
    
    function submited() {
      require("dbconn.inc");
      $word=$_POST['word'];
      $sql="select * from words where eng like '".mysql_real_escape_string($word)."'";
      $result=mysql_query($sql,$link); // I assume $link is defined in dbconn.inc
    
      if(@mysql_num_rows($result)!=0) {
        $rows=mysql_fetch_array($result)
        $vie=$rows["vie"];
        echo("Word<b>$word</b> in vietnamese is : <b>$vie</b><br>");
      } else {
        echo("Don't know that word !! My database is not update!");
      }
    
      // The connection will close automatically since
      // this is near the end of the script.
      mysql_close($link);
    
      show_form();
    }	
    ?>
    </body>
    </html>

    Comment

    • dagmawi michael
      New Member
      • Mar 2011
      • 5

      #3
      hello dgreenhouse
      Thank you for replying.
      It shows parse error in line 36
      Code:
      $rows=mysql_fetch_array($result)
          $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

      Comment

      • dgreenhouse
        Recognized Expert Contributor
        • May 2008
        • 250

        #4
        $rows=mysql_fet ch_array($resul t) is not terminated.

        Should be:
        $rows=mysql_fet ch_array($resul t);

        Comment

        • dagmawi michael
          New Member
          • Mar 2011
          • 5

          #5
          Thanks a million,You were very helpful Dgreanhouse.
          It works!!

          Comment

          Working...