Forum Moderators: coopster

Message Too Old, No Replies

Form Handling

Embarrasingly Simple Question

         

inuwolf

3:09 am on Feb 4, 2006 (gmt 0)

10+ Year Member



I'm trying feebly to link three files, (1) an HTML form that submits data to (2) a PHP file (data.php) that submits the data to (3) a CSV file. The problem is between (1) and (2). (2) requires the form data be sent to it like this:

[...data.php?n=DATA&1=first_item_to_save&2=second_item&3=third...] etc

where the first variable being saved in the first column, second variable in the second column and so on. It is however assumed that the first variable supplied will be specifying the datafile number and the variable will be called 'n'.

I have a form, but I don't know how to output its data into the hyperlinked string (2) wants. This is a sample of what my form looks like:

<form name="add" method="POST" action="data.php">

And the options:

<select name="q[1]" id="q[1]" class="fieldstyle">
<option value="a">a</option>
<option value="b">b</option>
...

I have very little experience, so I'm not sure how to connect all this. What am I doing wrong? How do I get my form's info into hyperlinked string that my php file requires? Thanks. I appreciate all the help I've gotten here.

StupidScript

4:19 am on Feb 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hiya, inuwolf,

What are you trying to do and what result are you currently experiencing?

The first issue is with the names of your array elements. While it may seem convenient to use simple numerals for the names (which it is), it kinda complicates things, later on.

Use a naming structure of at least two characters, like

q['q1']
, and be sure to tell the array construct that you are using a string by enclosing the name in some sort of delimiter ... preferably a set of apostrophes, as above.

Why not use a slightly longer, weirder array name, too, to avoid possible confusion with reserved variables? Maybe

qry['q1']
or something like that? Fact is,
q
wouldn't impact on any reserved variables, but ... can't hoit ...

Aside from the above-mentioned potential issues, on the page that collects the information, you'll want to refer to the array that is passing the data. In the example you noted, there are two arrays you can work with; the

$_SERVER['QUERY_STRING']
array and the
$_GET['q1']
array.

On the reception page, gather the data from the URI by referencing either of those two arrays, and then

echo
(or
print()
) the result.

inuwolf

4:51 am on Feb 4, 2006 (gmt 0)

10+ Year Member



StupidScript, thanks for your help. Let me clarify what I am doing and where the immediate problem is.

I'm not having a problem between (2) and (3)--if I paste a url like [...data.php?n=DATA&1=first_item_to_save......] into the browser the data ends up in my CSV as I'd like it to. The problem I'm having is generating that URL in the first place from the form data. I'm currently POSTing the form data to the data.php file and, although it tells me the data was sent successfully, all my table shows is "Array Submit" in the first column--the specific data was not added to the relevant columns.

My reception page is very simple:

<?php
$data[0] = "DataFile.csv";

if(!$fp = fopen($data[$_REQUEST['n']], "a")) die ("Cannot open data file");

array_shift($_REQUEST);
$size = sizeof($_REQUEST);
for($i=0; $i<$size; $i++) {
$str = $str . array_shift($_REQUEST);
if(!($i==($size-1))) {
$str = $str . ", ";
}
}
fwrite($fp, $str . "\n\r");
echo "<p>Thank You</p><p>The data was successfully saved.</p>";
?>

I have very limited knowledge of PHP, so I usually just work with premade scripts, as is the one above. But I made the form myself, so I'm guessing that's where the problem is (I'm also reluctant to change the premade script). The form is simple so I won't bother pasting it, but I suspect there is something wrong with the line <form name="add" method="POST" action="data.php">. Is that enough to POST the needed information to the php file? If not, how should I go about generating the URL from the form data?

Thanks for your help so far.

a1call

6:04 am on Feb 4, 2006 (gmt 0)

10+ Year Member



Hi inuwolf,
If you would like the form inputs to end up as URL encodes like you have mentioned you shoud use the get method and not the post. However the post data are available to the php program as well in the array $_POST
you can use it as $_POST[0], $_POST[1], ...
as well as $_POST['n'],...
If you rather use the get method the data are available in the array $_GET

inuwolf

12:32 pm on Feb 4, 2006 (gmt 0)

10+ Year Member



thanks a1call, I get a url now. but it looks strange:
[...data.php?q%5B1%5D=1st+Item+to+Save&q%5B2%5D=2nd+Item+to+Save...] etc
I want it to look like:
[data.php?n=0&1=first_item_to_save&2=second_item&3=third...] etc

This leads to a number of small questions, like how do I make "+" becomes underscores, how do I insert "n=0" to the beginning of the URL no matter what data is entered, and what do all the redundant "5B1%5D"s in my current URL mean?

Thanks for your help so far.

a1call

2:51 pm on Feb 4, 2006 (gmt 0)

10+ Year Member



Hi inuwolf,
The strange characters in the URL are inserted by your browser in place of invalid characters in the URL. Type
[example...] .com
in the address bar and press enter. It will change to:
[example%20.com...]
Space is an invalid character.
To avoid it make sure there are no invalid characters in the form input names and values.
It usually also works when you convert these back to the original characters in the sever side program like php.

To have
n=0
in the beginning of the query string(the part after "?")
The forms first input should have
name="n"
regardless of the input type.
FOR your exact URL you can method post the form with
ACTION="http://data.php?n=0&1=first_item_to_save&2=second_item&3=third"

You can pass this using a JavaScript global variable which you have made it to be equal to the above URL.

a1call

5:22 pm on Feb 4, 2006 (gmt 0)

10+ Year Member



Hi again,
I think it's worth clarifying a misconception about forms that I had when I started out with forms.
The form's input data does not have to appear in the URL in order to be passed to the sever side program. In a posted form the input is passed without being URL encoded.

inuwolf

8:19 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



a1call, thanks. you were right, the weird characters were coming from invalid characters in the option names. I changed them and, like you said, added the initial value of n=0 and now the form works. now that I (somewhat) know what I am doing I might work on a better/more ambitious form.