473,503 Members | 1,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

newby trying to use GET to return variable to script in same page

I have a simple html form and a bit of PHP in the same page, and I
want the script to get the entry from the form, and write it to a
file. I dont think I'm allowed to use POST on my webspace. Here is my
code ;

<p>&nbsp;
<form method="GET">
Type in a groovy word and press enter<input type="text"
name="groovyword">
<br>

</form></p>
<?php

// ** file created on 12/03/2005 17:18:03 **
// ** file created by Charlie Fortune
// ** Funk Name Generator

function addname ($word){
if (!file_exists("Funk name elements.txt")) touch ("Funk name
elements.txt") ;
$fp=fopen("Funk name elements.txt",'a');
fwrite($fp,"$word\r\n");
fclose($fp);
}
if (isset($groovyword))
addname($groovyword);
?>

what am I doing wrong ? I can see the entry in the url, but nothing is
being written to the file..
Jul 17 '05 #1
3 1543
On 12 Mar 2005 16:05:11 -0800, go****@charliefortune.com (charlie fortune)
wrote:
I have a simple html form and a bit of PHP in the same page, and I
want the script to get the entry from the form, and write it to a
file. I dont think I'm allowed to use POST on my webspace.
That would be a cruel and unusual restriction. What makes you think that?
Here is my code ;

<p>&nbsp;
<form method="GET">
Type in a groovy word and press enter<input type="text"
name="groovyword">
<br>

</form></p>
You haven't got a submit button, which puts you at the mercy of the browser
deciding to submit when you press enter or similar. Add a submit button.
<?php
// ** file created on 12/03/2005 17:18:03 **
// ** file created by Charlie Fortune
// ** Funk Name Generator

function addname ($word){
if (!file_exists("Funk name elements.txt")) touch ("Funk name
elements.txt") ;
$fp=fopen("Funk name elements.txt",'a');
You can remove the 'touch' call if you use mode 'a+' (append if exists, create
if not).

Filenames with spaces cause pain in various situations, consider replacing
spaces with underscores or similar. This isn't required, just a suggestion.
fwrite($fp,"$word\r\n");
It's up to you what line endings you use, but Unix-style plain "\n" is
generally preferred over Windows-style "\r\n".
fclose($fp);
} if (isset($groovyword))
addname($groovyword);


There is nothing in this script that will set $groovyword unless you are using
either a very old version of PHP, or a newer version with the register_globals
option set to true, which is strongly recommended to be set to false.

Change these lines to:

if (isset($_GET['groovyword']))
addname($_GET['$groovyword']);

Another issue to consider; what if two people run this script at the exact
same time? You might want to look at http://uk.php.net/flock . Or even move up
a level and use a database; handling concurrent access is one of the several
fundamental reasons to use a proper database.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
Thanks for your reply, Andy, it's very helpful. I'm still not quite
there, though.

When I tried to "POST" i got an Error 405 POST method not permitted,
so that's why I thought I would have to use "GET". I am following this
up with my hosts.

My script is now as follows, however I know that it isn't calling the
function, because the .txt file isn't being created.

<p>&nbsp;
<form method="GET">
Type in a groovy word<input type="text" name="groovyword">
<br>
<input type="submit" value="file it">
</form></p>
<?php

// ** file created on 12/03/2005 17:18:03 **
// ** file created by Charlie Fortune
// ** Funk Name Generator

function addname ($word){
$fp=fopen("Funk_name_elements.txt",'a+');
fwrite($fp,"$word\n");
fclose($fp);
}
if(isset($_GET["groovyword"]))
addname($_GET["groovyword"]);
?>
Originally, I had the php as a separate file on the server, in the
same directory,and called it with <form action="funk_filer.php"> and
it worked fine except for the fact that it went to a new blank page
every time it filed an entry. I want it to regenerate the same page
with an empty field. (I would also like to conquer the returning
values to the same page concept). What is the way around this ?

Have I got this right ; a GET or POST method creates an associative
array with the name part of the element as a key ? Even with
register_globals off ?

Also, in your reply, you put

if(isset($_GET['groovyword']))
addname($_GET['$groovyword']);

with a $ in front of groovyword in addname, but I tried with and
without, and I guessed that the way I have it at the top is correct ?

Anyway, I really appreciate your help. I am reading a book too, but
you can't ask books questions....
Jul 17 '05 #3
charlie fortune wrote:
Thanks for your reply, Andy, it's very helpful. I'm still not quite
there, though.

When I tried to "POST" i got an Error 405 POST method not permitted,
so that's why I thought I would have to use "GET". I am following this
up with my hosts.

My script is now as follows, however I know that it isn't calling the
function, because the .txt file isn't being created.


You are doing two things, a GET and a filesystem operation, and you need to
isolate which one is not working. Methinks you have a filesystem problem.
Try this simple sanity check:

<?php
echo "is this what you typed: ".$_GET["groovyword"];
?>

Which should settle once and for all for you that the variable is getting
through to your server.

With that settled, write yourself a little file:

FILE:Testme.php
<?php
$fp=fopen("Test.txt","a+");
$fwrite($fp,"sanity check");
fclose($fp);
echo "I wrote the file, check now to see if it worked";
?>

And then invoke http://example.com/Testme.php

....which will tell you if you your file commands are working.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2177
by: WindAndWaves | last post by:
Hi Gurus I am keen to make a search page on a website, but I have absolutely zero experience with PHP. I am going to hire an expert, but I thought that it may pay to try it a bit first myself...
2
2305
by: Gill Bates | last post by:
I'm trying to login to a banking site (https://www.providentconnection.com) using vb.net. I've tried many variations of WebClient and HttpWebRequest; none of which I've got to work. My latest...
41
10632
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
12
2912
by: Phil Certain | last post by:
Hi, I'm trying to do something very simple...or at least it should be. I have created a host page (gen.aspx) and a very simple user control (us.ascx). The corresponding code-behind files are...
10
1449
by: John Baker | last post by:
Hi: This is the only ASP newsgroup I can access using Verizon, and I would like to know a real basic piece of information. I am totall new to ASP, although I have done HTML coding. Say I The...
18
2240
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
20
6929
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
11
16889
by: Rob | last post by:
I know, I know, don't use frames. Well, I'm stuck with these frames and I'm trying to add functionality without a complete redsign. You can look at this as a nostalgic journey. Anyway, I've got...
1
3127
by: rsteph | last post by:
I've got some product information pages, with images and text, all setup within a table. I'm trying to add a small image in the upper right hand corner of the content div (where all the important...
0
7203
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
7089
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
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
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
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.