Jump to content

Archived

This topic is now archived and is closed to further replies.

meckr

posting user input as a variable

Recommended Posts

OK, I am trying to have one script prompt a user for a string (lets say a cd key) and have the user place it in a textbox as a variable. Then upon submitting the form, the script creates another php file with the user provided data placed inside the new php file.

Here is my code for creating the new php file with the user data
[code]if (file_exists ("data.php")) { @unlink ("inc/data.php"); }

   $read=@fopen ("blank_data.php", "r");
   $result=@fread ($read, 1024);
   @fclose ($read);

   $result=str_replace ("{{userdata}}", $userdata, $result);
  

   $read=fopen ("data.php", "w");
   fwrite ($read, $result);
   fclose ($read);
  
   chmod ("data.php", 0755);[/code]

Now, my only real problem is, how do I create a textbox within a form and make whatever is placed inside the textbox a variable that can be posted to the new php file?

I was thinking of something like (and it doesn't have to be inside a form necessarily)
[code]<td width='25%' align='left' valign='top'>Enter data:</td>
<td width='75%' align='left' valign='middle'><textarea name='license' rows='3' cols='59'>".((isset($data))?"".$data."":"")."</textarea></td>[/code]
If that makes any sense at all.

Any help would be so much appreciated, I might just offer an award.

[img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]

Share this post


Link to post
Share on other sites
Should get you started.

<?php
if!($_POST)
{
echo "<form method=post action=$PHP_SELF";
echo "<input type=text name=cd_key>";
echo "<input type=submit value=submit";
echo "</form";
}
else
{
echo "The cd_key you input was: $_POST[cd_key]";
}
?>

Share this post


Link to post
Share on other sites
I would like to avoid a form if at all possible. i know this can be done as I know of other programs that use this type of code. I just can't figur out how it is done

Share this post


Link to post
Share on other sites
What! You got to use a form in order to send the information to your script! How else is the information going to be sent to your script in order to be processed?

Share this post


Link to post
Share on other sites
sorry I missunderstood your post. I meant to say without a text box but within a text area and I guess it would still be the same only textarea instead of text.

Share this post


Link to post
Share on other sites
Still need a form even if its a textarea. To autopupulate the textarea when the data is sent you can use this:
[code]<td width='25%' align='left' valign='top'>Enter data:</td>
<td width='75%' align='left' valign='middle'><textarea name='license' rows='3' cols='59'><?php echo ((isset($data) ? $data : ''); ?></textarea></td>[/code]

Share this post


Link to post
Share on other sites
Thanks for your help wildteen88 but It seems it won't work. I have tried it numerous times without success.

So let me reiterate.

I have a textarea that a user will use to place a license key into. Afterwhich the user, will hit the submit button. Then, a file called install_license.php and will be written to by placing the license key the user provided, into the a variable within intall_license.php. Then install_license.php will be renamed to license.php.

How can this be accomplished. I have tried using my code above but i cannot get it to work. I have checked the permissions on install_license.php and they are set to 777.

Now, a differnet method that would work just as well, is if I could somehow figure out how to insert the user provided license (from the textarea) into a db.

Any help would be greatly appreciated. I just cannot figure it out for the life of me. I know this is simple stuff but I'm just racking my brain.

Please assist?

Share this post


Link to post
Share on other sites
Something like this:
[code]<?php

// check that the user has submitted the form
if (isset($_POST['submit']))
{
    // open install_license.php
    $file = 'install_license.php';
    $handle = fopen ($file, 'w');

    $contents = "<?php\n\n\$key = \"" . $_POST['license'] . "\"\n\n?>";

    // write contents to file:
    fwrite ($handle, $contents);

    // close the file
    fclose ($handle);

    // attempt to rename the file:
    if(!file_exists ('license.php'))
    {
        // rename install_license.php
        rename ($file, 'license.php');
    }
    else
    {
        echo "Unabel to rename install_license.php as license.php already exists! Please delete license.php before continuing";
    }

    echo "License added and file has been renamed!<br /><br />";
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <b>License Key:</b><br />
  <textarea name="license" cols="20" rows="5"><?php echo (isset($_POST['license']) ? $_POST['license'] : ''); ?></textarea><br />
  <input type="submit" name="submit" value="Add license" />
</form>[/code]
Thats does what you want, create the file, set the variable and rename the file.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.