(PHP) How to reference an element in $_POST[] array by way of an expression?
I want to discover whether or not a variable is set. PHP's isset() function (i.e. isset($myVariable)would ordinarily provide me with the variable's set status. However, I want to reference the variable as an expression. Is that possible? and if so, how?
A little more context and detail: I have several image submit buttons in an HTML form, each named 'DeleteFileButtonNNN' where NNN is an integer (actually, a file ID number). When the user clicks, say, the button associated with NNN=2, the form is submitted and $_POST['Dele开发者_如何学GoteFileButton2_x'] gets set (whereas $_POST['DeleteFileButton1_x' and $_POST['DeleteFileButton3_x'] will not be set). I want to be able to loop through the $_POST array in order to discover which button was clicked.
I can easily create the boundaries of the loop itself via a while or foreach loop because all possible file IDs (i.e. NNN values) are stored in a MySQL table. My need for help is in how to specify the various DeleteFileButtonNNN elements as array element indices in the $_POST array since the reference would need to be an expression (e.g. a string concatenation such as 'DeleteFileButton'.$anInteger.'_x'). Yet I don't believe PHP allows me to reference the $_POST array as $_POST[$someVariable].
Could someone me help please? Thank you in advance.
Um... don't. Use array access instead:
<input type="checkbox" name="deleteFileButton[0]" />
<input type="checkbox" name="deleteFileButton[1]" />
<input type="checkbox" name="deleteFileButton[2]" />
If the first and third items are checked above, then when you iterate through $_POST['deleteFileButton']
you'll get values for 0 and 2 and not for 1.
foreach( $_POST['deleteFileButton'] as $key => $val )
print "$key was clicked."
If you like, you can change it to associative array (eg. deleteFileButton[myKey]
) and you can set the value of the input so that $val has some meaningful value.
If you feel that <key>_<index>
is your only option then you can use isset
or array_key_exists
as other posters have suggested, but there will not be a way of knowing the maximum input value ahead of time without storing it somewhere else. Frankly, it is a less efficient and less elegant than using an array.
Leaving my answer here as it is appropriate to the question but @cwallenpoole's answer is better for the problem if not the question.
You could use array_key_exists($key, $_POST) for $_POST if I understand your question correctly.
And also you can access a specify another variable as the key to $_POST as long as it returns the correct value. e.g:
$key = 'DeleteFileButton2_x'
$value = $_POST[$key]
PHP allows you to use anything that devolves down to an integer or a string to reference an array, so you can most definitely do:
function x() {
return "a";
}
$x = 'a';
$val = $_POST[x()];
$val = $_POST[$x];
$val = $_POST['a'];
$val = $_POST[chr(ord('a'))];
"composite" keys are allowed as well:
$_POST["someprefixstring_$x"]; // exactly the same as $_POST['someprefixstring_a'];
<input name="asd[]" value="a">
<input name="asd[]" value="b">
<input name="asd[]" value="c">
<input name="asd[]" value="d">
<?php
foreach($_POST['asd'] as $key => $value)
echo $_POST['asd'][$key];
?>
first i would say php allow you to reference the $_POST array as $_POST[$someVariable]. an easy implementation would be to use a hidden input field and using javascript before submitting the form to replace the value of hidden field with the image id and in your php page get this hidden input as id of the image on which the operation will be performed this will saves many of the if-else constructs
精彩评论