How do I evaluate from a textarea? - php

here's what I tried:
<form method="POST" action="new.php">
<font color="#C0C0C0"><small>It doesn't highlight it but it still works. And feel free to resize it!</small></font><br />
<textarea name="high" class="prettyprint">Insert code here</textarea><br />
<button class="btn btn-large btn-success" type="submit">Debug</button>
</form>
</center>
<h1>Debug Info:</h1>
<pre><?php if(isset($_POST['high'])){
$high = (get_magic_quotes_gpc()) ? stripslashes($_POST['high']) : $_POST['high'];
eval($high);
}?></pre>
I need it to evaluate the code from the textarea so it executes the PHP code (i put echo 'hi' in the textarea and it gives this error:) Parse error: syntax error, unexpected '<' in /home/a3827523/public_html/new.php(44) : eval()'d code on line 1
I don't know what is the problem but can someone help?

(i put echo 'hi' in the textarea and it gives this error:)
because you miss a ";" at the end of line
--
additional:
I tried your code in my environment, got the same error in these two case below:
Case 1 in textarea begin code with php tag:
<?php
echo 'hi';
?>
php manual wrote:
mixed eval ( string $code_str )
The code string to be evaluated. code_str does not have to contain PHP
Opening tags.
Case 2 some syntax error code in textarea:
echo 'hi'
semicolon missing.

Related

Parse error: syntax error, unexpected '<<' (T_SL) on line 48

I have this code, but I'm getting
Parse error: syntax error, unexpected '<<' (T_SL) on line 48
I think the problem is from php code. but, i don't know what is it.
Can someone please explain why this error, or what my script is missing?
Thanks.
<!DOCTYPE html>
<html>
<head>
<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<script>
function updatemenu() {
if (document.getElementById('responsive-menu').checked == true) {
document.getElementById('menu').style.borderBottomRightRadius = '0';
document.getElementById('menu').style.borderBottomLeftRadius = '0';
}else{
document.getElementById('menu').style.borderRadius = '9px';
}
}
</script>
</head>
<body>
<div id="content">
<center><br><br>
<?php
$uid=<<<EOD
ID : $_POST['user_id']
EOD;
$upw=<<<EOD
PW : $_POST['user_pw']
EOD;
echo $uid;
echo $upw;
?>
</center><br><br><br><br>
</div>
</body>
</html>
While you could use HEREDOC it poses an issue when it comes to spaces as another answer already points out. The way that I would approach it is to use something like the following which works and is easier to read:
<center><br><br>
ID: <?php echo $_POST['user_id']; ?>
PW: <?php echo $_POST['user_pw']; ?>
</center>
Reflecting the user's password back to them on the page isn't advised unless you are just getting a feel for how data moves around pages.
I think you have blank space after EOD, when there should be none. Please remove it and retry. Also remove any proceeding blank space before the ending EOD;
Solution: Remove trailing space after ALL <<<EOD and remove preceding whitespace before ALL the terminating EOD;

display div inside php using echo

I want display my button if it have isset($_GET). I am trying to do like this.
<?php if(isset($_GET['project_id'])){
echo '<div class="add_btn_primary"> Project Users </div>';
}?>
its giving me error like
Parse error: syntax error, unexpected 'project_id' (T_STRING), expecting ',' or ';' in C:\xamppp\htdocs\mayank\add_project.php on line 101
I am not getting idea what I should do for echo project_id in div. Let me know if someone can help me for that.
Thanks
Thats incorrect to use echo inside another echo and how can you start a new php tag without closing the first.
The correct way is to concatenate the variable along the string passed in echo, here is how
<?php if(isset($_GET['project_id'])){
echo '<div class="add_btn_primary"> Project Users </div>';
}?>
instead of breaking the php tags break the ' quotes to concatenate the value in the string.
Why do you need again tag inside echo just use it as below:
<?php
if(isset($_GET['project_id']))
{
echo ('<div class="add_btn_primary"><a href="manage_project_users.php?project_id='.$_GET["project_id"].'>Project Users</a></div>');
}
?>

