|
-
Feb 23rd, 2004, 10:38 AM
#1
Thread Starter
Frenzied Member
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.
-
Feb 23rd, 2004, 11:27 AM
#2
Stuck in the 80s
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.
-
Feb 23rd, 2004, 12:34 PM
#3
Thread Starter
Frenzied Member
thanks
-
Feb 23rd, 2004, 02:28 PM
#4
Fanatic Member
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>
-
Feb 23rd, 2004, 02:49 PM
#5
Thread Starter
Frenzied Member
change your method to GET instead of POST.
-
Feb 23rd, 2004, 04:03 PM
#6
Fanatic Member
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
-
Feb 23rd, 2004, 05:42 PM
#7
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
-
Feb 24th, 2004, 09:07 AM
#8
Thread Starter
Frenzied Member
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.
-
Mar 8th, 2004, 08:38 AM
#9
Frenzied Member
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
-
Mar 8th, 2004, 09:57 AM
#10
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
-
Mar 8th, 2004, 12:57 PM
#11
Frenzied Member
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?
-
Mar 8th, 2004, 01:04 PM
#12
Nope.... mainly because PHP is a server side technology and has nothing to do with submitting a form.
Tg
-
Mar 8th, 2004, 02:38 PM
#13
Stuck in the 80s
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.
-
Mar 9th, 2004, 03:25 AM
#14
Frenzied Member
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.
-
Mar 9th, 2004, 09:21 AM
#15
Thread Starter
Frenzied Member
You should check out the isset function.
-
Mar 9th, 2004, 09:26 AM
#16
Frenzied Member
-
Mar 9th, 2004, 11:59 PM
#17
Stuck in the 80s
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.
-
Mar 10th, 2004, 02:32 AM
#18
Frenzied Member
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
-
Mar 10th, 2004, 07:54 AM
#19
Thread Starter
Frenzied Member
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.
-
Mar 10th, 2004, 08:34 AM
#20
Frenzied Member
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.
-
Mar 10th, 2004, 01:28 PM
#21
Stuck in the 80s
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.
-
Mar 10th, 2004, 01:34 PM
#22
Stuck in the 80s
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.
-
Mar 10th, 2004, 08:07 PM
#23
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.
-
Mar 11th, 2004, 05:51 PM
#24
Frenzied Member
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.
-
Mar 11th, 2004, 06:09 PM
#25
Stuck in the 80s
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.
-
Mar 11th, 2004, 06:25 PM
#26
Frenzied Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|