Jump to content

How to get values from Textboxes generated through a Loop.


theITvideos

Recommended Posts

Hi there,

 

I am working on a PHP form and running a loop to generate textboxes at runtime.

 

This code output the textboxes:

 

foreach ($MyTest as $tst)
{
   echo "<input name='testName' type='text'  size='12' maxlength='5' />";
}

 

I am able to display (or output) 4 or sometimes 6 textboxes depending on the variable $MyTest.

 

Now after I enter the values in these texboxes. Lets say, it outputs 3 textboxes, I woud like to display the values like:

 

In Textbox 1 you entered:

In Textbox 2 you entered:

In Textbox 3 you entered:

 

How can I get (or fetch) values from the textboxes generated through the loop.

 

Please reply.

 

All comments and feedbacks are always welcomed!

 

Thank you :)

Link to comment
Share on other sites

The way you're generating them will make it so that only the value of the last one will be passed through the $_POST array because they'll all have the same name= attribute. Why are you using a foreach() loop on an array, then not using any of the values from that array?

Link to comment
Share on other sites

I'm half awake so I may have missed something, or maybe it just feels like it.

btw I assumed the array was $_POST.

 

foreach($_POST as $key => $value){
   echo "<input name=\"$key\" value=\"$value\" type='text'  size='12' maxlength='5' />";
}

 

If that's wrong, someone please help this guy that knows what they are doing haha.

Link to comment
Share on other sites

The way you're generating them will make it so that only the value of the last one will be passed through the $_POST array because they'll all have the same name= attribute. Why are you using a foreach() loop on an array, then not using any of the values from that array?

 

Thats really a good point Pikachu2000, so how do generate or output the Textboxes field that will allows us to store the values.

 

Now the reason why I am using the foreach loop is because with the foreach loop, the rows are associated. I mean, if there are 4 rows (or items) in the array, it must output 4 textboxes next to the values.

 

Here is the refined code:

 

foreach ($MyTest as $tst)
{
echo $tst['Prod'] . "<input name='testName' type='text'  size='12' maxlength='5' />";
}

 

So now with every $tst['Prod'] variable, the textbox will appear next to it, and we can enter values in the textboxes.

 

This is what the requirement is. And I am able to accomplish this. Now the final step is to get the values from these textboxes appearing next to the variable.

 

What is the better way of doing this. Whats your best suggestion.

 

Thank you! :)

 

 

 

Link to comment
Share on other sites

You're on the right track with that. I'd actually set up the name the opposite, though and do it like below. That way all of those fields end up in one subarray within the $_POST array.

foreach ($MyTest as $tst)

{

echo Prod['$tst'] . "<input name='testName' type='text'  size='12' maxlength='5' />";

}

 

Then you could loop through them with

[code=php:0]

foreach( $_POST['Prod'] as $key => $val ) {

    echo "Index key: $key holds the value: $val<br>";

}

 

Let me know if this doesn't make sense to you.

Link to comment
Share on other sites

You're on the right track with that. I'd actually set up the name the opposite, though and do it like below. That way all of those fields end up in one subarray within the $_POST array.

foreach ($MyTest as $tst)

{

echo Prod['$tst'] . "<input name='testName' type='text'  size='12' maxlength='5' />";

}

 

Then you could loop through them with

[code=php:0]

foreach( $_POST['Prod'] as $key => $val ) {

    echo "Index key: $key holds the value: $val<br>";

}

 

Let me know if this doesn't make sense to you.

 

Thanks for your reply.

 

Now the output I get is shown in the Screenshot I've attached.

 

Now we just need to [u]get[/u] these textbox values (the textbox that we output in the foreach loop)

 

Keeping in mind the good point you mentioned earlier that only the value of the last one will be passed through the $_POST array because they'll all have the [u]same[/u] name= attribute. So shall we assign textboxes a different name at runtime...

 

So whats a good approach here to get the values and simply output saying this is what you entered for 1 textbox and for 2 textbox etc.

 

Please see the screenshot.

 

Thank you! :)

 

[attachment deleted by admin]

Link to comment
Share on other sites

I see I had a major typo in the post above, but also I didn't take into consideration that the values in the foreach() loop could end up being the same. So I'd make a change like this:

 

foreach ($MyTest as $tst)
{
echo "<input name=\"prod[]\" type=\"text\"  size=\"12\" maxlength=\"5\" />";
}

 

Then each field will end up in the $_POST array as $_POST['prod'][0], $_POST['prod'][1], $_POST['prod'][2], etc.

Link to comment
Share on other sites

I see I had a major typo in the post above, but also I didn't take into consideration that the values in the foreach() loop could end up being the same. So I'd make a change like this:

 

foreach ($MyTest as $tst)
{
echo "<input name=\"prod[]\" type=\"text\"  size=\"12\" maxlength=\"5\" />";
}

 

Then each field will end up in the $_POST array as $_POST['prod'][0], $_POST['prod'][1], $_POST['prod'][2], etc.

 

Thanks for you reply. I put prod[] for the textbox name:

 

<input name=\"prod[]\" type=\"text\"  size=\"12\" maxlength=\"5\" />

 

And trying to read the textbox values as:

 

echo "Textbox 1 value entered is: " . $_POST['prod'][0];

 

I get no value for textbox 1. the result I'm getting is:

 

Textbox 1 value entered is:

 

I even tried to use $_GET instead of $_POST:

 

echo "Textbox 1 value entered is: " . $_GET['prod'][0];

 

But still no output, is there something we need to do here.

 

Please reply. Thank you. :)

 

Link to comment
Share on other sites

That looks fine to me. Throw this block of code at the top of the script and see what exactly shows up.

 

echo '<pre>';
print_r($_POST);
echo '</pre>';

 

Thanks for the reply. I tried this code: print_r($_POST) enclosed with <pre> but I get no output (value) for the textboxes.

 

Lets try some more methods.  :)

 

Thanks!

Link to comment
Share on other sites

Paste in the actual code you're working with. I just set up a similar script locally, and it tested fine.

 

Thanks for the reply.

 

I guess I get the picture now. I have multiple submit buttons on my page. And the output for member details that I'm creating is lying outside the submitting form thus it is not shown in that $_POST variable.

 

I guess this is what happens when we got multiple submit buttons on a single page. (though i need those multiple submit for submitting different information).

 

I think thats why it is working on your computer :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.