469,890 Members | 1,929 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

form passing value problem in loop

Hi all,
I would like to know how to assign and get the form value when I using it in a loop. I cannot get all the input that I inserted in 'Desc' part and it is from this site. May I know to get this work when I do a loop? Thanks.

a.php
[HTML]// this can work
<font size="2">Title:</font><br><input type="text" name="Title[]" size="40" value="<?php echo htmlentities($match[$counter], ENT_QUOTES); ?>">
</tr>
<tr>&nbsp;</tr>
// but this can only get the last input when do multiple post
<tr>
<font size="2">Description:</font>
<script language="JavaScript" type="text/javascript">
<!--
function submitForm() {
updateRTE('Desc');
return true;
}

initRTE("images/", "", "");
writeRichText('Desc', '', 350, 200, true, false);

//-->
</script>
</tr>[/HTML]
b.php
[PHP]$title= $_POST['Title'];
$desc= $_POST['Desc'];
mysql_query("insert into post(title, description)values ('$title[$counter]', '$desc[$counter]')")or die (mysql_error());[/PHP]
Mar 16 '08 #1
16 2005
ronverdonk
4,258 Expert 4TB
This piece of code [php]<input type="text" name="Title[]" [/php] shows that you have the value of that input field in an array with the name 'Title'. Since I see that it is the only value assigned to that array, it will be stored in the first entry. So to get that value out, assuming you use a posted form, you specify[php]$title=$_POST['title'][0];[/php]Ronald
Mar 16 '08 #2
This piece of code [php]<input type="text" name="Title[]" [/php] shows that you have the value of that input field in an array with the name 'Title'. Since I see that it is the only value assigned to that array, it will be stored in the first entry. So to get that value out, assuming you use a posted form, you specify[php]$title=$_POST['title'][0];[/php]Ronald
thanks, oops..sorry about that, but I had used the counter in that place also, but I can only get the value of title, and i can't get the value of decription while in the loop. The problem is lied on the description, it works fine when single, but when there's a loop, then it will have problem in getting the value of the 'Desc' part.
Mar 16 '08 #3
Markus
6,050 Expert 4TB
thanks, oops..sorry about that, but I had used the counter in that place also, but I can only get the value of title, and i can't get the value of decription while in the loop. The problem is lied on the description, it works fine when single, but when there's a loop, then it will have problem in getting the value of the 'Desc' part.
I cant even see you using a loop.
Mar 16 '08 #4
TheServant
1,168 Expert 1GB
Can you post all your relevant code, because there is no loop there, so not sure what you're talking about.
Mar 16 '08 #5
a.php
[PHP]
<?php
$counter=0;
while($counter<$test)
{
?>
<form name="myform" action="b.php" method="post">
<table>
// this can work
<font size="2">Title:</font><br><input type="text" name="Title[]" size="40" value="<?php echo htmlentities($match[$counter], ENT_QUOTES); ?>">
</tr>
<tr>&nbsp;</tr>
// but this can only get the last input when do multiple post
<tr>
<font size="2">Description:</font>
<script language="JavaScript" type="text/javascript">
<!--
function submitForm() {
updateRTE('Desc');
return true;
}

initRTE("images/", "", "");
writeRichText('Desc', '', 350, 200, true, false);

//-->
</script>
</tr>
</table>
<?php
$counter++;}
?>
</form>
<input type="submit" value="Save"/>
[/PHP]
b.php
[PHP]
<?php
$counter=0;
while($counter<$test)
{

$title= $_POST['Title'];
$desc= $_POST['Desc'];
mysql_query("insert into post(title, description)values ('$title[$counter]', '$desc[$counter]')")or die (mysql_error());

$counter++;
}
[/PHP]
Mar 17 '08 #6
ronverdonk
4,258 Expert 4TB
Information is lacking, but I'll give it a try:
Assuming that :
1. Title and Desc are both user (form) filled arrays with an equeal count of entries
2. You want to store each of those fields in a db table
3. You look at the following sample for B.PHP[php]<?php
for ($counter=0; $counter<count($_POST['Title']); $counter++)
{
$title= $_POST['Title'][$counter];
$desc = $_POST['Desc'][$counter];
mysql_query("insert into post(title, description)values ('$title', '$desc'")
or die (mysql_error());

}
?>[/php]Ronald
Mar 17 '08 #7
Thanks, ronverdonk, my while loop is seems like your for loop, but I still can't get the second 'Desc' part for example if it loops twice. I not sure how to get the value from the 'Desc' after it is posted.

