By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

using variables from form

P: n/a
Phi
Hi there,

I've just started learning php and I hope you guys could help me with
this problem. I've found a useful pice of code from a book to make a form
and I would like to use it. I would like to print the variables (input)
on the screen , but I can't make it work. I think I have to do something
with an array, but don't know what. I've tried

$field = $_POST['field'];
echo $field;

and also

$arr = $_POST['array'];
print_r($arr);

but it doesn't work.

Could some one show me what I'm missing?

Thanks a bunch!
Phi

########################### code ###################################

<?php

// new function
echo "<br><br><h4>second function</h4>\n";

function print_form($action="", $method="post")
{
if (empty($action)){return FALSE;}
echo "<form action=$action method=$method>\n";
$numargs = func_num_args();
for($i = 2; $i < $numargs; $i++)
{
echo "$i <input type = text name =". func_get_args($i) . ">
<br>\n";
}
echo "<input type=submit>";

echo "</form>";
}
print_form("autoform2.php", "", "field", "field2", "field3", "field4");
// testing if the variable exist...and it doesn't...
if(isset($_POST['field'])){
echo "exist";
}
else {
echo "does not exist!";
}

?>

########################### end code ##################################
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Phi wrote:
Hi there,

I've just started learning php and I hope you guys could help me with
this problem. I've found a useful pice of code from a book to make a form
and I would like to use it. I would like to print the variables (input)
on the screen , but I can't make it work. I think I have to do something
with an array, but don't know what. I've tried

$field = $_POST['field'];
echo $field;

and also

$arr = $_POST['array'];
print_r($arr);

but it doesn't work.

Could some one show me what I'm missing?

Thanks a bunch!

try to make a phpinfo(); on the page you submit to and see if the
variables are there (almost at the bottom).

--
Henrik Hansen
Jul 17 '05 #2

P: n/a
Henrik Hansen wrote:
$arr = $_POST['array'];
print_r($arr);


print_r($_POST); ??

