473,388 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 1753
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

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

Similar topics

8
by: bmgx | last post by:
I would like to use an already existing online service (currency converter) basically consisting of a html form with a few options that is submitted and returns the results. I really don't know...
3
by: Il Khan | last post by:
Hi, I have an asp application running on a dozen of winXP machines configured as kiosks. This application stores its data on a MS access db. I have the need to make some reports extracting data...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
3
by: Prakash Wadhwani | last post by:
Here is a code snippet in my Report: ** Snippet starts here ************************ Private Sub GroupHeader0_Format(Cancel As Integer, FormatCount As Integer) Dim sSQL As String, strSQL0 As...
5
by: steve_marjoribanks | last post by:
This isn't strictly an XML problem but I thought someone might be able to help! As part of my degree I am working on a new data format for use in the geotechnical engineering domain. The data...
3
by: suresh Chowdary | last post by:
Hi everyone, i ahve the one table with 2 not null fileds ,in this one column accept certain values (1,2,3), and another column will accept (1m,2m,3m). the problem is when the data extracting...
1
by: napolpie | last post by:
----Messaggio originale---- Da: napolpie@tin.it Data: 3-mag-2007 10.02 A: <python-list@python.org> Ogg: problem with meteo datas Hello, I'm Peter and I'm new in python codying and I'm using...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
4
by: poolboi | last post by:
hi guys i've having some problem extracting data from a text file example if i got a text file with infos like: Date 2008-05-01 Time 22-10 Date 2008-05-01 Time 21-00 Date 2008-05-02 Time...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.