Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Making PHP insert code in the same page with the html code...

  1. #11
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    well, I'm still learning a lot of mysql, so I'm not sure why it appears that a new, blank record is being inserted. It seems to me that *nothing* new should show up. The reason that your form is no longer inputting new records like you want is that the code for doing it:
    PHP Code:
    <?php

      
    include('dbconnect.php');

      
    $name $_POST[name];
      
    $age $_POST[age];

      
    $data "insert into simplerecord values(null,'$name','$age')";
       
      
    mysql_query($data) or die(mysql_error());

    ?>
    is still in "simple.php", which is no longer the file your form uses (when you had no "action" defined, the server defaulted to using the same page as the form). "simple2.php" doesn't reference any POST data (from the form) at all. When you specify that the form uses "simple2.php", you have to put all the code you want the form to use in "simple2.php" also.

    You will end up with conflicting variables, so you'll have to rename them when you rewrite "simple2.php".

  2. The Following User Says Thank You to traq For This Useful Post:

    heavensgate15 (06-11-2009)

  3. #12
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    hmmm, it is troublesome if I rewrite my php code from the simple.php to simple2.php because it is redundant.... is there a way that you can go to another page without the submit button? what i mean is something like this:

    PHP Code:
    <?php

      
    include('dbconnect.php');

      
    $data "insert into table_name values(null,'name','age')";

      
    $query mysql_query($data);

      if(!
    $query)
      {
         die(
    mysql_error());
      }
      else
      {
        
          
    // php code/built-in function to go to another page

      
    }


    ?>
    is there something like that in php? a function or a code to go to another page? What I think is that, I will separate my codes; one for getting the data through forms and put it in a file named form.html. Then one for inserting the data from form.html and put it in file named insert.php (the code for this is supposedly something like above), and another file named display.php for displaying the data from the database. Or, is there another way in writing a code in a way that after the user inputs the value, the value will be inserted to the database after the user clicks the submit button, then when the user clicked the submit button, it will go to another page?
    Last edited by Snookerman; 06-10-2009 at 07:56 AM.

  4. #13
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    okay...:
    this
    PHP Code:
    simple.php

    <?php

      
    include('dbconnect.php');

      
    $name $_POST[name];
      
    $age $_POST[age];

      
    $data "insert into simplerecord values(null,'$name','$age')";
       
      
    mysql_query($data) or die(mysql_error());

    ?>
    does the query and inserts a record. However, because the form action is now "simple2.php", it does not take data from the form. The record it inserts is blank.

    Adding that code (or, more specifically, *moving* that code) to simple2.php would not be redundant because the code no longer works properly where it is anyway. You will end up with extra blank records every time someone visits the page, whether they fill out the form or not.

    Am I correct that you want to display the simplerecord table after someone submits their record to it? Try something like this (all on one page):
    PHP Code:

    simple.php

    <?php

    if(!isset($_POST['name'])){
    echo 
    '
    <html>
      <head>
         <title> simple form </title>
      </head>
      <body>
          <form action="'
    .$_SERVER['PHP_SELF'].'" method="POST">
               <p><strong>Name:</strong><br>
                     <input type="text" name="name"></p>
               <p><strong>Age:</strong><br>
                     <input type="text" maxlength="2" name="age"></p>
               <p><input type="submit" value="send"></p>
           </form>
        </body>
    </html>'
    ;
    }else{
      include(
    'dbconnect.php');

      
    $name $_POST[name];
      
    $age $_POST[age];

      
    $data "insert into simplerecord values(null,'$name','$age')";
       
      
    mysql_query($data) or die(mysql_error());

      
    $data2 "select * from simplerecord";

      
    $query2 mysql_query($data2);
      
    $num2 mysql_num_rows($query2);

      
    $x 0;
      while(
    $x $num2)
      {
         
    $name2 mysql_result($query2,$x,"name");
         
    $ID2 mysql_result($query2,$x,"ID");
         
    $age2 mysql_result($query2,$x,"age");

         
    printf("Name: %s<br>",$name2);
         
    printf("ID: %d<br>",$ID2);
         
    printf("Age: %d<br><br>",$age2);
         
         
    $x++;
      }
    }
    ?>
    OR, you could remove the if - else statement and keep the code on two pages. only the form would go on "simple.php" (with action="simple2.php"), everything else would go on "simple2.php".

  5. The Following User Says Thank You to traq For This Useful Post:

    heavensgate15 (06-11-2009)

  6. #14
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    I think, I get your point... when the user visit the page for the first time, the code that is executed is the if statement right? because the submit button is not yet clicked. So, when the user clicked the submit button, the data that he/she inputs will be process in the else statement because the if condition is no longer true.

    But I want that, after the user clicks the button, it will go to another page. So probably, the code for that will be put in the else part I think? But I don't know what code or functions will be used so that it will go to another page....

  7. #15
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    you can break it up into two pages if you prefer.
    PHP Code:
    ####simple.php####

    <html>
      <head>
         <title> simple form </title>
      </head>
      <body>
          <form action="simple2.php" method="POST">
               <p><strong>Name:</strong><br>
                     <input type="text" name="name"></p>
               <p><strong>Age:</strong><br>
                     <input type="text" maxlength="2" name="age"></p>
               <p><input type="submit" value="send"></p>
           </form>
        </body>
    </html>

    ####simple2.php####
    <?php

      
    include('dbconnect.php');

      
    $name $_POST[name];
      
    $age $_POST[age];

      
    $data "insert into simplerecord values(null,'$name','$age')";
       
      
    mysql_query($data) or die(mysql_error());

      
    $data2 "select * from simplerecord";

      
    $query2 mysql_query($data2);
      
    $num2 mysql_num_rows($query2);

      
    $x 0;
      while(
    $x $num2)
      {
         
    $name2 mysql_result($query2,$x,"name");
         
    $ID2 mysql_result($query2,$x,"ID");
         
    $age2 mysql_result($query2,$x,"age");

         
    printf("Name: %s<br>",$name2);
         
    printf("ID: %d<br>",$ID2);
         
    printf("Age: %d<br><br>",$age2);
         
         
    $x++;
      }
    ?>
    if you just want a redirect, I think the simplest way would be via javascript. Put it at the end of your php code:
    PHP Code:
    <?php

    echo '
       <script type="text/javascript">
          window.location = "http://www.yourdomain.com/otherpage.php"
       </script>'
    ;

    ?>

  8. The Following User Says Thank You to traq For This Useful Post:

    heavensgate15 (06-11-2009)

  9. #16
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    Thank you for your code... I got an idea on how to do this now hehe, and it works... Thanks a million....

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •