Link to home
Start Free TrialLog in
Avatar of shawn857
shawn857

asked on

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
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In this statement, try changing $_GET to $_POST so that the request method="post" matches:

if ($_GET["function"] == "getcode") {
Avatar of shawn857
shawn857

ASKER

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
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
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
ASKER CERTIFIED SOLUTION
Avatar of Brian Tao
Brian Tao
Flag of Taiwan, Province of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Brian, that worked... and Ray for your contribution also.

Thanks!
    Shawn
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:
https://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
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