Jump to content

Run code typed directly into a textbox.


Recommended Posts

I have a textbox and a submit button.  It's easy to make an echo so whatever is typed in the textbox, echos under the textbox.  I want to do something a little more complicated.  I want to run the php code that is entered in the textbox.  For example if I put this in the textbox <?php echo"Hello World";?> or if I put an if statement in the textbox I want it to run it.  I am trying to learn php and it would be great if I could just input code into a textbox to see the result rather than have to upload the file to the FTP server every time.  Sometimes I'm not at my home computer and I want to sample some code I read on the php manual. 

Question:  Is it possible to run php code that is typed into a textbox?

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Learn PHP</title>
</head>
<body>
<form method="post">
    <input name="txtTextbox" style="width: 500px" type="text"><br>
    <?php
    $txtTextbox = $_POST['txtTextbox'];
    $search = $_POST['btnSubmit'];
    if ($search)
        {
         // Here's the part I need help on.
         echo '<p>';?>
         $txtTextbox;
   <?php echo '</p>';
         // Assume I typed Hello World <?php echo $txtTextbox;?> in the textbox
        }
    echo'<br>';
    ?>
    <input name="btnSubmit" type="submit" value="submit">
</form>
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/277796-run-code-typed-directly-into-a-textbox/
Share on other sites

You can but it is a huge security risk on a publicly accessible server. I would suggest installing WAMP, MAMP, or similar on your local machine. No uploads, it is easy to install, speeds development, and you don't have a server on the net that can be easily hacked.

 

?AMP (W for windows, M for Mac) will put Apache (webserver), MySQL, and PHP on you local machine.

Yes,its possible.Below is the code for it:

<html>
<head>
<title>Execute PHP Code</title>
</head>
<body>
<form action="" method="post">
<textarea name="code" cols="50" rows="20"></textarea><br>
<input type="submit" name="submit" value="Execute">
</form>
<?php
if (isset($_POST['submit'])){
$code = $_POST['code'];
eval("?> $code <?php ");
}
?>
</body>
</html>

Hope it helps you! :happy-04:

Thanks for the code.  It didn't work though but I modified it to work.  Here is the code that worked for me for anyone else who reads this and wants to do this.  Note: you should not do this.  It's a security risk.  I'm doing it because I have nothing on my server space and I'm learning php with it so I don't have to keep uploading the file to the server.

<html><head><title>Execute PHP Code In A Textbox</title></head><body>
<form action="" method="post">
<textarea name="code" cols="50" rows="20"></textarea><br>
<input type="submit" name="submit" value="Execute">
</form>
<?php
if (isset($_POST['submit'])){
$code = $_POST['code'];
eval($code);
}
?>
</body></html>

You're Welcome! I know that its not good if you're using it for your live sites as anyone can execute his bad codes & can harm your server.You said on your post that you're testing it on local machine.So,offcose you will not harm your machine by executing bad codes.I tested it & it works Good! can I know issues you're getting while executing mine code.Your code was Good too,but you only remove php tags from the eval.Its still same & will give same output too.I really appreciate that it helps you! ;D

Archived

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

×
×
  • 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.