从输入字段实时更新 PHP 变量



我正在尝试将输入的文本(名称为"motd"(放入我的php脚本($text(。所有堆栈溢出的答案都说要放$text = 'echo $_POST['motd'];'但这不起作用。我的拉拉维尔页面无法加载。

最终,我尝试使用.我在下面包含了大脚本。

控制器:https://github.com/Pterodactyl/Panel/blob/develop/app/Http/Controllers/Server/ServerController.php

谢谢。

<form method="post" accept-charset="utf-8" action="#motd" id="motd">
<!-- motd (server) -->
<div class="form-group">
  <div class="col-xs-12">
     <div class="card card-inverse bg-space-600 m-b-10">
         <div class="card-block">	
           <div class="minecraft" id="motd-preview">
            <?php
            require (app_path().'/Http/Colors.php');
            use SystemHttpColors;
            $text = 'The Text I want to Show';
            echo Colors::convertToHTML($text);
            echo Colors::clean($text);
            ?>
           </div>
         </div>
     </div>
  </div>
</div>
<div class="form-group p-b-35">
  <div class="col-xs-10">
     <label class="font-size-15" class="font-size-15" for="motd">MOTD</label> 	
     <a href="javascript:void(0);" class="breadcrumb-toggle" id="toggle-color-codes">
       <i class="wb-chevron-down-mini"></i>
     </a>						
  </div>
  <div class="col-xs-2"> 
    <span class="pull-right">	
      <input type="textbox" name="motd" id="motd-raw" class="maxlength-input form-control d-inline-block w-200" data-plugin="maxlength" data-placement="top-right" maxlength="20" value="The Text I Want to Move" />  
     </span>
  </div> 
</div>          
</form>

<?php
namespace SystemHttp;
class Colors {
	const REGEX = '/(?:§|&amp;)([0-9a-fklmnor])/i';
	const START_TAG  = '<span style="%s">';
	const CLOSE_TAG  = '</span>';
	const CSS_COLOR  = 'color: #';
	const EMPTY_TAGS = '/<[^/>]*>([s]?)*</[^>]*>/';
	const LINE_BREAK = '<br />';
	static private $colors = array(
		'0' => '000000', //Black
		'1' => '0000AA', //Dark Blue
		'2' => '00AA00', //Dark Green
		'3' => '00AAAA', //Dark Aqua
		'4' => 'AA0000', //Dark Red
		'5' => 'AA00AA', //Dark Purple
		'6' => 'FFAA00', //Gold
		'7' => 'AAAAAA', //Gray
		'8' => '555555', //Dark Gray
		'9' => '5555FF', //Blue
		'a' => '55FF55', //Green
		'b' => '55FFFF', //Aqua
		'c' => 'FF5555', //Red
		'd' => 'FF55FF', //Light Purple
		'e' => 'FFFF55', //Yellow
		'f' => 'FFFFFF'  //White
	);
	static private $formatting = array(
		'k' => '',                               //Obfuscated
		'l' => 'font-weight: bold;',             //Bold
		'm' => 'text-decoration: line-through;', //Strikethrough
		'n' => 'text-decoration: underline;',    //Underline
		'o' => 'font-style: italic;',            //Italic
		'r' => ''                                //Reset
	);
	static private function UFT8Encode($text) {
		//Encode the text in UTF-8, but only if it's not already.
		if (mb_detect_encoding($text) != 'UTF-8')
			$text = utf8_encode($text);
		return $text;
	}
	static public function clean($text) {
		$text = self::UFT8Encode($text);
		$text = htmlspecialchars($text);
		return preg_replace(self::REGEX, '', $text);
	}
	static public function convertToMOTD($text, $sign = 'u00A7') {
		$text = self::UFT8Encode($text);
		$text = str_replace("&", "&amp;", $text);
		$text = preg_replace(self::REGEX, $sign.'${1}', $text);
		$text = str_replace("n", 'n', $text);
		$text = str_replace("&amp;", "&", $text);
		return $text;
	}
	static public function convertToHTML($text, $line_break_element = false) {
		$text = self::UFT8Encode($text);
		$text = htmlspecialchars($text);
		preg_match_all(self::REGEX, $text, $offsets);
		$colors      = $offsets[0]; //This is what we are going to replace with HTML.
		$color_codes = $offsets[1]; //This is the color numbers/characters only.
		//No colors? Just return the text.
		if (empty($colors))
			return $text;
		$open_tags = 0;
		foreach ($colors as $index => $color) {
			$color_code = strtolower($color_codes[$index]);
			//We have a normal color.
			if (isset(self::$colors[$color_code])) {
				$html = sprintf(self::START_TAG, self::CSS_COLOR.self::$colors[$color_code]);
				//New color clears the other colors and formatting.
				if ($open_tags != 0) {
					$html = str_repeat(self::CLOSE_TAG, $open_tags).$html;
					$open_tags = 0;
				}
				$open_tags++;
			}
			//We have some formatting.
			else {
				switch ($color_code) {
					//Reset is special, just close all open tags.
					case 'r':
						$html = '';
						if ($open_tags != 0) {
							$html = str_repeat(self::CLOSE_TAG, $open_tags);
							$open_tags = 0;
						}
						break;
					//Can't do obfuscated in CSS...
					case 'k':
						$html = '';
						break;
					default:
						$html = sprintf(self::START_TAG, self::$formatting[$color_code]);
						$open_tags++;
						break;
				}
			}
			//Replace the color with the HTML code. We use preg_replace because of the limit parameter.
			$text = preg_replace('/'.$color.'/', $html, $text, 1);
		}
		//Still open tags? Close them!
		if ($open_tags != 0)
			$text = $text.str_repeat(self::CLOSE_TAG, $open_tags);
		//Replace n with <br />
		if ($line_break_element) {
			$text = str_replace("n", self::LINE_BREAK, $text);
			$text = str_replace('n', self::LINE_BREAK, $text);
		}
		//Return the text without empty HTML tags. Only to clean up bad color formatting from the user.
		return preg_replace(self::EMPTY_TAGS, '', $text);
	}
}
?>

您可以像这样将文本输入值放入控制器中:

public function someName(Request $request) {
     dd($request->motd) // this is test either you get value from form text field or not
}

确保在控制器顶部使用use IlluminateHttpRequest;

最新更新