Information is lacking, but I'll give it a try:
Assuming that :
1. Title and Desc are both user (form) filled arrays with an equeal count of entries
2. You want to store each of those fields in a db table
3. You look at the following sample for B.PHP[php]<?php
for ($counter=0; $counter<count($_POST['Title']); $counter++)
{
$title= $_POST['Title'][$counter];
$desc = $_POST['Desc'][$counter];
mysql_query("insert into post(title, description)values ('$title', '$desc'")
or die (mysql_error());

}
?>[/php]Ronald
Mar 18 '08 #8
ronverdonk
4,258 Expert 4TB
Read the assumptions in my post and it will be clearer. E.g. I assumed that youn have as many Desc's as Title's and both are in an array with the same number of enries.
If that is not true, you must retrieve the Desc value outside the loop.

Problem is that I cannot read (do not understand) how the form you showed is constructe (RTE??). Title is clear, but I cannot work out how and where you insert Desc into the form.

Ronald
Mar 18 '08 #9
Read the assumptions in my post and it will be clearer. E.g. I assumed that youn have as many Desc's as Title's and both are in an array with the same number of enries.
If that is not true, you must retrieve the Desc value outside the loop.

Problem is that I cannot read (do not understand) how the form you showed is constructe (RTE??). Title is clear, but I cannot work out how and where you insert Desc into the form.

Ronald
oo..sorry about that, I think here should make you clearer. http://www.dynamicdrive.com/dynamici...itor/index.htm
For the 'Desc' part, I take from here. Thanks.
Mar 19 '08 #10
ronverdonk
4,258 Expert 4TB
Now I see what you were doing: using a richt text editor to get and pass the values.

Now the quickest way to tackle this and to see what is actually passed to your B.PHP script, is to put the following statement at the top of your B.PHP.[php]echo '<pre>'; print_r($_POST); echo '</pre>';[/php]That will show you exactly what is passed to B.PHP and in what type of format (single vars or array).

When you know that you can determine how to handle the $_POSTed values.

Ronald
Mar 19 '08 #11
Now I see what you were doing: using a richt text editor to get and pass the values.

Now the quickest way to tackle this and to see what is actually passed to your B.PHP script, is to put the following statement at the top of your B.PHP.[php]echo '<pre>'; print_r($_POST); echo '</pre>';[/php]That will show you exactly what is passed to B.PHP and in what type of format (single vars or array).

When you know that you can determine how to handle the $_POSTed values.

Ronald
Thanks, I can see the output already. The 'Desc' part only get the last value that I entered. I wonder why it cannot get the both values if there are two (loop)...
Mar 20 '08 #12
ronverdonk
4,258 Expert 4TB
You can only do that when keeping an index counter and concatenate that with the 'desc' name in your RTE statement. Or make 2 RTE's with different names.
So then you get 'desc1' and 'desc2' fields.

Ronald
Mar 20 '08 #13
aktar
105 100+
Yep, I would second that
Mar 20 '08 #14
ronverdonk
4,258 Expert 4TB
Yep, I would second that
Why would you do that?

Ronald
Mar 21 '08 #15
thanks ronverdonk for your help. I change a bit of your post in #7. now it can work already.
Mar 21 '08 #16
ronverdonk
4,258 Expert 4TB
thanks ronverdonk for your help. I change a bit of your post in #7. now it can work already.
Glad it works for you. But, since I am curious, did you change the Desc.. names as suggested before?

Ronald
Mar 23 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by jedimasta | last post: by
26 posts views Thread by Jerim79 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.