Jump to content

posting user input as a variable


meckr

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\" /]

Link to comment
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]";
}
?>

Link to comment
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]
Link to comment
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?
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.