Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 16

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

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

    Default Making PHP insert code in the same page with the html code...

    hey, I want to know whether it's possible to write your PHP insert code together with your HTML code where your forms are created...... what I mean is something like this:
    Code:
    <?php
    
    
        //code for inserting the data found in the forms from the html code below
    
    ?>
    
    
    <html>
       <head>
             <title> Inserting data from forms to the database</title>
       </head>
    
       <body>
    
             <form action="nextpage.php" method="POST">
                
                     //input tags
             
             </form>
    
       </body>
    </html>
    You know, I tried this one but when I checked out my database after executing this, nothing has been added to my database... But I saw some of my classmates having this kind of codes... I've already asked them, but they cannot explain to me why... I feel like, this kind of code will not work if I'm the one who's gonna write it... please help... T_T

  2. #2
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    You can only do that once the user presses the submit button, take some look in the links below:
    http://www.google.com/search?hl=en&r...in+php&spell=1
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    heavensgate15 (06-11-2009)

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

    Default

    ahw... I looked all the links that you gave... I didn't find something that could answer my question....

  5. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,621
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    if you're asking how to write both the form and the form processing code on the same page, try this:
    PHP Code:
    <?php
    if (!isset($_POST['submit'])) { //if form is not submitted,
       
    echo '<!--form html code goes here--><input type="submit" name="submit" value="submit">';  //show the form.
    } else {  //otherwise,
       //php code to process form goes here
    }
    ?>
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

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

    heavensgate15 (06-11-2009)

  7. #5
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Likewise you can do this, in a php file, when you have retrieved the data you want.

    PHP Code:
    <?
    PHP code here that creates a variable $php_variable
    ?>
    <html>
    ......
    <input type="text" value='<? echo "$php_variable"?>'><? echo "php_variable" ?>
    ......
    </html>

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

    heavensgate15 (06-11-2009)

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

    Default

    thanks for the reply.. but I want to know how my code above is executed... what will happen if my php code for inserting data from forms is placed above my html code? Is my php code for inserting data is executed then after the user clicks the submit button? or it is not? What would be the case if I put it after my html code? Is the result would be the same?
    Last edited by heavensgate15; 06-02-2009 at 01:36 PM.

  10. #7
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    It doesn't matter where you put the PHP in relation to the HTML, the PHP will always execute first.

    That is why in your HTML form you have method="POST" and action="someaction.php", this way your HTML can get the field variables that PHP can then process.

  11. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    heavensgate15 (06-11-2009)

  12. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,621
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    However, if you use the "if - else" method I posted above, then it is actually the php code that is writing the html. So, the php will choose to either write the form OR process the submission, based on whether or not the form has been submitted already - but it won't do both.

    It might be easier for you to think of the two steps not as two parts of the same script, but as two different scripts that do different parts of the job. The form and the code to process the form could easily be on separate pages.
    Last edited by traq; 06-03-2009 at 01:10 AM.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

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

    heavensgate15 (06-11-2009)

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

    Default

    hmm... I understand what you mean... But still, there are lots of question in my mind... hmm, let's put it like this... I have a simple php scripts:

    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());
    
    ?>
    
    
    <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>
    Code:
    simple2.php
    
    <?php
    
      include('dbconnect.php');
    
      $data = "select * from simplerecord";
    
      $query = mysql_query($data);
      $num = mysql_num_rows($query);
    
      $x = 0;
      while($x < $num)
      {
         $name = mysql_result($query,$x,"name");
         $ID = mysql_result($query,$x,"ID");
         $age = mysql_result($query,$x,"age");
    
         printf("Name: %s<br>",$name);
         printf("ID: %d<br>",$ID);
         printf("Age: %d<br><br>",$age);
         
         $x++;
      }
    
    ?>
    Code:
    dbconnect.php
    
    <?php
    
    $con = mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("db",$con);
    
    ?>


    let's say, these 3 scripts create an application...These are the things that I found out.... First is the simple.php... In this script, when I didn't put a value on the action attribute, the process seems to be ok, I successfully added a record (simple2.php is not yet created)... so the form tag looks like this:
    Code:
    <form action="" method="POST">
    When I created the simple2.php, the form tag in simple.php script looks like this:
    Code:
    <form action="simple2.php" method="POST">
    But this time, when I test it through my browser(filling up the name and age) and checked my database, the record that I inserted become null in my database (the attributes name and age has no value in my database but the attribute ID has a value)... Why is that?


    OUTPUT in browser when simple2.php was not yet created: // I put display code in the same page

    Name: lourence john cabaluna
    ID: 1
    Age: 18

    Attributes values in the database when simple2.php was not yet created:

    ID = 1
    name = lourence john cabaluna
    age = 18



    OUTPUT in browser when simple2.php was created:

    Name: lourence john cabaluna
    ID: 1
    Age: 18

    Name:
    ID: 2
    Age: 0

    Attributes values in the database when simple2.php was created:

    ID = 1
    name = lourence john cabaluna
    age = 18

    ID = 2
    name =
    age = 0

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

    Default

    kindly help me about this.. it's still bugging me....

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
  •