Run run php code client side
We know that php is a server side script.It run to server side.You have seen some website run php code from browser means from user end.So how will you do this?It is very easy to implement.Just you have keep knowledge on jquey and some php function.Basically here we are using jquery ajax for that to access the server side php script.You know if we use ajax then we don’t need to reload the whole page to access the script.Then we can run php code from client side very easily.Most of the website now using this technique.
First of all create two page one index.php and result.php .
Then add jquery library (CDN) in index.php.
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
Then create a textarea and submit button and for output take paragraph tag using HTML.
Client side script
<label> <?php echo htmlentities('<?php'); ?> </label><br /> <textarea rows="20" name="code" cols="80" id="code"></textarea> <?php echo htmlentities('?>'); ?><br /> <button type="submit" name="sub" onclick="x()">Submit</button> <p id="output"></p>
Now define the onclick function.
<script> function x() { var da=$('#code').val(); $.ajax({ url: 'result.php', data: {'da' :da}, type:'POST', success: function(result){ $('#output').html(result); }, error: function(error){ console.log(error.responseText); } }); } </script>
Here ajax has five parameter.
1> url: In this parameter we are setting php file where we keep the php script.
2> data: In this parameter we are passing the data from client side to php script.
3> type: In the parameter we are defining the method.
4> success: if ajax method run successful then this function will trigger and you can get return data in result parameter.
5> error: if there is an error in server side this function will trigger.
Now use jquery ajax to get the output of from result.php and set the output to the paragraph tag using #output id selector.
So add the following code to result.php
<?php if(isset($_REQUEST['da'])) { $r=$_REQUEST['da']; echo eval($r); } ?>
That all.Now you can run your php code to browser.