Can I submit an array in a form as an input(type="hidden")?
Can any one show me a simple example?
I tried below code but it didn't work out.
<?php
$idx = $_POST["idx"];
if (!$idx)
$idxArr = array();
else
$idxArr = $_POST["idxArr"];
$idxArr[] = $idx;
print "<form action= '".$_SERVER["PHP_SELF"]."' method='post'>";
print "<input name='idxArr' value='$idxArr' type='hidden'>";
print "<input name='idx' value='$idx' type='text'>";
print "<input type='submit' value='submit'>";
print "</form>";
?>your print commands are wrong for a start - it's a function and needs to be treated as such:
print("content") or print($content) or print("text". $var)
you could implode the array into a single string, then have that as your hidden variable, then explode it into an array again at the target
PHP manual: IMPLODE (<!-- m --><a class="postlink" href="http://uk2.php.net/manual/en/function.implode.php">http://uk2.php.net/manual/en/function.implode.php</a><!-- m -->)
PHP manual: EXPLODE (<!-- m --><a class="postlink" href="http://uk2.php.net/manual/en/function.explode.php">http://uk2.php.net/manual/en/function.explode.php</a><!-- m -->)Oh.. so that's the difference between echo and print?
Thanks!why do you do that? you are over writing what you set
$idx = $_POST["idx"];
if (!$idx)
$idxArr = array();
else
$idxArr = $_POST["idxArr"];
$idxArr[] = $idx;
what about the other ones? you set $idxArr in the if statement and then you overwrite that.I haven't thought about it thoroughly.
But here I try to have $idxArr correct.
Let's say I have a list of forms.
Search by Name: [ ]
<search>
-------------------------
idx 1
name: [ ]
phone number: [ ]
<save>
idx 2
name: [ ]
phone number: [ ]
<save>
...
After each submit(by click on <save>),
I want the saved list to be dimmed(disabled) until user does another search.that will be difficult to do. and what does this have to do with the array?It might not be the most efficient way but I just want a quick solution.
Every time user clicks on <save>, it stores the idx to the $idxArr then when it displays the list it checks if its idx is in the array.
so..
$results = mysql_query("select * from people");
while ($oneRec = mysql_fetch_array($results))
{
if oneRec[idx] in the $idxArr // use a loop for this part?
{
print "<input name='name' type='text' value='$oneRec[name]' disabled>";
print "<input name='phoneNum' type='text' value='$oneRec[phoneNum]' disabled>";
print "<input type='submit' value='save' disabled>";
}
else
{
print "<input name='name' type='text' value='$oneRec[name]'>";
print "<input name='phoneNum' type='text' value='$oneRec[phoneNum]'>";
print "<input type='hidden' name='idx' value='$oneRec[idx]'>";
print "<input type='hidden' name='idxArr' value='$idxArr'>";
print "<input type='submit' value='save'>";
}
}// end of while
And on a search, it unsets the $idxArr.I found the answer.
To submit an array one can use a loop like following.
print "<form method='post' ...>";
for ($i=0; $i<count($modelArr); $i++)
{
print "<input type='hidden' name='modelArr[$i]' value='$modelArr[$i]'>";
}
print "<type='submit' ...>";
print "</form>";
And receive it like..
$modelArr = $_POST["modelArr"];ok a couple of things.
you will not recieve it like that. also if iti s the start of the array then you don't need the $i in it.
print "<input type='hidden' name='modelArr[]' value='$modelArr[$i]'>";
will work just fine
it gets submitted like this
$modelArr = $_POST["modelArr"][$i];
and you have to lopp through those as well. also i is recommended to not have count in the for loop because it has to count each and every time, just have a variable instead.
$total = count($modelArr);
for ($i=0; $i<$total; $i++)
just a little faster.
and Horus:
print() is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.cheers Scoutt,
I found a bit more out -->HERE<-- (<!-- m --><a class="postlink" href="http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40">http://www.faqts.com/knowledge_base/vie ... d/1/fid/40</a><!-- m -->)Originally posted by Horus_Kol
cheers Scoutt,
I found a bit more out -->HERE<-- (<!-- m --><a class="postlink" href="http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40">http://www.faqts.com/knowledge_base/vie ... d/1/fid/40</a><!-- m -->)
and that is 4 years and all wrong.
there is not one bit of difference with echo or print. that was back in php2 or php3 days.oops - their documentation needs a little cleaning up then. Mind you - I know how hard it is to do that!
Can any one show me a simple example?
I tried below code but it didn't work out.
<?php
$idx = $_POST["idx"];
if (!$idx)
$idxArr = array();
else
$idxArr = $_POST["idxArr"];
$idxArr[] = $idx;
print "<form action= '".$_SERVER["PHP_SELF"]."' method='post'>";
print "<input name='idxArr' value='$idxArr' type='hidden'>";
print "<input name='idx' value='$idx' type='text'>";
print "<input type='submit' value='submit'>";
print "</form>";
?>your print commands are wrong for a start - it's a function and needs to be treated as such:
print("content") or print($content) or print("text". $var)
you could implode the array into a single string, then have that as your hidden variable, then explode it into an array again at the target
PHP manual: IMPLODE (<!-- m --><a class="postlink" href="http://uk2.php.net/manual/en/function.implode.php">http://uk2.php.net/manual/en/function.implode.php</a><!-- m -->)
PHP manual: EXPLODE (<!-- m --><a class="postlink" href="http://uk2.php.net/manual/en/function.explode.php">http://uk2.php.net/manual/en/function.explode.php</a><!-- m -->)Oh.. so that's the difference between echo and print?
Thanks!why do you do that? you are over writing what you set
$idx = $_POST["idx"];
if (!$idx)
$idxArr = array();
else
$idxArr = $_POST["idxArr"];
$idxArr[] = $idx;
what about the other ones? you set $idxArr in the if statement and then you overwrite that.I haven't thought about it thoroughly.
But here I try to have $idxArr correct.
Let's say I have a list of forms.
Search by Name: [ ]
<search>
-------------------------
idx 1
name: [ ]
phone number: [ ]
<save>
idx 2
name: [ ]
phone number: [ ]
<save>
...
After each submit(by click on <save>),
I want the saved list to be dimmed(disabled) until user does another search.that will be difficult to do. and what does this have to do with the array?It might not be the most efficient way but I just want a quick solution.
Every time user clicks on <save>, it stores the idx to the $idxArr then when it displays the list it checks if its idx is in the array.
so..
$results = mysql_query("select * from people");
while ($oneRec = mysql_fetch_array($results))
{
if oneRec[idx] in the $idxArr // use a loop for this part?
{
print "<input name='name' type='text' value='$oneRec[name]' disabled>";
print "<input name='phoneNum' type='text' value='$oneRec[phoneNum]' disabled>";
print "<input type='submit' value='save' disabled>";
}
else
{
print "<input name='name' type='text' value='$oneRec[name]'>";
print "<input name='phoneNum' type='text' value='$oneRec[phoneNum]'>";
print "<input type='hidden' name='idx' value='$oneRec[idx]'>";
print "<input type='hidden' name='idxArr' value='$idxArr'>";
print "<input type='submit' value='save'>";
}
}// end of while
And on a search, it unsets the $idxArr.I found the answer.
To submit an array one can use a loop like following.
print "<form method='post' ...>";
for ($i=0; $i<count($modelArr); $i++)
{
print "<input type='hidden' name='modelArr[$i]' value='$modelArr[$i]'>";
}
print "<type='submit' ...>";
print "</form>";
And receive it like..
$modelArr = $_POST["modelArr"];ok a couple of things.
you will not recieve it like that. also if iti s the start of the array then you don't need the $i in it.
print "<input type='hidden' name='modelArr[]' value='$modelArr[$i]'>";
will work just fine
it gets submitted like this
$modelArr = $_POST["modelArr"][$i];
and you have to lopp through those as well. also i is recommended to not have count in the for loop because it has to count each and every time, just have a variable instead.
$total = count($modelArr);
for ($i=0; $i<$total; $i++)
just a little faster.
and Horus:
print() is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.cheers Scoutt,
I found a bit more out -->HERE<-- (<!-- m --><a class="postlink" href="http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40">http://www.faqts.com/knowledge_base/vie ... d/1/fid/40</a><!-- m -->)Originally posted by Horus_Kol
cheers Scoutt,
I found a bit more out -->HERE<-- (<!-- m --><a class="postlink" href="http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40">http://www.faqts.com/knowledge_base/vie ... d/1/fid/40</a><!-- m -->)
and that is 4 years and all wrong.
there is not one bit of difference with echo or print. that was back in php2 or php3 days.oops - their documentation needs a little cleaning up then. Mind you - I know how hard it is to do that!