SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Using form variables within the same page

    Good day,

    I have found many information about how to pass form variables from onbe webpago to another, but how can I use these variables within the same webpage where the for is?

    Let's say I have a form and two variables:
    <form action="target_webpage" method="post">
    <p>
    Nombre del alumno: <input type="text" class="lists_input" name="Student_Name" /><br>
    </p>
    <p>
    Nombre del apoderado: <input type="text" class="lists_input" name="Parent_Name" /><br>
    </p>
    </form>

    How can I use these two variables here (within the same webpage) to load them into a MySQL table?

    Thanks a lot!!!

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You use the same code that you would if you were passing it to another file, there isn't anything different when using the one file compared to two.

  3. #3
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot.

    Could you please give me an example showing how to get the values from the form and then load them into the database, all within the same webpage?

  4. #4
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    I think this is what he's looking for. An example:

    PHP Code:
    <?php

    require_once('includes/database_functions.php');
    require_once(
    'includes/location_functions.php');

    if (
    $_SERVER['REQUEST_METHOD'] == 'POST')
    {
    $sname $_POST['Student_Name'];
    $pname $_POST['Parent_Name'];
    $success database_insert(sprintf("insert into registrations (sname, pname) values ('%s','%s')"$sname$pname));
    if (
    $success == true)
    redirect_to('Success.php');
    // if first display, or error, the html below will show.
    // on error, text boxes will have previously entered values.
    }
    ?>
    <html>
    <head>
    <title>My Page</title>
    </head>
    <body>
    <form action="target_webpage" method="post">
     <p>
     Nombre del alumno: <input type="text" class="lists_input" name="Student_Name" value="<?php echo($sname)?>" /><br>
     </p>
     <p>
     Nombre del apoderado: <input type="text" class="lists_input" name="Parent_Name" value="<?php echo($pname)?>" /><br>
     </p>
     </form>
    </body>
    </html>


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
  •