Results 1 to 26 of 26

Thread: check if field is in GET array? [Resolved]

  1. #1

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    check if field is in GET array? [Resolved]

    There are times that I want to pass certain fields to a page and other times those fields won't be there.

    I want to use those values if they are in the array, but not if they aren't there. But I know from experience that if you check for a key that isn't there, it gives you an error about an invalid variable.

    Is there a way to check this or ignore the error?
    Last edited by ober0330; Feb 23rd, 2004 at 12:35 PM.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Code:
    if (isset($_GET['elementName'])) {
        //...
    }
    There is also a way to loop through all of the elements in the $_GET array without having to know the element keys, which might make your code shorter, depending on what you're doing.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    thanks
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  4. #4
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648

    PLEASE HElp

    This is closely related to this post so I hope you do not mind If I continue here...


    I have a form and am posting to a php page but the parameters are not being passed to the php script.

    here is the form declaration:

    PHP Code:

    <form ACTION="http://www.nkgroup.co.za/admin/mailer.php" METHOD="post" id=form1 name="form1" target="" LANGUAGE=javascript onsubmit="return form1_onsubmit()"
    This is the request line that ie uses to send to the website:

    PHP Code:
    [url]http://www.nkgroup.co.za/admin/mailer.php?txtname=mike&[email protected]&lstemail=Email&txtaddress=&txtpcode=&lstcountry=&submit1=Submit[/url]' 


    And here is the script:


    PHP Code:
    <?    
        if (isset($_GET['txt_name'])) 
        {
            echo("param email OK");
        }
        else
        {
            echo("param email NOT OK");
        }
        
        $msg = "A user has just downloaded NetReachDemo
            \nName $txt_name
            \nEmail $txt_email
            \nMethod $lst_email
            \nAddress $txt_address
            \nPostalCode $txt_pcode
            \nCountry $lst_country";

        mail("[email protected]","NetReacherDemo Download", $msg, "From:NK Group");
        $msg="Thank you for your interest in NetReacher Demo. This is an automated response. 
            You should receive a response from us within 48 hrs. For more information, 
            please visit [url]http://www.nkgroup.co.za[/url]";
        mail($Email, "NetReacher Demo", $msg, "From:NK Group");

    ?>

    <html>
    <head>
    <script>
        //location.href = "http://www.nkgroup.co.za/net-reacher.htm#150"
    </script>
    <HTML>
    <HEAD>
    <META NAME="Keywords" Content="NetReacher">
    <TITLE></TITLE>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

  5. #5

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    change your method to GET instead of POST.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  6. #6
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    Thanks ober - that worked!!!

    Apparently some servers only accept the $_GET['var'] form of retrieving the variable from the request string.

    Thanks for your help



  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by MikkyThomeon
    Thanks ober - that worked!!!

    Apparently some servers only accept the $_GET['var'] form of retrieving the variable from the request string.

    Thanks for your help


    No, it isn't that at all.... it has to do with how you want to recieve your data. If you change $_GET to $_POST and then change the method of your form to "post" then it should work just the same. Only then you would not be able to see the query string (since it gets sent over as part of the header information.)

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Originally posted by MikkyThomeon
    Apparently some servers only accept the $_GET['var'] form of retrieving the variable from the request string.
    As TG said, this is not the case. They are 2 different methods with 2 different global arrays ($_GET and $_POST).

    GET shows the URL with all of the variables in it. POST is more of a hidden passing of the variables. They do not show up in the URL when submitting.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  9. #9
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Another relates issue....

    What hacks me off is that when I set a form to "Post" and have text fields and radio buttons all set and ready to go, if I use a hypertext link from the page none of the text or radio button info is posted.

    The data associated with the hypertext link comes across in $_GET. but the $_POST is empty.

    I can get around this by converting my hyperlinks into radio buttons instead and doing a jump using a submit button - then everything is in the $_POST array.

    Is this a problem with the web server I am using (Abyss by the way) or is this a truism of web page operation.

    Confused of Windsor
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Nooo.... that's the way it works.... you need to have a form setup, then the form needs to be submitted. There are ways to do it with hypterlinks, rather than buttons, but it involves calling javascript code that then submits the form.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    I was afraid of that. Shame really as it limits the user interface options to even fewer than we already have.

    Do you have you any idea if a form post could be forced using PHP?
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Nope.... mainly because PHP is a server side technology and has nothing to do with submitting a form.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by David.Poundall
    Another relates issue....

    What hacks me off is that when I set a form to "Post" and have text fields and radio buttons all set and ready to go, if I use a hypertext link from the page none of the text or radio button info is posted.

    The data associated with the hypertext link comes across in $_GET. but the $_POST is empty.
    If this is the case -- when you don't know if the data will come via $_GET or $_POST -- you can use the $_REQUEST global, which contains $_GET and $_POST data (along with a few others).

    So no matter which method it comes, it will be in $_REQUEST.

    I've had this scenario before, and use $_REQUEST to handle it. However, it is recommended not to use $_REQUEST[] if there's another way around it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  14. #14
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    I check that one by doing a ...

    PHP Code:
    $variable=$_POST['varname'];
    if (
    $variable==''$variable=$_GET['varname']; 
    But the problem is that the data just doesn't send in the first place. The Hyperlink has pre-defined variables thet can be recovered using $_GET. But Techgnome appears to be right here. Because the form is not submitted - it is just linked from - the $_POST variables are not attached at the the client end at the time the link is invoked.


    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  15. #15

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    You should check out the isset function.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  16. #16
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    It would still be two steps to load the variable, this way I don't have to remember to use 'isset' which I always keep forgetting anyway.

    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  17. #17
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by David.Poundall
    I check that one by doing a ...

    PHP Code:
    $variable=$_POST['varname'];
    if (
    $variable==''$variable=$_GET['varname']; 
    But the problem is that the data just doesn't send in the first place. The Hyperlink has pre-defined variables thet can be recovered using $_GET. But Techgnome appears to be right here. Because the form is not submitted - it is just linked from - the $_POST variables are not attached at the the client end at the time the link is invoked.


    That's just really poor coding.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  18. #18
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    What is? - the linking or the non use of isset.

    The non use of isset is neither here nor there. One way is as good as another.

    The linking is inevitable. I am writing a web based application. In an application - I am sure you understand this - it is all about user perception. The users in this instance are not in the least PC Savy and so I am trying to make it easy on them by making the interface less "Submit Button" based and more "Click" based. More like a VB app feel if you follow.

    Idon't want to use cookies or sessions as they rely on the client being configured to some degree. This has to be idiot proof

    So I find myself wanting to maintain state with the odd array or two. Of course you lose it when you jump. My simple question was - is this normal.

    It apears it is.

    -----------------

    Cut to the quick - of Windsor
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  19. #19

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Originally posted by David.Poundall
    PHP Code:
    $variable=$_POST['varname'];
    if (
    $variable==''$variable=$_GET['varname']; 
    That doesn't even make sense. #1, what happens if 'varname' is not set? You'll get an error or at least a warning. Where do you handle that? #2, It's really quite silly to check both. Just use the REQUEST array.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  20. #20
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    I use a top level .php filename vector and switch to sub forms depending on the variables received. As not all variables are set for every form my code is insensitive to a variable coming through as no value.

    I only use this code structure so that I don't have to ponder if the variable coming through is via the $_Get get or $_Post route. Depending on the downstream form build it could be sent across via either path.

    I havn't come across any code referencing the use of the REQUEST array. I will check that out. Thanks for the constructive pointer ober.
    Last edited by David.Poundall; Mar 10th, 2004 at 08:38 AM.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  21. #21
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by David.Poundall
    I havn't come across any code referencing the use of the REQUEST array. I will check that out. Thanks for the constructive pointer ober.
    Dude, I said the same thing about 10 posts up. I even gave an explaination of the $_REQUEST array.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  22. #22
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by David.Poundall
    What is? - the linking or the non use of isset.

    The non use of isset is neither here nor there. One way is as good as another.
    No, one way is not as good as another. One way (your way) is sloppy and will produce notice errors. Perhaps you have them shut off, but if you ever decide to release your code or application, the people who download it are going to be in a world of hurt. Especially if they pay for it.

    The same go for clients. You may have notice errors off on your development machine, but how do you know the clients (and future clients) will?

    If you're just coding little projects for yourself, fine, be sloppy. But we're going to call you out everytime you do it.

    Originally posted by David.Poundall
    The linking is inevitable. I am writing a web based application. In an application - I am sure you understand this - it is all about user perception. The users in this instance are not in the least PC Savy and so I am trying to make it easy on them by making the interface less "Submit Button" based and more "Click" based. More like a VB app feel if you follow.
    How are the users going to perceive a crapload of notices in your application?

    I'm not telling you to not use links...I'm telling you how to do it the clean way.

    Originally posted by David.Poundall
    Idon't want to use cookies or sessions as they rely on the client being configured to some degree. This has to be idiot proof
    1) Using cookies or sessions for this problem would be overkill. 2) Sessions have NOTHING to do with the client machine. Everything is stored on the server.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  23. #23
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    heh.. you know, if you're looking for a more VB application feeling form, using links wouldn't be the way to go about that.. seeing as how VB incorporates the Command Button and most people use it. I hate the Command Button in VB; it's ugly and uncustomizable. However, the Command Button is fairly customizable within HTML using CSS, and you can even make it look like a link, if you want.. it wouldn't be the best way, but hey, it works.
    Like Archer? Check out some Sterling Archer quotes.

  24. #24
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    OK - lets try and put this one to bed....

    Hobo
    ------
    you mentioned the $_REQUEST global up top but I put it to the back of my mind for now as you mentioned in the same breath that it is a non preferred aproach - why is that? Is it possibly going to be depracated ?

    Also, why would those two lines of code geneate notice errors? More to the point from my end - what IS a notice error. The PHP I am using is out of the box. Register globals turned off of course. But that is all.

    Finally Hobo, yep, sessions - thanks for the pointer. I agree that is the way to go.

    Kows
    -------
    Thank for that. I will experiment and see what I can come up with.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  25. #25
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by David.Poundall
    you mentioned the $_REQUEST global up top but I put it to the back of my mind for now as you mentioned in the same breath that it is a non preferred aproach - why is that? Is it possibly going to be depracated ?
    The problem with the $_REQUEST array is that it contains a lot of information, not only from $_GET and $_POST, but from $_COOKIE, as well. So you have to be very careful in your coding, as well as being conscious of other applications.

    If you have something coming via $_POST, such as $_POST['name'], but some other script (that the user ran on their server) created a cookie with the name 'name', how are you going to know whether $_REQUEST['name'] holds the value of your form data or the cookie set by the other program?

    You don't.

    So it's probably a good recommendation NOT to use $_REQUEST unless you absolutely have to. I just threw it out there as an option. I use it in some programs, but I probably shouldn't. Now that I think about it, I'll probably change it in future releases of my software.

    The BEST thing you can probably do is something like:

    PHP Code:
    if (isset($_POST['name'])) {
        
    // variable was sent via $_POST:
        
    $_DATA['name'] = $_POST['name'];
    } elseif (isset(
    $_GET['name'])) {
        
    // variable was sent via $_GET:
        
    $_DATA['name'] = $_GET['name'];
    } else {
        
    // variable was not sent at all:
        
    $_DATA['name'] = '';

    But that's a lot of code just to check, especially if you have a lot of variables being sent. Which is why I've always used $_REQUEST. It would probably be pretty easy to setup a function to loop the two arrays and create your own array with the data, and place that in an included file that is called on every script. That's probably a good method. Let me know if you want to see an example and I'll work it up. I'll probably go that route with my code in the future.

    Originally posted by David.Poundall
    Also, why would those two lines of code geneate notice errors? More to the point from my end - what IS a notice error. The PHP I am using is out of the box. Register globals turned off of course. But that is all.
    A notice is like an error, except it doesn't stop the execution of the rest of the code. It just produces a message to the browser.

    In the code you provided, a notice would be given when $_POST['variablename'] is not set...the notice will say that it is not set, or that it doesn't exist.

    Of course, this doesn't happen if notices are shut off (in the php.ini). They usually come turned on, and if you didn't change anything, it surprises me that you haven't gotten a notice for it.

    When I first installed PHP, I got notices all over the place. Taught me a good programming lesson (check to make sure the variables exist before I try to use them).

    If you distribute things you make in PHP, you can't be sure that the people that use them will have notices off. If they don't, they could end up with a slew of messages all over the browser if you don't check variables properly.

    --------------------------

    This post turned out to be much longer than I thought it'd be, but I hope I explained things clearly enough.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  26. #26
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Crystal clear Hobo. I will checkout PHPINFO to see if notices are turned off.

    Thanks for taking the time to set me straight on all this.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width