Having a problem bringing contents of a variable from one page to another.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • orfiyus
    New Member
    • Jul 2007
    • 36

    Having a problem bringing contents of a variable from one page to another.

    Hi

    I am writing a php script that contains a bunch of links. Each link when clicked redirects to the same page however where the clicked link once was is now a textfield. The user then types into the text field once the user has entered input the page redirects to itself again this time where the text field was is a link again. The link is wutever the user typed into the previous text field. I am doing this by using $_REQUEST[....] to get wutever the user types in. So far Ive only been able to get one of these to work. The problem I am having is when the user clicks another link after the process is completed the previous process's variable is erased and the default link is outputted. Can someone help me with this problemF? I am posting a piece of my script.

    Thanks

    [PHP]$outer_link1 = "<a href=\"?Expand= outer_link1\" >outer_link1</a>";
    if ($_REQUEST['Expand'] == "outer_link 1")
    {
    $TextField1 = "<input type=text name=outer1 onChange='submi t();' ";
    echo("<td>$Text Field1</td>");



    }

    else
    {

    $outer_link1_co ntents= $_REQUEST['outer1'];

    if ($outer_link1_c ontents)
    {
    $outer_link1a = "<a href=\"?Expand= outer_link1\" >$outer_link1_c ontents</a>";
    echo("<td>$oute r_link1a</td>");

    }
    else
    {
    echo("<td>$oute r_link1</td>");
    }


    }

    $outer_link2 = "<a href=\"?Expand2 =outer_link2\" >outer_link2</a>";



    if ($_REQUEST['Expand2'] == "outer_link 2")
    {
    $outer_link1_co ntents= $_REQUEST['outer1'];
    if ($outer_link1_c ontents)
    {
    $outer_link1a = "<a href=\"?Expand= outer_link1\" >$outer_link1_c ontents</a>";
    echo("<td>$oute r_link1a</td>");


    }
    else
    {
    echo("<td>$oute r_link1</td>");
    }
    $TextField2 = "<input type=text name=outer2 onChange='submi t();' ";
    echo("<td>$Text Field2</td>");



    }


    else
    {


    $outer_link2_co ntents= $_GET['outer2'];
    if ($outer_link2_c ontents)
    {
    $outer_link2a = "<a href=\"?Expand2 =outer_link2\" >$outer_link2_c ontents</a>";
    echo("$outer_li nk2a");
    }
    else
    {
    echo("<td>$oute r_link2</td>");

    }

    } [/PHP]
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Hi,

    Each time a user clicks a link they get a text box, completing the text box is supposed to change the text of the link. this works but only for one link at a time.

    I think this is bacuase when you pist to a page it gets the page definition from the server and unless you change that file then you will always have this problem. what is happening is you are posting a vraibale to the target page and it's using it just fine. then with the second one it';s getting the original page as the file has not been updated.

    To do what you want there are a couple of options open to you.

    1. A database
    Define the page based on a list of links in a table, then when a user makes a change to this the field in the table is updated and the page is redefined using the latest data from the database.

    2. AJAX
    When the user clicks a link simply display the text box without going to another page, then one ce the text is entered you can display the link again. This would be the same for each link and as it would never go and get the whole page from the server you would get teh desired results.

    The deciding factor is really how permanent the change needs to be and should differnt users be able to set these things independantly of one another.

    My prefered solution would be a combo of the database and AJAX.

    Decide what exactly this system needs to do and for who and then pick a solution. Read up on the technology and post back here with any questions - I'm happy to try to help.

    Cheers
    nathj

    Comment

    • orfiyus
      New Member
      • Jul 2007
      • 36

      #3
      Originally posted by nathj
      Hi,

      Each time a user clicks a link they get a text box, completing the text box is supposed to change the text of the link. this works but only for one link at a time.

      I think this is bacuase when you pist to a page it gets the page definition from the server and unless you change that file then you will always have this problem. what is happening is you are posting a vraibale to the target page and it's using it just fine. then with the second one it';s getting the original page as the file has not been updated.

      To do what you want there are a couple of options open to you.

      1. A database
      Define the page based on a list of links in a table, then when a user makes a change to this the field in the table is updated and the page is redefined using the latest data from the database.

      2. AJAX
      When the user clicks a link simply display the text box without going to another page, then one ce the text is entered you can display the link again. This would be the same for each link and as it would never go and get the whole page from the server you would get teh desired results.

      The deciding factor is really how permanent the change needs to be and should differnt users be able to set these things independantly of one another.

      My prefered solution would be a combo of the database and AJAX.

      Decide what exactly this system needs to do and for who and then pick a solution. Read up on the technology and post back here with any questions - I'm happy to try to help.

      Cheers
      nathj
      ^^^^^^^^^^^^^^^ ^^^^^^^
      Unfortunatly Im limited in how I can work on this. I dont have direct access to the database. And Im not allowed to create my own tables. So thats out.

      As for AJAX...I had this working in javascript. The problem I had with that tho was that php wasnt able to access wutever I put in the javascript textfield. So thats also out.

      Is there any other way to do this or am I trying the impossible at this point?

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Originally posted by orfiyus
        ^^^^^^^^^^^^^^^ ^^^^^^^
        Unfortunatly Im limited in how I can work on this. I dont have direct access to the database. And Im not allowed to create my own tables. So thats out.

        As for AJAX...I had this working in javascript. The problem I had with that tho was that php wasnt able to access wutever I put in the javascript textfield. So thats also out.

        Is there any other way to do this or am I trying the impossible at this point?
        Okay so those in charge are making life difficult for you. I think though that AJAX can be your solution as the text fields are still just html text fileds. when the link is clicked simply load the text fields then on the submit of the text field call a JS function with the ID of the text field the JS can get the value and using the XMLHTTP Request object it can be passed to php page with the value on the end of the query string to be retreived by $_GET[].

        I think this is the best bet you've got.

        Cheers
        nathj

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          I must admit I haven't looked very carefully at the code (since it is not complete), but have you thought of using the $_SESSION array as a way of saving these links?

          Ronald

          Comment

          • orfiyus
            New Member
            • Jul 2007
            • 36

            #6
            Originally posted by ronverdonk
            I must admit I haven't looked very carefully at the code (since it is not complete), but have you thought of using the $_SESSION array as a way of saving these links?

            Ronald
            I have no idea how to use SESSION. You got a link?

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              The $_SESSION array allows you to exchange data between pages. Works like an associative array. See these tutorials:




              Ronald

              Comment

              • orfiyus
                New Member
                • Jul 2007
                • 36

                #8
                okay how do I use a session to retrieve the contents of a text field?

                Would this work?

                [PHP]

                $TextField1 = "<input type=text name=outer1 onChange='submi t();' >";

                echo("$TextFiel d1");

                $_SESSION['outer1'] = $_REQUEST['outer1'];

                echo("$_SESSION['outer1'] ");
                [/PHP]

                Comment

                • orfiyus
                  New Member
                  • Jul 2007
                  • 36

                  #9
                  ^^^^^^^^^^^^^^^ ^

                  Yea that code piece dont work. The only thing I cant figure out is how to retrieve the user input in the text field and then store it in a session variable.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Originally posted by orfiyus
                    ^^^^^^^^^^^^^^^ ^

                    Yea that code piece dont work. The only thing I cant figure out is how to retrieve the user input in the text field and then store it in a session variable.
                    When you want to use the $_SESSION array you MUST start the session at the very beginning of your script, like
                    [php]<?php
                    session_start() ;
                    .. rest of code
                    [/php]
                    In your code sample that would be:
                    [php]
                    <?php
                    session_start() ;
                    $TextField1 = "<input type=text name=outer1 onChange='submi t();' >";

                    echo("$TextFiel d1");

                    $_SESSION['outer1'] = $_REQUEST['outer1'];

                    echo $_SESSION['outer1'];[/php]
                    Observe that the echo statement has been corrected because the original one would not show any output.

                    Ronald

                    Comment

                    • orfiyus
                      New Member
                      • Jul 2007
                      • 36

                      #11
                      okay I got the code to work. I still have the same problem that the value in the variable is being erased once I click a different link. Wut else can I do? Is there sumthing Im missing?

                      [PHP]

                      /* ############### ############ UNEXPAND ############### ############### ### */


                      $unexpand = "<a href=\"?Expand= Unexpand\" >Unexpand</a>";
                      echo("$unexpand ");
                      session_start() ;
                      $outer_link1 = "<a href=\"?Expand= outer1\" >-0-</a>";
                      $TextField1 = "<input type=text name=outer1 onChange='submi t();'>";
                      $_SESSION['outer1_value'] = $_REQUEST['outer1'];

                      if ($_REQUEST['Expand'] == "outer1")
                      {
                      //$textfield1_val ue = $_GET['outer1']
                      //value=$textfiel d1_value

                      echo("<td>$Text Field1</td>");


                      }
                      elseif ( ($_REQUEST['Expand'] == "Unexpand")
                      || ($_REQUEST['Expand'] !== "Unexpand") || ($_REQUEST['Expand'] !== "outer1"))
                      {


                      echo("should be a changed value");
                      if (!($_SESSION['outer1_value']))
                      {
                      $outer_link1 = "<a href=\"?Expand= outer1\" >-0-</a>";
                      echo("<td>$oute r_link1</td>");
                      }

                      echo "<td>sessio n value here<a href=\"?Expand= outer1\" >" . $_SESSION['outer1_value'] . "</td></a>";

                      }

                      else
                      {
                      echo("Do nothing");
                      }
                      [/PHP]

                      Comment

                      • ronverdonk
                        Recognized Expert Specialist
                        • Jul 2006
                        • 4259

                        #12
                        If you have a session_start() at the top of each script, you do the blanking out of the $_SESSION variable yourself.

                        Ronald

                        Comment

                        • orfiyus
                          New Member
                          • Jul 2007
                          • 36

                          #13
                          Originally posted by ronverdonk
                          If you have a session_start() at the top of each script, you do the blanking out of the $_SESSION variable yourself.

                          Ronald
                          where should I put it then?

                          Comment

                          • ronverdonk
                            Recognized Expert Specialist
                            • Jul 2006
                            • 4259

                            #14
                            Originally posted by orfiyus
                            where should I put it then?
                            '
                            Sorry for my not-too-clear post. What I mean is that, when you correctly issue the session_start() at the top of each script, then no data in that $_SESSION array can be lost between pages.
                            So a blank entry in the array is your own doing.

                            Sorry for the misunderstandin g.

                            Ronald

                            Comment

                            • orfiyus
                              New Member
                              • Jul 2007
                              • 36

                              #15
                              Originally posted by ronverdonk
                              '
                              Sorry for my not-too-clear post. What I mean is that, when you correctly issue the session_start() at the top of each script, then no data in that $_SESSION array can be lost between pages.
                              So a blank entry in the array is your own doing.

                              Sorry for the misunderstandin g.

                              Ronald
                              Its pretty much right at the top of the page. Im putting in the first few lines. Because the previous author of this script is using session variables for various db related processes. Should I destroy his old session before I start my own?
                              Theres the first few lines of my script
                              [PHP]
                              <?php
                              require("/scenarios/callable.php");
                              $db_conn = connect_db();
                              session_start() ;

                              .....

                              [/PHP]

                              Comment

                              Working...