469,948 Members | 2,737 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Problem extracting data from form

106 100+
Hi

I m using form like below
[HTML]<form action='' method = post>
<input type=text name=txt id=txt>
<select name=region id=region multiple>
<option value=1>ONE</option>
<option value=2>Two</option>
<option value=3>Three</option>
<option value=4>Four</option>
</select>
<input type=checkbox name=chkbox value=1>One
<input type=checkbox name=chkbox value=2>Two
<input type=checkbox name=chkbox value=3>Three
<input type=checkbox name=chkbox value=4>Four
<input type=submit name=summit value=Enter>
</form>[/HTML]
and I use the following code to get the values
[PHP]
echo "Text Field value = ". $_POST['txt'] . "<br>";
echo "Select Field values=".$_POST['region']. "<br>";
echo "Checkbox Field values=".$_POST['chkbox']. "<br>";
[/PHP]
Let I enter Hello in Text field, select One and Two from select field and Check Three and Four Check Boxes then the above PHP code show the following results

Text Field value=Hello
Select Field values=2
Checkbox Field values=4

where as I expect the follwoing

Text Field value=Hello
Select Field values=1,2
Checkbox Field values=3,4

Looking forword for Help
Thanks in advance
May 26 '08 #1
9 1628
Markus
6,050 Expert 4TB
When you give something a value you need to use quotations
Expand|Select|Wrap|Line Numbers
  1. option value="1"
  2.  
Could you do a print_r($_POST) on the page that receives the submitted values and show us the output.

You shouldnt be able access $_POST['chxbox'] by simply doing that, because it should be a multidimensional array, as you have given all checkboxes the same name.
May 26 '08 #2
hsriat
1,654 Expert 1GB
When you are submitting variables with same name, you got to submit them as array.

Use this instead...
[HTML]<form action='' method = post>
<input type=text name=txt id=txt>
<select name="region[]" id=region multiple>
<option value=1>ONE</option>
<option value=2>Two</option>
<option value=3>Three</option>
<option value=4>Four</option>
</select>
<input type=checkbox name="chkbox[]" value=1>One
<input type=checkbox name="chkbox[]" value=2>Two
<input type=checkbox name="chkbox[]" value=3>Three
<input type=checkbox name="chkbox[]" value=4>Four
<input type=submit name=summit value=Enter>
</form>[/HTML]
On the action page, write
[php]echo "<pre>";
print_r($_POST)
echo "</pre>";[/php] in the start and you can come to know how to manipulate the submitted data according to your requirement.

Regards,
Harpreet

PS: Keeping values of attributes in quotes is really important for a proper mark-up, as markus said.
May 26 '08 #3
Hamayun Khan
106 100+
Thanks for replying to All.

First Thing I don't want to print the values. This was just an example. If I want to save the values into database. If i set the the the method attribute of the form to get then I see the following URL

page.php?txt=Hello&region=1&region=2&chkbox=3&chkb ox=4

I seems not an array.

Second I also I also tried the following

[PHP]

echo $_POST['region'];
echo $_POST['region'][0];
echo $_POST['region'][1];
[/PHP]

Here the first and 2nd statements print same values while the 3rd statement doesn't print any thing. ( Assume that I select one and Two form List).

Third In ASP Every thing is OK.


response.write(request.querystring("region"))

this prints

1,2 And I need the same from PHP.

Please some more help
May 27 '08 #4
hsriat
1,654 Expert 1GB
I didn't force you to print the values. It was just so that you could understand in what form is the data received by PHP, as this is not ASP (but far better than ASP)

and I think you didn't try what I said...

<select name="region[ ]" id=region multiple>

And last of all, use method="post" instead of get.

Read the last post carefully and try what I said. That may help you.
May 27 '08 #5
rpnew
188 100+
Hi,
All the above posts asked you to do this....
Expand|Select|Wrap|Line Numbers
  1. <form action='test.php' method = post>
  2.  
  3.               <input type=text name=txt id=txt>
  4.  
  5.               <select name=region[] id=region multiple>
  6.  
  7.                   <option value=1>ONE</option>
  8.  
  9.                   <option value=2>Two</option>
  10.  
  11.                   <option value=3>Three</option>
  12.  
  13.                   <option value=4>Four</option>
  14.  
  15.               </select>
  16.  
  17.               <input type=checkbox name=chkbox[] value=1>One
  18.  
  19.               <input type=checkbox name=chkbox[] value=2>Two
  20.  
  21.               <input type=checkbox name=chkbox[] value=3>Three
  22.  
  23.               <input type=checkbox name=chkbox[] value=4>Four
  24.  
  25.               <input type=submit name=summit value=Enter>
  26.  
  27.           </form>
  28.  
To get the exact output you wanted i manipulated your PHP script a bit like this...
Expand|Select|Wrap|Line Numbers
  1.     echo "Text Field value = ". $_POST['txt'] . "<br>";
  2.     echo "Select Field values=".implode(",",$_POST['region']). "<br>";
  3.      echo "Checkbox Field values=".implode(",",$_POST['chkbox']). "<br>";
  4.  
Implode will convert your array into string... and for reverse you can use Explode . To directly access array posted you can use foreach loop.

Regards,
RP
May 27 '08 #6
Hamayun Khan
106 100+
I didn't force you to print the values. It was just so that you could understand in what form is the data received by PHP, as this is not ASP (but far better than ASP)

and I think you didn't try what I said...

<select name="region[ ]" id=region multiple>

And last of all, use method="post" instead of get.

Read the last post carefully and try what I said. That may help you.
Thanks for helping

<select name="region[ ]" id=region multiple>

I really forget to place [] braces after the name.

Thanks once again
May 27 '08 #7
hsriat
1,654 Expert 1GB
You are welcome :)
May 27 '08 #8
Markus
6,050 Expert 4TB
I read somewhere that assigning the same name to an element automatically turned it into an array (no need for []).

Hm, I must've read incorrectly.

This post could've been resolved in your first reply, hs. If only people would listen :P
May 27 '08 #9
hsriat
1,654 Expert 1GB
Not in PHP.... might be in ASP.

Try this... [php]//show_post.php
if (!isset($_POST['submit']))
{
echo "<from action=\"show_post.php\" method=\"post\">";
for ($i=0; $i<10; $i++)
echo "<input type=\"text\" name=\"markus\">";
//For the second time, use markus[] instead of markus
echo "<input type=\"submit\" name=\"submit\" value=\"Let's test\"></form>";
}
else
{
echo "<pre>";
print_r($_POST);
echo "</pre>";
}
?>[/php]
Elaboration ;)

... yeh... you are right... just in first reply..
May 27 '08 #10

Post your reply

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

Similar topics

3 posts views Thread by Il Khan | last post: by
5 posts views Thread by Michael Hill | last post: by
3 posts views Thread by Prakash Wadhwani | last post: by
5 posts views Thread by steve_marjoribanks | last post: by
1 post views Thread by napolpie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.