<?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>
';
?>
As you see, this presents a conflict at
<a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
and at
<script type="text/javascript">
$('input[id=image_file]').change(function() {
$('#photoCover').val($(this).val());
});
</script>
due to the script start ' ending the echo ' how would you fix this? Sorry if it is a stupid question
Short answer is to use back-slash escaping, like this:
<?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>
';
?>
Hope this helps.
You can escape '
and "
like this:
$html = ' " \" \' ';
This way you get 3 different quotations:
$html = '<script type="text/javascript">
$(\'input[id=image_file]\').change(function() {
$(\'#photoCover\').val($(this).val());
});
</script>';
Also be careful when using escaped qoutation in javascript code, which is inside a PHP string:
$html = '<script type="text/html">
var = \'This text has \\\'quoted\\\' single quotes :)\';
</script>';
Try this. This will work.
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>
';
?>
I don't know why you're echoing this in PHP, but you can just do this:
<?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
?>
Alternatively you can use Heredoc
syntax:
<?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;
?>
In this case you don't need to worry about quotes and escaping.
Since this is a common thing with PHP string constructions, I'll keep it to examples:
echo 'I want this text to show my ' in the sentence';
The example above is a syntax error since the string was closed before expected. However if the single quote in the string is escaped, it will be valid and displayed as so:
echo 'I want this text to show my \' in the sentence';
//output : I want this text to show my ' in the sentence
Other methods is to go back and forth between double and single quotes:
echo "This will show my ' in the sentence";
//output : This will show my ' in the sentence
But sometimes you need more and it looks ugly to do the following:
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
So remember to escape. The other way with HTML is to exit PHP to display it (sometimes not an option):
<?php if(true) : ?>
<p>Show some html <?php echo $var;?> here</p>
<?php endif; ?>
or simply
<?php
//some code here
?>
<html>
</html>
This can be done many ways and will avoid the need to juggle the quotes in echo
Looks like you are looking for heredoc syntax. Heredoc syntax acts like a 3rd type of '
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;
Start your heredoc statement with <<<UNIQUEIDENTIFIER
and then end it with NO whitespace, UNIQUEIDENTIFER
, possibly a ;
, and then a newline. The syntax for closing it is very particular, so be careful with it.
See: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Note: You don't have to use HEREDOC as your unique identifier, but I tend to do so as it makes it easier for those who are new to the syntax to have something to google to figure out what it is they are seeing.