I am just starting out with PHP. I have resorted to doing some custom PHP programming because the plug-INs for a CMS I am using need a little customization.

The question I have is very simple and I am sure PHP programmers can answer this instantly. I have successfully declared a global PHP variable. All I need to know how to do for now is how to declare a button or a hyperlink or an input form element such that it can change the assignment of this variable.

    if i understand the question:

    if(url\post\get =='foo'){
    $var='x';
    }

      [Merged into this thread -- MOD]

      I am just starting out with PHP. I have resorted to doing some custom PHP programming because the plug-INs for a CMS I am using need a little customization.

      The question I have is very simple and I am sure PHP programmers can answer this instantly. I have successfully declared a global PHP variable. All I need to know how to do for now is how to declare a button or a hyperlink or an input form element such that it can change the assignment of this variable.

      I have tried the following suggestion. In the html code I have this:

      <form method="post">
        <input type="text" name="textbox1"/>
        <input type="submit" value="Click me!"/>
      </form>
       

      Then I am supposed to be able to grab the input of the textbox like by using the following php code:

        <?php
          $variable = $_POST['textbox1'];
        ?>
       

      But where would I put this php code? I have tried to do this prior to the form declaration and I have tried after the form decoaration in the php file. Both do not seem to work because prior to the page being submitted, it does not exist inthe context of the page.

        In the html code I have this:

        <form method="post">
          <input type="text" name="textbox1"/>
          <input type="submit" value="Click me!"/>
        </form>
         

        Then I am supposed to be able to grab the input of the textbox like by using the following php code:

          <?php
            $variable = $_POST['textbox1'];
          ?>
         

        But where would I put this php code? I have tried to do this prior to the form declaration and I have tried after the form decoaration in the php file. Both do not seem to work because prior to the page being submitted, it does not exist in the context of the page.

          you'd have to put it on the page the form goes to (form action).

          Also, make sure that the $POST variable is set using isset(). See dagon's post on this as well:

          if (isset($_POST['textbox1']){
          $var = $_POST['textbox1'];
          }
          

            Also remember that PHP itself will not actually remember the values when changing from page to page. The var will ONLY be changed for that specific request. That means that if you open the form again, the $_POST data will be gone and the $var will have the default value again.

              Desdinova;10973930 wrote:

              Also remember that PHP itself will not actually remember the values when changing from page to page. The var will ONLY be changed for that specific request. That means that if you open the form again, the $_POST data will be gone and the $var will have the default value again.

              And if your looking for something more semi-permanent, try using sessions ($_SESSION) - note each page will need a call to session_start(); before you output anything on any page using sessions.

                Write a Reply...