SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $PHP_SELF produces problems with submit form

    Hello, maybe someone knows this one.

    I have a form the goes to itself using $PHP_SELF, no problem. I also use that same form to send it URL variables ($_GET). My problem is that after I send the URL variables to populate ther form (so that users can update records), when I hit the submit button again, the URL variables are not cleared! Hence my submit (update) doesn't work because one of the URL variables is used in an IF statement to show the form.

    My question is, how can I clear the URL variables when I submit the form? There has to be a simple way to do this right?

    Thanks
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  2. #2
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Variables may be cleared via unset($var). So, (if I do understand what you're asking) when you send the form, unset() the variables. Note, however, that when you unset() the variables, it does not merely clear them; instead it erases them. So if you need to use them again you need to re-create them.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  3. #3
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use php's unset function after the user submits the data..

    PHP Code:
    unset ($var1$var2$var3); 
    <edit>too slow </edit>
    http://redgoals.com ... my site ... nuff said

  4. #4
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, but how do I unset them before they hit the submit button? You mean just unset them in the code right after I have populated my form, then when they hit submit they won't be there, should work righ? I'll give it a try and report.

    Thanks
    Last edited by pata; Jun 7, 2002 at 18:00.
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  5. #5
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by pata
    ... You mean just unset them in the code right after I have populated my form, then when they hit submit they won't be there, should work righ? ...
    Yep -- that should do it.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  6. #6
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, tried it, it didn't work. Here's the code I'm using:

    PHP Code:
    // form
            ?>
              <form name="form1" method="post" action="<?$PHP_SELF?>">
            Name: <input type="text" name="name" value="<?=$_GET[name]?>">
              Year: <input name="date" type="text" size="4" maxlength="4" value="<?=$_GET[date]?>">
              Special Features: 
            <input type="radio" name="features" value="yes">Yes | 
            <input type="radio" name="features" value="no"> No<br><br>
              Actors: 
      <textarea name="actors" cols="50" rows="5" wrap="VIRTUAL"><?=$_GET[actors]?></textarea>
      <?
      
    unset ($_GET[update]);
      
    ?>
      
              <input type="submit" name="updateSQL" value="Submit">
              </form>

     <?
    see anything wrong?

    When I hit submit, it stays on the same page with the same URL with the update=yes variable
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  7. #7
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try encapsulating all of your variable names in single quotes since they are strings in an array (e.g. instead of $_GET[name] use $_GET['name']). Change that on all of them. Also, change $PHP_SELF to $_SERVER['PHP_SELF'].

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  8. #8
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, change:
    <form name="form1" method="post" action="<?$PHP_SELF?>">
    to:
    <form name="form1" method="post" action="<?=$PHP_SELF?>">
    Oh no! the coots are eating my nodes!

  9. #9
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok I'll try these things, but the form works by sending to itself the way I have it right now (it just doesn't clear the variables). Is there a diference at all with using either of the 2 ways you have here?

    Mucho thanks.

    Man, you guys are geniuses, I love this forum! What did it was changing $PHP_SELF to =$PHP_SELF.

    Does anyone know why? The form obviously sends to itself with the former syntax, what difference does =$PHP_SELF make?

    Muchisimos Thanks!
    Last edited by pata; Jun 8, 2002 at 10:01.
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  10. #10
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well with just $PHP_SELF, you aren't doing anything so the form defaults to submitting to the same page, but using the short_open tags (using <? instead of <?php) if you want to print the variable, you can go <?=$PHP_SELF?> instead of eg:
    <?php echo $PHP_SELF ?> or <? echo $PHP_SELF ?>, which means the form will submit to itself, only this time without the original $_GET vars I suspect...
    Last edited by timnz; Jun 8, 2002 at 15:23.
    Oh no! the coots are eating my nodes!

  11. #11
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    since you are using $_GET, etc. you should use $_SERVER['PHP_SELF'] instead of just $PHP_SELF. also, instead of $_GET[name] etc., it should be $_GET['name'] with the quotes around the index name.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  12. #12
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thig is Dr Larry, that often when I use the quotes inside $_GET['name'], etc I get syntax errors when they're enclosed within double quotes! The manual says I shouldn't but the parser doesn't like it and every time I get an unexpected T_String or some such nonsense!

    If I understand you correctly, you're saying that I need to use $_SERVER['PHP_SELF'] because globals are off?
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  13. #13
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is what he (and I said above ) said, and that's correct. I don't know where it says to use double quotes -- but generally that is not preferred syntax.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  14. #14
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I meant, Aes, is that whenever these variables are contained within a function that uses double quotes I get an error:

    echo("Name: $row['firstName']");

    Instant error.

    Now that you mention it, I suppose I could try:

    echo("Name: " . $row['firstName']);

    right? But it's not always possible with other functions.
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  15. #15
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That should not throw out an error. Typically I would use it like so:
    PHP Code:
    <?php
    echo("Name: {$row['firstName']}");
    ?>
    Does that alter anything? If not, then your second example should work just fine I imagine.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  16. #16
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it does, that didn't throw an error! But I didn't find any mention of this in the manual???

    You must have gotten it from the "Mentor's version"

    And tha manual insists that the earlier syntax "['']" should work. Maybe it's a 4.2.1 thing?
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  17. #17
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's in the manual somewhere -- I'll find it for you. I just always use those curly brackets to explicitly declare variables in any double-quoted strings -- that way I don't have to worry whether PHP will interpret it correctly or not.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  18. #18
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, and you knew this one was coming, What exactly are the curly braces doing in this code? (you got me very interested when you said "I don't have to worrry about PHP interpreting it correctly")

    The ever curious Pata.
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  19. #19
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, sorry. i meant when the variable isn't inside of a string that you need quotes around the index name. inside of a string, you can use Aes's way, or you can lose the quotes around the index:

    PHP Code:
    echo "Name: $row[firstName]"
    this is allowed because the "bare" firstName can't be regarded as a constant as it is when it's outside of a string.

  20. #20
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, and regarding what the braces are doing, that syntax is explained here: http://www.php.net/manual/en/languag...arsing.complex

  21. #21
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically, when the PHP Interpreter program runs into the curly braces in the code, it is instructed to interpret the data inside of those braces as a variable. Kind of a way of removing all doubt, so to speak.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.


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
  •