Jump to content


Photo

posting user input as a variable


  • Please log in to reply
7 replies to this topic

#1 meckr

meckr
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 June 2006 - 02:52 PM

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
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);

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


And what is more, I agree with everything I have just said!

Education is what remains after one has forgotten everything he learned in school. --Albert Einstein.

www.tspadvantage.com

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 21 June 2006 - 02:59 PM

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


wisewood: proven fact, I am both wise, and wooden.

#3 meckr

meckr
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 June 2006 - 03:03 PM

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
And what is more, I agree with everything I have just said!

Education is what remains after one has forgotten everything he learned in school. --Albert Einstein.

www.tspadvantage.com

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 June 2006 - 03:10 PM

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?

#5 meckr

meckr
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 June 2006 - 03:14 PM

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.


And what is more, I agree with everything I have just said!

Education is what remains after one has forgotten everything he learned in school. --Albert Einstein.

www.tspadvantage.com

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 June 2006 - 03:20 PM

Still need a form even if its a textarea. To autopupulate the textarea when the data is sent you can use this:
<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>


#7 meckr

meckr
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 June 2006 - 11:56 PM

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?

And what is more, I agree with everything I have just said!

Education is what remains after one has forgotten everything he learned in school. --Albert Einstein.

www.tspadvantage.com

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 June 2006 - 10:20 AM

Something like this:
<?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>
Thats does what you want, create the file, set the variable and rename the file.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users