Forum Moderators: coopster
[...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.
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.
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.
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.
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.