0
<?php

echo '

<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
  <input id="photoCover" class="input-large" type="text">
    <a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>

<script type="text/javascript">
   $('input[id=image_file]').change(function() {
   $('#photoCover').val($(this).val());
   });
</script>

'; 
?>

如您所见,这在

<a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>

并且在

<script type="text/javascript">
   $('input[id=image_file]').change(function() {
   $('#photoCover').val($(this).val());
   });
</script>

由于脚本开始“结束回声”,你将如何解决这个问题?对不起,如果这是一个愚蠢的问题

4

7 回答 7

1

尝试这个。这将起作用。

echo '

<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
<input id="photoCover" class="input-large" type="text">
<a class="btn" onclick="$(\'input[id=image_file]\').click();">Browse</a>
</div>

<script type="text/javascript">
$("input[id=image_file]").change(function() {
$("#photoCover").val($(this).val());
});
</script>

'; 
?>
于 2013-03-15T17:59:21.433 回答
1

看起来您正在寻找 heredoc 语法。Heredoc 语法就像第三种类型的'or "

echo <<<HEREDOC

<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
  <input id="photoCover" class="input-large" type="text">
    <a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>

<script type="text/javascript">
   $('input[id=image_file]').change(function() {
   $('#photoCover').val($(this).val());
   });
</script>

HEREDOC;

以 NO 空格开始 heredoc 语句,<<<UNIQUEIDENTIFIER然后以 NO 结尾UNIQUEIDENTIFER,可能是 a ;,然后是换行符。关闭它的语法非常特殊,所以要小心。

见:http ://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

注意:您不必使用 HEREDOC 作为您的唯一标识符,但我倾向于这样做,因为它使那些对语法不熟悉的人更容易通过 google 找出他们所看到的内容。

于 2013-03-15T18:04:05.550 回答
0

你可以逃避'"像这样:

$html = ' " \" \' ';

这样你就可以得到 3 个不同的报价:

$html = '<script type="text/javascript">
   $(\'input[id=image_file]\').change(function() {
   $(\'#photoCover\').val($(this).val());
   });
</script>';

在 javascript 代码中使用转义的 qoutation 时也要小心,它位于 PHP 字符串中:

$html = '<script type="text/html">
    var = \'This text has \\\'quoted\\\' single quotes :)\';
</script>';
于 2013-03-15T17:57:15.927 回答
0

简短的回答是使用反斜杠转义,如下所示:

<?php
echo '
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
<input id="photoCover" class="input-large" type="text">
<a class="btn" onclick="$(\'input[id=image_file]\').click();">Browse</a>
</div>
<script type="text/javascript">
$(\'input[id=image_file]\').change(function() {
   $(\'#photoCover\').val($(this).val());
});
</script>
'; 
?>

希望这可以帮助。

于 2013-03-15T17:56:35.050 回答
0

我不知道你为什么在 PHP 中回显这个,但你可以这样做:

<?php
    // PHP Code
    // In a PHP file, anything outside of PHP tags just gets echoed
?>

<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
  <input id="photoCover" class="input-large" type="text">
    <a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>

<script type="text/javascript">
   $('input[id=image_file]').change(function() {
   $('#photoCover').val($(this).val());
   });
</script>

<?php
    // PHP Code
?>
于 2013-03-15T18:01:12.507 回答
0

或者,您可以使用Heredoc语法:

<?php

$html = <<<HTML

<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
  <input id="photoCover" class="input-large" type="text">
    <a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>

<script type="text/javascript">
   $('input[id=image_file]').change(function() {
   $('#photoCover').val($(this).val());
   });
</script>

HTML;

echo $html;

?>

在这种情况下,您无需担心引号和转义。

于 2013-03-15T18:03:14.193 回答
0

由于这在 PHP 字符串构造中很常见,因此我将其保留为示例:

echo 'I want this text to show my ' in the sentence';

上面的例子是一个语法错误,因为字符串在预期之前被关闭了。但是,如果字符串中的单引号被转义,它将有效并显示如下:

echo 'I want this text to show my \' in the sentence';
//output : I want this text to show my ' in the sentence

其他方法是在双引号和单引号之间来回切换:

echo "This will show my ' in the sentence";
//output : This will show my ' in the sentence

但有时您需要更多,并且执行以下操作看起来很难看:

echo "I want this ' in here but I need to quote ".'"poney"'." in my sentence";
//output : I want this ' in here but I need to quote "poney" in my sentence

所以记得逃跑。HTML 的另一种方法是退出 PHP 以显示它(有时不是一个选项):

<?php if(true) : ?>
<p>Show some html <?php echo $var;?> here</p>
<?php endif; ?>

或者干脆

<?php
    //some code here
?>
<html>
</html>

这可以通过多种方式完成,并且可以避免在echo

于 2013-03-15T18:03:20.977 回答