Error durring editing functions.php (Wordpress)

I'm getting the error
Parse error: syntax error, unexpected '}' in /var/www/humanityx/wp-content/themes/wordpress-bootstrap-master/functions.php on line 1127
/* Display the post meta box. */
function smashing_post_class_meta_box( $object, $box ) {
?>
<?php wp_nonce_field( basename( __FILE__ ), 'smashing_post_class_nonce' ); ?>
<p>
<a class="post-btn-blue">Button</a>
<br />
<span class="short-blue">[buttonblue]Link Text[/buttonblue]</span>
<br />
</p>
<?php
}
I run the whole code in PHP Code checker and there is no problem. Also PHPStorm is not viewing any error.
https://pastebin.com/U45wqnpX
I have PHP 7.1
Just my two cents.....
In the function 'smashing_add_post_meta_boxes' you set the callback 'smashing_post_class_meta_box'.
According to the WordPress codex:
$callback
(callable) (Required) Function that fills the box with the desired content. The function should echo its output.
When I look at 'smashing_post_class_meta_box' the function perhaps does not really 'echo' anything and therefore gives unexpected output resulting in the unexpected '}' not picked up by PHPStorm?
Like I said, not sure, could not test, but wanted to help.

PHP variable inside onclick function?

I have a PHP file. In this I have to write html. Also instead of closing PHP tag every time, I just use echo 'html code';
So my code is like:
<?php
$mid = 1;
echo '<div class="message_wrap"><span onclick="viewMessage(1)">Click</span></div>';
?>
<script>
function viewMessage(id){
alert(id);
}
</script>
It gives me 1 in alert but I want to use $mid in onclick function but it breaks my code. I used this:
echo '<div class="message_wrap"><span onclick="viewMessage("'.$mid.'")">Click</span></div>';
but I got nothing on clicking and when I see generated HTML it is breaking like this
<div class="message_wrap"> <span 1")"="" onclick="viewMessage(">Click</span> </div>;
<?php
$mid = 1;
?>
<div class="message_wrap"><span onclick="viewMessage(<?php echo $mid;?>)">Click</span></div>
But I don't want to close and open PHP tag as I have large code, so it does not look good.
How can I get it to work?
It returns "Uncaught SyntaxError: Unexpected token }" error because the parameter you passed has space before it and "="" after ) because of that approach
to fix it change your
echo '<div class="message_wrap"><span onclick="viewMessage("'.$mid.'")">Click</span></div>';
to
echo '<div class="message_wrap"><span onclick="viewMessage(\''.$mid.'\')">Click</span></div>';

Adding custom PHP source into HTML

Need some help to add PHP into HTML. I am trying to embed PHP code snippet in HTML however output is showing the snippet itself in the text box instead of evaluating it.
In HTML (register.php), added the below lines of code
<tr>
<td>
<?php utils::addControl('Name','label'); ?>
</td>
<td>
<?php utils::addControl('username','text'); ?>
</td>
</tr>
Here is the source of utils::addControl()
public function addControl($ctlName, $type) {
switch ($type) {
case 'text':
echo <<<EOT
<input type='text' name='$ctlName' value='&lt?php echo htmlspecialchars($ctlName); ?&gt'>
EOT;
break;
case 'label':
echo "<label for id='lbl$ctlName'>$ctlName</label>";
break;
}
Output is showing <?php echo htmlspecialchars(username); ?> in the text box.
What should be done in order to get the PHP snippet evaluated properly. TIA.
Servers are typically only configured for a single pass over a script while looking for PHP…
Source → PHP → Output
Not a double pass:
Source → PHP → PHP → Output
If it did perform a double pass then you would have to jump through hoops if you wanted to include the sequence <?php in the output (or even <? if short tags were turned on).
Write your PHP so it generates that output you want in the first place, don't try to generate PHP from PHP.
$htmlCtrlName = htmlspecialchars($ctlName);
echo "<input type='text' name='$htmlCtlName' value='$htmlCtrlName'>"

Categories