Jump to content

Run code typed directly into a textbox.


Go to solution Solved by RonnieCosta50,

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>
Edited by RonnieCosta50
Link to comment
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.

Edited by davidannis
Link to comment
Share on other sites

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:

Link to comment
Share on other sites

  • Solution

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>
Edited by RonnieCosta50
Link to comment
Share on other sites

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

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.