Forum Moderators: coopster
I have a function findvalue();
//Function: Find value
function findvalue($element, $key=0) {
$count = count($_POST[$element]);if($count==1) {
//Single item only
$value = $_POST[$element];
}elseif($count>1) {
//Element is in an array
$value = $_POST[$element][$key];
}//Return value
if($value) {if(get_magic_quotes_gpc()) {
//No slashes needed
$result = "'".trim($value)."'";
}else {
//Add slashes
$result = "'".addslashes(trim($value))."'";
}}
else {
//No result to return
$result = 'NULL';
}return $result;
}
The script is supposed to find the value specified and return it for insertion into a database. I can pass a post variable name into it and optionally a key for post array instances.
I hope I am being clear.
I am trying to call findvalue() like this:
$name = findvalue('name');
$textbase = findvalue('textbase');
$query = "UPDATE helpitems SET name=".$name.", textbase=".$textbase." WHERE id=".$itemid;
Here is my HTML.
<form action="" method="POST" id="edititems">
<table>
<tr>
<td>Help item for: <?php print($titlehere);?><br /><br />
<input type="text" name="name" class="wide" value="New Name" maxlength="45" /><br /><br />
<textarea name="textbase" rows="4" class="wide"></textarea><br /><br /></td>
</tr>
</table>
</form>
The problem is that it grabs and inserts textbase properly but it doesn't work with any input type=text's. It returns the word "Array" instead of what's in the input box. Can you tell me what I am doing wrong?
Please it's been driving me crazy all day.
When it's inside a foreach loop like I'm using it on a different page it works perfectly:
foreach($_POST['itemid'] as $i => $itemid) {
$name = findvalue('name', $i);
$textbase = findvalue('textbase', $i);
...
That's weird, and when I try and use just zero instead of $i it doesn't work. Same problem where the individual input type=text box is passed as an array. Please help.
Change
$count = count($_POST[$element]);
if($count==1) {
//Single item only
$value = $_POST[$element];
}
elseif($count>1) {
//Element is in an array
$value = $_POST[$element][$key];
}
to
if(!is_array($_POST[$element])) {
//Single item only
$value = $_POST[$element];
}
else {
//Already in an array
$value = $_POST[$element][$key];
}
<?php
function findvalue($element, $key=0)
{
$post = $_POST[$element];
$count = count($post);
if($count==1)
{
//Single item only NOT TRUE! it may still be an array!
if(is_array($post)) $value = $post[0];
else $value = $post;
}
elseif($count>1)
{
//Element is in an array
$value = $post[$key];
}
else return false;//Return value
if($value)
{
if(get_magic_quotes_gpc())
{
//No slashes needed
$result = "'".trim($value)."'";
}
else
{
//Add slashes
$result = "'".addslashes(trim($value))."'";
}
}
else {
//No result to return
$result = 'NULL';
}return $result;
}
//Function: Find value
function findvalue($element, $key=0) {
if(!is_array($_POST[$element])) {
//Single item only
$value = $_POST[$element];
}
else {
//Already in an array
$value = $_POST[$element][$key];
}//Return value
blah blah blah blah...
}
Or else does a value of zero in $key get passed as NULL into the array and thusly not work when we ask for $_POST[$element][$key]?
Thanks!