HTML form not passing variables to PHP function

Hi, I have a pretty simple HTML form that accepts 2 text values and upon clicking 'Submit', I want to pass those two variables to a PHP function that manipulates them by calling an EXE. I don't know much PHP however and I'm basically just using the code from an example in the User's Manual for the exe. Anyway, my problem is - the variables aren't getting passed properly to the PHP function, as I get an "Undefined variable" error message when I run the script.

I'll spare the gory details of my whole index.php file, but the pertinent sections are:

 
<table class="form" cellspacing="1" cellpadding="1">
<tr><td class="title" colspan="2"><center><font face="arial"><b>Generate DEMO activation code</b></font></center></td></tr>
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]."?function=getcode"; ?>">
<tr><td class="title"><span class="small">Name</span></td><td class="title"><input type="text" name="n" size="70" class="text"></td></tr>
<tr><td class="title"><span class="small">User Data</span></td><td class="title"><input type="text" name="u" size="70" class="text"></td></tr>
<tr><td class="background" colspan="2" align="center"><input type="submit" value="Generate"></td></tr>
</form>
</table>

Open in new window



...and


<?php
if ($_GET["function"] == "getcode") {

echo $n;

$progname = "XXXXXXXXXXX.exe ";
$programcode = "My program code";
$userdata = "ED3E78BDB99C15A36DB6746B9966B6ECCFF62E8202EEB9956DC83B7F15C4AF00";
$nameofuser = "SampleUsername";
$fname = "regcode" . $nameofuser . ".txt";
$sixtydays = "1";
$runcmd = $progname . '"' . $programcode . '|' . $userdata . '|' . $nameofuser . '|' . $fname . '|' . $sixtydays . '"'; 

shell_exec($runcmd);
echo nl2br(file_get_contents($fname));

}  
?>

Open in new window



The "Undefined Variable" error comes on line "echo $n;". 'n' is one of the two text fields in my form and the value is apparently not getting passed on properly to the function. Can anybody see what the problem would be?

Thanks!
    Shawn
shawn857Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
$_GET and $_POST are different variables.  You want to choose one or the other. These relate to the method= attribute in the form tag

PHP has online information about this sort of thing, starting here:
http://php.net/manual/en/tutorial.php

If you're new to PHP and want to learn the language:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
Ray PaseurCommented:
In this statement, try changing $_GET to $_POST so that the request method="post" matches:

if ($_GET["function"] == "getcode") {
0
shawn857Author Commented:
Thanks Ray, so you're saying my FORM statement should instead be:

<form method="GET" action="<?php echo $_SERVER["PHP_SELF"]."?function=getcode"; ?>">

Open in new window


That would work?

Thanks
   Shawn
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

shawn857Author Commented:
Ray, changing _GET to _POST as you suggested results in this error:

Notice: Undefined index: function in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\index.php on line 22

Thanks
    Shawn
0
shawn857Author Commented:
Ray, now I tried having GET both in my FORM statement:

<form method="GET"

 and in my function:

if ($_GET["function"] == "getcode")

That brings back the "Undefined Variable" error I originally had... :-(

Thanks
    Shawn
0
Brian TaoSenior Business Solutions ConsultantCommented:
There is nothing wrong for the line about $_GET["function"] and the method in your HTML form.
To get $n, you also have to use the same way you get the function parameter.  If you keep your form method as "POST", use $_POST["n"] in you php program, so by adding
$n = $_POST["n"];

Open in new window

before line #4, your code should be running fine.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shawn857Author Commented:
Thank you Brian, that worked... and Ray for your contribution also.

Thanks!
    Shawn
0
Ray PaseurCommented:
Shawn: This is a wasted statement.  All it does is proliferate a variable, making no new or useful value from it.  Don't do that - just use the original variable.  Or better yet, transform the original variable in the external $_POST array by running it through a sanitize filter so that you know it contains a known good value that is safe for use in your script.
$n = $_POST["n"];

Open in new window

If you're new to PHP and want to learn the language:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

If you want to learn how to use HTML forms with PHP, check here:
http://php.net/manual/en/tutorial.php
0
shawn857Author Commented:
Couldn't get it working that way Ray... Brian's proposed solution works so I went with that! Don't have a lot of available time to read all about PHP - just needed to get my little thing working and press on...

Thanks
    Shawn
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.