I'm trying to do a simple function here, but it's not working.
I want to check if the variable is indeed in the URL, and if it is I want to define a variable with it.
if(isset($_GET['ref'])){
$ref = $_GET['ref'];
}
Could someone point out the errors?
It's probably that isset only checks if the variable has been declared. That variable could still be empty. so this will set $ref even when $_GET['ref'] = ""; Try this instead:
if(isset($_GET['ref'])){
if(!empty($_GET['ref'])))
{
$ref = $_GET['ref'];
}
}
The Get and Post variables are accessible to you as Arrays. I would loop over the array and treat it as a Key/Value pair. This will give you the ability to do what ever you would like with the key and the value.
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
See this previous Stack Overflow thread.
How do I get the key values from $_POST?
Related
I was wondering if it's possible to put a PHP variable inside $_POST[''] like this:
$_POST[$variable];
I'm asking this because I have a page in which all the inputs have dynamic names according to how many orders the user has made and when I must retrieve their values through post, I never know the names but if I would have something like $_POST[$variable] I would know. The variable is a string and so it would turn out like $_POST['String'].
So is it possible to do something like this?
$numero = $count_ficha;
$countU = 1;
for ($i = 1;$i < $numero + 1;$i++) {
$identificador = "identificadorNI".$countU;
$identificador2 = "identificador".$countU;
$id_subencomenda = $_POST[$identificador];
$countU++;
echo $id_subencomenda;
}
Yes, it is possible to use a variable inside $_POST[''] such as $_POST[$variable]
You will still be required, however, to define a value for $variable
It is posible, but if you don't know the key of $POST[] that you're trying to get the value of, how could you know what value that you have to put on $variable?
For unknow values, the best you can do is a foreach, so you can iterate all the $POST array and then you can do whatever you want with the values, like this
foreach ($_POST as $key) {
//do whatever you want with the $key
}
Check PHP foreach
if you have a dynamic value for the page you just use some $variable because $_POST is use to send information from some HTTP POST method.
(http://php.net/manual/en/reserved.variables.post.php)
maybe you can resolve your problem by creating a global variable.
In my php file when I submit I'm able to read the following variables quite easily (see attached picture)
$mobile = $_REQUEST['mobile'];
$about = $_REQUEST['about'];
$comment = $_REQUEST['comment'];
Now my question is.. how do I read the "category[]" form value?
Just read it like you read normal variables.
$category = $_REQUEST['category'];
Unlike other variables it will return you PHP Array. You can iterate over this variable just like normal PHP Array.
foreach($category as $cat) {
echo $cat;
}
Make sure your check if request actually have the value you're looking for by using isset(). Otherwise above code might throw undefined indexed error.
You should try
foreach ($_REQUEST['category'] as $category) {
var_dump($category);
}
Get array variable
$categories = $_REQUEST['category'];
var_dump($categories);
Try this, this'll also show you the index or keys if you need
print_r ($_REQUEST['category']);
I would like to set a session variable with something akin to:
$key = '_SESSION[element]';
$$key = 'value';
This does indeed set $_SESSION['element'] equal to value, but it also seems to clear the rest of my $_SESSION variable, resulting in the $_SESSION array only containing the new key/value pair.
How can I write into the session using variable variables without nuking it?
Edit: if this can't be done, so be it, we'll probably have to restructure and do things the "right" way. I just wanted to know if there was an easy fix
#Mala, I think eval will help you.
Check the code below. It may help you for what you want.
session_start();
$_SESSION['user1'] = "User 1";
$_SESSION['user2'] = "User 2";
$key = "_SESSION['user3']";
eval("\$$key = 'User 3';");
foreach ($_SESSION as $key=>$value){
echo $key." => ".$value."<br/>";
unset($_SESSION[$key]);
}
session_destroy();
If you still have any trouble, Let me know. Thank you
From PHP Documentation:
Please note that variable variables cannot be used with PHP's
Superglobal arrays within functions or class methods. The variable
$this is also a special variable that cannot be referenced
dynamically.
How you ended up with a situation like this, is really questionable. You're probably doing something wrong.
EDIT
This little trick should give you what you want:
$key = '_SESSION[element]';
$key = str_replace(array('_SESSION[', ']'), '', $key);
$_SESSION[$key] = 'value';
var_dump($_SESSION);
This will basically produce the same results as xdazz's answer
Isn't this way better?
$key = 'element';
$_SESSION[$key] = 'value';
So I'm new to PHP and am trying to create a form. I accept a bunch of parameters and want to process them in the same page. I'm not sure how to do this without a giant if-else containing the entire page as if($_POST). This doesn't seem ideal.
In addition, I'm finding that I do the following a lot. Is there any way to shorten this? The names all remain the same.
$name = $_REQUEST["name"];
$gender = $_REQUEST["gender"];
$age = $_REQUEST["age"];
And I have a lot of lines which are just doing that, and it seems terribly inefficient
You can use the extract() function to do that. But it has a security downside: existing variables can be overwritten if someone would add variables to the POST header.
Edit: hsz's solution is better
What process you are doing with if..else..if you have to post the code so that we can let you know how that can be shorten.
you can avoid the assignment for each variable using extract function.
extract($_POST);
But be aware that can overwrite your existing variable if the are named same as your input controls.
Stop using $_REQUEST, because it is a combination of $_COOKIE , $_POST and $_GET.
It becomes a security risk.
Instead of using $_REQUEST you should use $_POST here.
$keys = array('name', 'gender', 'age');
foreach ( $keys as $key ) {
if ( isset($_POST[$key]) ) {
$$key = $_POST[$key];
}
// optional:
else {
$$key = ''; // default value
}
}
Magic quotes? http://php.net/manual/en/security.magicquotes.php
For the first thing: Turn it around. Don't do
if ($_POST) {
// Your handling code
} else {
echo "No data!";
}
do
if (!$_POST) {
die("No data!");
}
// Your handling code
You can use extract(), however, this means that you're bringing in a lot of variables that you (might not know about) int your current scope.
My suggestion would be to loop through your array and do something with the variables in there (e.g. - validation)
foreach ($_POST as $key => $valu) {
//do something with the variables
}
Also, don't use $_REQUEST unless you really want to check $_GET, $_POST and $_COOKIE. Use the proper array when accessing variables or people can send data you don't expect.
I have some code here which looks as following:
if (isset($_SESSION['addToCart'])) {
foreach ($_SESSION["addToCart"] as &$value){
if ($value['titel'] == $titel){
$value['aantal'] = $aantal;
if($value['aantal'] == 0){
unset($value);
}
}
}
}
so when 'aantal' = 0, I want to delete that record, but it doesn't, it just gives back the result and 'aantal' is 0 instead of the record being removed from the session.
Anyone know what I'm doing wrong?
According to the PHP docs (http://php.net/manual/en/function.unset.php), a variable that is passed by reference is only destroyed in the local context. Try $value = null;
From the manual: "When you unset the reference, you just break the binding between variable name and variable content. This does not mean that variable content will be destroyed."
You'd want to do something like this:
foreach ($_SESSION['addToCart'] as $key => &$value) {
if ($value['aantal'] == 0) {
unset($_SESSION['addToCart'][$key]);
}
}
Think of it like filesystem's symbolic links. When you destroy a symbolic link, it doesn't destroy the file it was linked to.
Your problem may stem from the fact that $value is a value variable, a copy of the $_SESSION["addToCarT"][current_index]. You should be setting the $_SESSION["addToCart"][current_index] to null or unsetting that, not the copies variable in the limited scope.