Order Processing Page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LugNut29
    New Member
    • Jul 2013
    • 16

    Order Processing Page

    Hi All,

    I have an assignment to complete and I just can't get it and I was hoping someone could help me out. What I need to do is:

    Call the session_start function.

    Code:
    <?php
    session_start();
    ?>
    Detect whether or not the name session variable has a value; if so, display name and age as text; if not, display the name and age input fields.

    Code:
    First name: <input type="text" name="fname" />  Age: <input type="text" name="age" />  <input type="submit" name="submit" value="Submit" />
    
    <?php
    if (isset($_POST['submit'])) { 
    $_session['fname'] = $_POST['fname'];
    $_session['age'] = $_POST['age'];
    } 
    ?>
    How can have the text fields replaced with the users Name and age after they type it in and submit the form? So, when they go back to the that page, they will already see their name and age printed out in place of the text fields. I also upload an image of what it is suppose to look like.

    Thanks in advance for your help,
    T
    Attached Files
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    #2
    Your fields and button must be placed inside the form tag. (<form method="post"> fields here!.. </form>)

    Try to run this codes. . .
    Code:
    <?php 
    session_start();
    ob_start();
    ?>
    <!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>Sample</title>
    </head>
    
    <body>
    
    <form method="post">
    	First name: <input type="text" name="fname" />  Age: <input type="text" name="age" />  <input type="submit" name="submit" value="Submit" />
    </form>
    
    </body>
    </html>
    <?php 
    	if(isset($_POST['submit']))
    	{
    		$_SESSION['fname']=$_POST['fname'];
    		$_SESSION['age']=$_POST['age'];
    		//header('Location: page_to_go.php');
    		echo $_SESSION['fname'].' '.$_SESSION['age'];
    	}
    ?>

    Comment

    • LugNut29
      New Member
      • Jul 2013
      • 16

      #3
      Hi Exequiel,

      Thank you for the reply. I tried the code you suggested and it still doesn't work. Here's what I have:

      Code:
      <?php 
      session_start();
      ob_start();
      ?>
      
      <?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
      <!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 name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org" />
      <link rel="stylesheet" type="text/css" href="css/styles.css" />
      <script type="text/javascript" src="js/catalog.js"></script>
      <title>Scion tC Catalog</title>
      </head>
      <body>
      <a name="Top" id="Top"></a>
      
      <form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" 
      
      method="post">First name: <input type="text" name="fname" />  Age: <input type="text" name="age" />  <input 
      
      type="submit" name="submit" value="Submit" /> <input type="button" onclick="formReset()" value="Reset form" />
      <br />
      <table class="table1">
      <tr>
      <th>Select</th>
      </tr>
      <tr>
      <td class="radio button"><input type="radio" name="scion" id="r1" value="You selected the 2010 Scion tC RS" /><br 
      
      /></td>
      </tr>
      <tr>
      <td class="radio button"><input type="radio" name="scion" id="r2" value="You selected the 2012 Scion tC RS" /><br 
      
      /></td>
      </tr>
      <tr>
      <td class="radio button"><input type="radio" name="scion" id="r3" value="You selected the 2013 Scion tC RS" /><br 
      
      /></td>
      </tr>
      <tr>
      <td class="radio button"><input type="radio" name="scion" id="r4" value="You selected the 2014 Scion tC" /><br 
      
      /></td>
      </tr>
      </table></form>
      
      </body>
      </html>
      
       <?php 
          if(isset($_POST['submit']))
          {
              $_SESSION['fname']=$_POST['fname'];
              $_SESSION['age']=$_POST['age'];
              //header('Location: catalog.php');
              echo $_SESSION['fname'].' '.$_SESSION['age'];
          }
      ?>

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        You need to first check whether or not the name variable in the session is set and then choose what to output depending on if it's set or not.

        Comment

        • LugNut29
          New Member
          • Jul 2013
          • 16

          #5
          If there's a session started, I need to have their name (fname) and age (age) displayed where the input text fields should be. If the session is not started, then the whole form should display with text fields for the user's input. Would it look something like this:

          Code:
          <?php 
              if(isset($_POST['submit']))
              {
                  $_SESSION['fname']=$_POST['fname'];
                  $_SESSION['age']=$_POST['age'];
                  //header('Location: catalog.php');
                  echo $_SESSION['fname'].' '.$_SESSION['age'];
              }
              if (isset($_POST['fname']) && ($_POST['age']))
                       $session = $_POST['fname'] && $_POST['age'];
               session_start();
               $_SESSION['fname'] && $_SESSION['age'] = $session;
          ?>
          I'm sorry if this isn't close. I'm not a very good programmer and I'm still trying to learn.

          Thanks,
          T

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            It needs to look more like this
            Code:
            <?php 
            if(isset($_POST['submit'])) {
               set your session variables
               output your values
            } else {
               output your form
            }
            ?>
            Last edited by Rabbit; Aug 15 '13, 04:06 PM.

            Comment

            • LugNut29
              New Member
              • Jul 2013
              • 16

              #7
              I tried this:

              Code:
              <form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" method="post">
              <?php 
                  if(isset($_POST['submit'])) {
              
              $_SESSION['fname']=$_POST['fname'];
              $_SESSION['age']=$_POST['age'];
                   
                  if(isset($_SESSION['fname'])) {
                    //header('Location: catalog.php');
                    echo $_SESSION['fname'].' '.$_SESSION['age'];}
                  else {
              echo 'First name: <input type="text" name="fname" />  Age: <input type="text" name="age" />  '; }
              }
              ?>
              <input type="submit" value="Submit" /> <input type="button" onclick="formReset()" value="Reset form" />
              It's not displaying the text fields or the output. Am I getting close?

              T

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                That structure doesn't follow my example.

                Comment

                • LugNut29
                  New Member
                  • Jul 2013
                  • 16

                  #9
                  Ok, so what am I doing wrong? Please remember I trying to learn this. I very green when it comes to programming. Should it look like this:

                  Code:
                  <?php 
                      if(isset($_POST['submit'])) {
                  
                  $_SESSION['fname']=$_POST['fname'];
                  $_SESSION['age']=$_POST['age'];
                       
                      if(isset($_SESSION['fname'])) {
                        //header('Location: catalog.php');
                        echo $_SESSION['fname'].' '.$_SESSION['age'];}
                      else {
                  echo '<form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" method="post"> 
                  
                  First name: <input type="text" name="fname" />  Age: <input type="text" name="age" /> <input type="submit" value="Submit" /> 
                  
                  <input type="button" onclick="formReset()" value="Reset form" />'; }
                  }
                  ?>

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    Compare your structure to my structure.

                    To repeat, my structure is this:
                    Code:
                    <?php 
                    if(isset($_POST['submit'])) {
                       set your session variables
                       output your values
                    } else {
                       output your form
                    }
                    ?>
                    Your structure is this:
                    Code:
                    <?php 
                    if(isset($_POST['submit'])) {
                       set your session variables
                    
                       if(isset($_SESSION['fname'])) {
                          output your values
                       } else {
                          output your form
                       }
                    }
                    ?>
                    As your can see, your structure is not the same as mine.

                    Comment

                    • LugNut29
                      New Member
                      • Jul 2013
                      • 16

                      #11
                      So from what you said above, it should look like this?

                      Code:
                      <?php 
                          if(isset($_POST['submit'])) {
                      $_SESSION['fname']=$_POST['fname'];
                      $_SESSION['age']=$_POST['age'];
                      //header('Location: catalog.php');
                      echo $_SESSION['fname'].' '.$_SESSION['age'];}
                          else {
                      <form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" method="post"> First name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" value="Submit" /> <input type="button" onclick="formReset()" value="Reset form" />';
                      }
                      ?>

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        That looks about right.

                        But I see another error, you didn't give your submit button a name. So it's not going to be set when it goes to check it.

                        Comment

                        • LugNut29
                          New Member
                          • Jul 2013
                          • 16

                          #13
                          Ok, I just tried the code above and it still doesn't show the name and age in replace of the text fields after the I go back to my catalog page. This is what I have:

                          Code:
                          <?php 
                              if(isset($_POST['submit'])) {
                          $_SESSION['fname']=$_POST['fname'];
                          $_SESSION['age']=$_POST['age'];
                          echo $_SESSION['fname'].' '.$_SESSION['age'];}
                              else {
                          echo '<form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" method="post">';
                          echo 'Name: <input type="text" name="fname" />'; 
                          echo 'Age: <input type="text" name="age" />';
                          echo '<input type="submit" name="submit" value="Submit" />';
                          echo '<input type="button" onclick="formReset()" value="Reset form" />';
                          }
                          ?>
                          I really do appreciate your help with this.
                          T

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            Works fine for me.

                            Assuming of course that everything else is correct. I am assuming all the code before and after this block of code is correct and that this code is in fact in the thankyou.php page. By which I mean you're submitting the page to itself.

                            And if by going back, you mean clicking the back button on your browser, that doesn't refresh the page.
                            Last edited by Rabbit; Aug 15 '13, 10:18 PM.

                            Comment

                            • Exequiel
                              Contributor
                              • Jul 2012
                              • 288

                              #15
                              @LugNut29
                              Its not still working because of the action on your form, action="thankyo u.php". Your code that you replied to me is right, only the thankyou.php is the problem. If you try to remove the action="thankyo u.php" on your form the code is working fine. but if you really want to display the session on thankyou.php try my code. I've created thankyou.php. can you post your thakyou.php ?
                              Code:
                              <?php 
                              session_start();
                              ob_start();
                              ?>
                              
                              <?php 
                              	echo $_SESSION['fname'].' '.$_SESSION['age'];
                              ?>

                              Comment

                              Working...