is there a way to do soemthing like this- see code <php $no1=4; $no2=5; $i=$_GET["value"]; echo $no$i; // I am trying to echo $no1 or $no2 depending upon user sends 1 or 2 value // i want to echo $no1 where 1 should be value of $i ?> PHP: If there is a way to do something like that please tell me I am struck in some code where such thing is needed.
i do those things with arrays .. arrays is a much better and easier way to do it once you get used to the arrays $no = array(); $no[1] = 4; $no[2] = 5; $i = $_GET[$i]; echo $no[$i];
Everything said above is correct , but please keep in mind , if you are getting variables from a submission form , its better to use the method POST instead of GET ! because in method GET the passing variables , usually , are shown in the url ! and their values can be altered. while that not usually happens in method post. SOMETIMES, we are forced to use method GET , so I suggest whenever you use method GET , try to use the rewritting URL method , by enabling the rewrite mod in ur apache , so you can convert, for example , mypage.php?variable1=bla&variable2=heyy to mypage/bla/heyy.html. alot of things can do , but be careful of what method you are using. Regards, Mohie.
Mohie, if your way of securing the variables from being altered is changing from $_GET to $_POST or using url rewrites, you might be in trouble soon .. the $_POST variables are as easy to alter as the $_GET variables for those who are dedicated to alter them .. same is true for cookie and session ID. the right way is to validate those variables on the next page once you receive them (and on every subsequential page) another, some more advanced way (but still derived from the above method), is to save them in a temp DB (or file) and assign a hash which will be passed from page to page .. this is mostly done in login pages (the encryted hash of the password is set as a cookie or session ID and is passed along the way while user is on your domain).
am not saying that method post is the most secure !! but am saying that is better than method GET ! In addition, am with u about everything related validation.. but am not with you about writing them into temp files , because writing and reading is time consuming and might get the site slow , there is many ways to make everything secure, if you want I can open thread and I will explain everything in details.. but my reply for this thread, just to tell him , for a simple form its better to use method post instead of GET thats all regards, Mohie.
the temp files are for some special cases, true that. Not always those are good idea. and about providing some basic advantage, yes $_POST is better but that's just basic advantage, nothing more i had a $_GET form to submit enquiries, and users kept bookmarking that page after submitting their enquiries .. then every time they hit their bookmark - i kept getting the same enquiry over and over .. (since their bookmark contained all the GET parameters from the time they originally submitted the form) .. well, that was silly , so yes, use POST for sure
The code is correct now,but not the best. You can get any form elements value by using the $_GET or $_POST arrays.(Points to choose them,filter values) More about these you can use google... Holp it can help u! Regards