Aphrael...
--
"La demande mondiale d’ordinateurs n’excédera pas cinq machines."
(Thomas Watson, Fondateur d'IBM, 1945)
Jul 17 '05 #3

P: n/a
Phi
Henrik Hansen <hh****@fsck.dk> wrote in
news:R7********************@news000.worldonline.dk :
Phi wrote:
Hi there,

I've just started learning php and I hope you guys could help me with
this problem. I've found a useful pice of code from a book to make a
form and I would like to use it. I would like to print the variables
(input) on the screen , but I can't make it work. I think I have to
do something with an array, but don't know what. I've tried

$field = $_POST['field'];
echo $field;

and also

$arr = $_POST['array'];
print_r($arr);

but it doesn't work.

Could some one show me what I'm missing?

Thanks a bunch!

try to make a phpinfo(); on the page you submit to and see if the
variables are there (almost at the bottom).


I don't know if it's this what you mean. Thanks for your time again...
phpinfo();
################################################## #############
_REQUEST["Array"] test test test
_GET["Array"] test test test
_SERVER["HTTP_HOST"] localhost
_SERVER["HTTP_USER_AGENT"] Mozilla/5.0 (Windows; U; Windows NT 5.2; en-
US; rv:1.7) Gecko/20040626 Firefox/0.8
_SERVER["HTTP_ACCEPT"] application/x-shockwave-
flash,text/xml,application/xml,application/xhtml+xml,text/html;q=
0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/
*;q=0.1
_SERVER["HTTP_ACCEPT_LANGUAGE"] en-us,en;q=0.5
_SERVER["HTTP_ACCEPT_ENCODING"] gzip,deflate
_SERVER["HTTP_ACCEPT_CHARSET"] ISO-8859-1,utf-8;q=0.7,*;q=0.7
_SERVER["HTTP_KEEP_ALIVE"] 300
_SERVER["HTTP_CONNECTION"] keep-alive
_SERVER["PATH"] C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\Sys tem32
\\Wbem;C:\\Program Files\\ATI Technologies\\ATI Control Panel;C:\\Program
Files\\Microsoft SQL Server\\80\\Tools\\BINN
_SERVER["SystemRoot"] C:\\WINDOWS
_SERVER["COMSPEC"] C:\\WINDOWS\\system32\\cmd.exe
_SERVER["PATHEXT"] .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
_SERVER["WINDIR"] C:\\WINDOWS
_SERVER["SERVER_SIGNATURE"] <address>Apache/2.0.50 (Win32) PHP/4.3.7
Server at localhost Port 80</address>
_SERVER["SERVER_SOFTWARE"] Apache/2.0.50 (Win32) PHP/4.3.7
_SERVER["SERVER_NAME"] localhost
_SERVER["SERVER_ADDR"] 127.0.0.1
_SERVER["SERVER_PORT"] 80
_SERVER["REMOTE_ADDR"] 127.0.0.1
_SERVER["DOCUMENT_ROOT"] Z:/www/apache
_SERVER["SERVER_ADMIN"] wh*********@nomail.com
_SERVER["SCRIPT_FILENAME"] Z:/www/apache/_faq/func/autoform2.php
_SERVER["REMOTE_PORT"] 2168
_SERVER["GATEWAY_INTERFACE"] CGI/1.1
_SERVER["SERVER_PROTOCOL"] HTTP/1.1
_SERVER["REQUEST_METHOD"] GET
_SERVER["QUERY_STRING"]
Array=test+test+test&Array=test+test+test&Array=te st+test+test&Array
=test+test+test&Array=test+test+test
_SERVER["REQUEST_URI"] /_faq/func/autoform2.php?
Array=test+test+test&Array=test+test+test&Array=te st+test+test&Array=test
+test+test&Array=test+test+test
_SERVER["SCRIPT_NAME"] /_faq/func/autoform2.php
_SERVER["PHP_SELF"] /_faq/func/autoform2.php
_SERVER["PATH_TRANSLATED"] Z:/www/apache/_faq/func/autoform2.php
_SERVER["argv"]

Array
(
[0] => Array=test
[1] => test
[2] => test&Array=test
[3] => test
[4] => test&Array=test
[5] => test
[6] => test&Array=test
[7] => test
[8] => test&Array=test
[9] => test
[10] => test
)

_SERVER["argc"] 11
_ENV["ALLUSERSPROFILE"] C:\\Documents and Settings\\All Users
_ENV["ClusterLog"] C:\\WINDOWS\\Cluster\\cluster.log
_ENV["CommonProgramFiles"] C:\\Program Files\\Common Files
_ENV["COMPUTERNAME"] TOWER
_ENV["ComSpec"] C:\\WINDOWS\\system32\\cmd.exe
_ENV["INCLUDE"] C:\\Program Files\\Microsoft Visual Studio .NET 2003\\SDK
\\v1.1\\include\\
_ENV["LIB"] C:\\Program Files\\Microsoft Visual Studio .NET 2003\\SDK
\\v1.1\\Lib\\
_ENV["NUMBER_OF_PROCESSORS"] 2
_ENV["OS"] Windows_NT
_ENV["Path"] C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\Sys tem32
\\Wbem;C:\\Program Files\\ATI Technologies\\ATI Control Panel;C:\\Program
Files\\Microsoft SQL Server\\80\\Tools\\BINN
_ENV["PATHEXT"] .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
_ENV["PROCESSOR_ARCHITECTURE"] x86
_ENV["PROCESSOR_IDENTIFIER"] x86 Family 6 Model 8 Stepping 3,
GenuineIntel
_ENV["PROCESSOR_LEVEL"] 6
_ENV["PROCESSOR_REVISION"] 0803
_ENV["ProgramFiles"] C:\\Program Files
_ENV["SystemDrive"] C:
_ENV["SystemRoot"] C:\\WINDOWS
_ENV["TEMP"] C:\\WINDOWS\\TEMP
_ENV["TMP"] C:\\WINDOWS\\TEMP
_ENV["USERPROFILE"] C:\\Documents and Settings\\NetworkService
_ENV["VS71COMNTOOLS"] C:\\Program Files\\Microsoft Visual Studio .NET 2003
\\Common7\\Tools\\
_ENV["windir"] C:\\WINDOWS
_ENV["AP_PARENT_PID"] 1384
################################################## #############
Jul 17 '05 #4

P: n/a
On 23 Jul 2004 11:53:39 GMT, Phi <ph*******@hotmail.com(remove)>
wrote:
Could some one show me what I'm missing?


1. you forgot to pass "post" to your function:

print_form("autoform2.php", "post", "field", "field2", "field3",
"field4");

would work with your script.

2. the result from func_get_args() is an array, you you have to change

echo "$i <input type = text name =". func_get_args($i) . ">

to this:

$fields= func_get_args();
echo "$i <input type = text name =". $fields[$i] . ">

With these changes your code works fine.

Regards

Marian

--
Tipps und Tricks zu PHP, Coaching und Projektbetreuung
http://www.heddesheimer.de/coaching/
Jul 17 '05 #5

P: n/a
Phi wrote:
Henrik Hansen <hh****@fsck.dk> wrote in
news:R7********************@news000.worldonline.dk :

Phi wrote:
Hi there,

I've just started learning php and I hope you guys could help me with
this problem. I've found a useful pice of code from a book to make a
form and I would like to use it. I would like to print the variables
(input) on the screen , but I can't make it work. I think I have to
do something with an array, but don't know what. I've tried

$field = $_POST['field'];
echo $field;

and also

$arr = $_POST['array'];
print_r($arr);

but it doesn't work.

Could some one show me what I'm missing?

Thanks a bunch!

try to make a phpinfo(); on the page you submit to and see if the
variables are there (almost at the bottom).

as you can see in your phpinfo() the variables are in _GET not in _POST,
so use _GET not _POST or change your submit method in the form.

--
Henrik Hansen
Jul 17 '05 #6

P: n/a
Phi
Marian Heddesheimer <22*************@spamgourmet.com> wrote in
news:m6********************************@4ax.com:
On 23 Jul 2004 11:53:39 GMT, Phi <ph*******@hotmail.com(remove)>
wrote:
Could some one show me what I'm missing?


1. you forgot to pass "post" to your function:

print_form("autoform2.php", "post", "field", "field2", "field3",
"field4");

would work with your script.

2. the result from func_get_args() is an array, you you have to change

echo "$i <input type = text name =". func_get_args($i) . ">

to this:

$fields= func_get_args();
echo "$i <input type = text name =". $fields[$i] . ">

With these changes your code works fine.

Regards

Marian


Thanks, your tips did the trick. I changed it and it worked! Now I have a
much better understanding of how the script and certain functions work.

Phi

##############code##########
/// new function
echo "<br><br><h4>second function</h4>\n";

function print_form($action="", $method="POST")
{
if (empty($action)){return FALSE;}
echo "<form action=$action method=$method>\n";
$numargs = func_num_args();
for($i = 2; $i < $numargs; $i++)
{
$fields= func_get_args();
echo $fields[$i]. "<input type = text name =".$fields[$i]. ">
<br>\n";

}
echo "<input type=submit>";

echo "</form>";
}
print_form("autoform2_2.php", "post", "field", "field2", "field3",
"field4");

//test the variables

$field = $_POST['field'];
echo $field;

$field2 = $_POST['field2'];
echo $field2;
Jul 17 '05 #7

P: n/a
Phi
Henrik Hansen <hh****@fsck.dk> wrote in news:r78Mc.27060$Vf.1476102
@news000.worldonline.dk:
Phi wrote:
Henrik Hansen <hh****@fsck.dk> wrote in
news:R7********************@news000.worldonline.dk :

as you can see in your phpinfo() the variables are in _GET not in _POST,
so use _GET not _POST or change your submit method in the form.


Thank you for your help. It works fine now with Marian's tips.
Thank for you tip to use phpinfo(). I'll make use of it more often.

Kind regards...

Phi
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.