Anyone know how should I write to get long hash value inside the single quote but when writing this code inside the php.
For example at the moment my code is below which I know its wrong:
$raw_tbody .= "<td width='10%'><a onclick='customRuleEdit('{$row['hash']}')' >";
So when I write like above its display on HTML side like below: (Which is wrong as you can see the single and double quotes)
<a onclick="customRuleEdit(" f6710ecaa686546bb424525686c792823f8d5193')'="">
What I want is as below Output:
<a onclick="customRuleEdit('f6710ecaa686546bb424525686c792823f8d5193')">
So anyone knows what I should write to make the output like above??
Thanks
Try this code for helpful..
$raw_tbody .= "<td width='10%'><a onclick=\"customRuleEdit('{$row['hash']}')\" >";
I have a problem I can't solve by myself, don't know how simple it is to solve but I want to open this link in another frame:
<?php echo "<a href=details.php?id=$row[idProdukt]>$row[Produkt_Namn]</a>" ?>
tried to use taget but since it's html I couldn't really wrap my head around how to type it.
any help is greatly appreciated.
Just add the attribute to your anchor tag as usual. Make sure you use the name of the targeted iframe as its value:
<?php echo "<a href='details.php?id=$row[idProdukt]' target='framename'>$row[Produkt_Namn]</a>" ?>
I added quotes around your html attribute values as that is a good practice to be in. It prevents issues arising from errant or intentional spaces in attribute values.
First, you have error on array index, missing quotes. You can use this;
<iframe src="some_url" name="test">
.....
</iframe>
<?php echo "" . $row['Produkt_Namn'] . ""; ?>
The trouble is missing quotes around href attribute value and array key.
echo "{$row['Produkt_Namn']}";
See in action
I'm a beginner PHP programmer, and I was wondering what was wrong with my code.
Here is the small excerpt from the affected spot:
echo "<form action='?tab=4' name='toedit5' method='get'><input value='text' onblur='edit('toedit5')' /></form>";
In Chrome's Developer Tools, the form element totally disappears, and the edit('toedit5') becomes edit(' toedit5').
The edit() function doesn't execute.
Is there anything wrong with this one line of code? Otherwise it is outside code messing with it. Sorry I didn't include it, but I don't know what to include. If you need more information, please tell me.
Thanks.
You need to escape your quotes inside your quoted echo'd statement, like this:
<?php
echo "<form action='?tab=4' name='toedit5' method='get'>";
echo "<input value='text' onblur='edit(\"toedit5\")' />"; // escaped..!
echo "</form>";
?>
It helped me to think about it like this when I was starting out: how does your browser know if the second single quote in onblur='edit('toedit5')' is closing your onblur statement or opening up the parameter? In this example, your browser will pair up the first 2 quotes it sees and assign that to the onblur attribute, i.e.: onblur='edit(' only!
Update 1:
Using the code above, I inspected a quick PHP page I created in Chrome's developer tools and was able to see the following (form available for inspection):
You really should use the more standard double quotes around the HTML properties and use single quotes around your string, with escaped single quotes within the javascript method calls. Like this:
echo '<form action="?tab=4" name="toedit5" method="get"><input value="text" onblur="edit(\'toedit5\')" /></form>';
<?
//SQL SELECT HERE
$result = mysql_query($sql);
$options = '';
while ($row = mysql_fetch_array($result)) {
$options .= '<option>Data: ' . $row['data'] .'</option>';
}
?>
$("#multiSelect").html("<?=$options?>");
The above is a PHP query inlined in a javascript function. It's goal is to populate a multiselect. The issue is that when $row['data'] contains something with double quotes jQuery doesn't like it and complains. When I remove the row containing double quotes it works fine.
How can I get around this? Is this normal behavior of jQuery.
Try to addslashes: http://php.net/manual/en/function.addslashes.php
It's because your call is being coming out as something like:
$("#multiSelect").html(""Hello"");
Most programming languages will have problems with that - they assume that the first quote you're adding ends the string you're passing in, and that the next text should be a valid piece of code.
You can get around it by escaping the quotes, removing them, or substituting them to something else:
$("#multiSelect").html("<?=addslashes($options)?>");
$("#multiSelect").html("<?=str_replace('"', '', $options)?>");
$("#multiSelect").html("<?=str_replace('"', '\'', $options)?>");
Depending on what the input text is likely to be.
WHY WHY WHY would you build the options with the code behind and than set it with jQuery? Why can't PHP just set it itself?
You need to escape the quotes with a \
"Man it is \"hot\" in here"
I've never seen something like this before. So, it's confusing me for a while. But now I understand and use it sometimes. So, after brief experience, can anybody tell me what is the pro and cons using Heredoc Notation in your PHP?
$stringval = <<<MYHEREDOC
just creating variable here. nothing more.
MYHEREDOC;
Personally, how do you use this PHP feature? Is it a bad way of coding or good way?
99% of the time I use it, it's for SQL queries eg:
$sql = <<<END
SELECT *
FROM sometable
WHERE value > 100
ORDER BY name
END;
I find it easier to spot such queries in the source code and copy and paste them into something to run them. Your mileage may vary. Note: you can do multi-line with normal strings. I tend to avoid this however as the first line is indented differently to the rest.
The biggest "pro" as far as I'm concerned is that you don't need to escape quotes. That's particularly an issue with markup. Decide for yourself which is easier to read. Assuming:
$name = 'foo';
$type = 'text';
$value = 'Default value';
Version 1: single quotes
$html = '<input type="' . $type . ' name="' . $name . '" value="' . $value . '">';
Version 2: double quotes
$html = "<input type=\"$type\" name=\"$name\" value=\"$value\">";
Version 3: heredoc
$html = <<<END
<input type="$type" name="$name" value="$value">
END;
Note: in version 2 you can of course use single quotes for attribute values to solve the escaping problem but the point is you have to worry about things like this. Personally I don't like to mix attribute quote types in markup either.
There are now two types of "Heredoc:"
Heredoc, similar to quotation marks "" will convert any variables into their value on creation. This is very useful for putting exact spacing/returns where you need them (no need for \n or \t).
Nowdoc, similar to single marks '' will NOT convert any variables and stores just the text value. This can be used in default values of class variables (unlike heredoc).
Heredoc Example:
$value = 5;
$string = <<<EOL
The number is $value
EOL;
// The number is 5
Nowdoc Example:
$value = 5;
$string = <<<'EOL'
The number is $value
EOL;
// The number is $value
EDIT: The only downside I can think of is that the ending characters (in the examples, EOL) MUST be preceded by 0 spaces. This is hard to remember and looks ugly when inside nested functions:
function foo() {
if ($bar) {
$string = <<<LINE
Hey, how is it going, $name.
This is a great test of Heredoc and Nowdoc.
Blah blah blah, go upvote me!
LINE;
}
}
Well, the pro(s) is
it's handy for large chunks of text with quotes
and the cons
you rarely if ever need large chunks of text in MVC
breaks indentation
has some quirks if used with classes (prior to 5.3)
Personally, i don't use it.
(an obligatory disclaimer about all this being a matter of preference, etc etc)
I don't use it at all. It was quite useful in Perl, as Perl had no HTML escape feature. So, PHP does, and if I want to print out large amount of text, i'd just close PHP tag and type text as is. In any other case old good double quotes suits me well.
HEREDOCs are very useful for dumping large blobs of text into a variable, or directly out to the client. It also saves you multiple concatenations if you're building up a long multi-line string. The only main drawback is you need to pre-process any data you'll be inserting.
echo <<<EOF
<input type="text" name="foo" value="$bar" />
EOF;
is very readable, especially with an syntax-highlighting editor. But you do need to pre-process $bar with htmlspecialchars() OUTSIDE there heredoc (for escaping quotes), so you end up with:
$bar = htmlspecialchars($bar);
echo <<<EOF
etc....
Whereas if you're in "html mode", you could use:
<input type="text name="foo" value="<?php echo htmlspecialchars($bar) ?>" />
or
echo '<input type ... snip ... value="' . htmlspecialchars($bar) . etc....
Generally you don't need to store the escaped version of the data, so you might as well dump it out directly and save yourself the variable assignment step. HEREDOCs don't let you do that, as you can't insert a function call into them directly.
It is very useful to have a very neat programming style segregating html with php. Your php codes will be very readable and easier to understand. It facilitates debbuging codes considerably. It allows you to concentrate more on the logic of the program rather than the presentation with html. You just need one echo statement rather than the annoying php scripts all over within your html. To illustrate how it is being use, here is an example.
masterpage.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?php $title ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="../js/jquery.min.js" type="text/javascript"></script>
<link href="../bsa.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript" src="../js/sortEligibility.js"></script>
<body>
<div id="maincontainer">
<div id="menu">
<ul id="navtop">
<li><*linkhere* title="Home">Home</a></li>
<li >*linkhere* title="Eligibility">Eligibility</a></li>
<li >*linkhere* title="Registration List">Registration</a></li>
<li ><*linkhere*" title="BSA Ranks and Requirements ">BSA Ranks</a></li>
<li><*linkhere* title="Merit Badges">Merit Badges</a></li>
</ul>
</div>
<div id='contentcolumn'>
<?php echo "<h2>".$title."</h2>".$bodyhtml;?>
</div>
<div id="footer">Footer</div>
</div>
</body>
</html>
meritBadges.php
<?php
//include dbase connection
include("*linkhere*/includes/dbconnect.php");
//this page is called where the value id_scout is sent through GET
//Show required output
$result = mysql_query("SELECT e.id_merit, m . *
FROM bsa_merits AS m
LEFT JOIN bsa_earned_merits AS e ON e.id_merit = e.id_merit
WHERE id_scout = '{$_GET[id_scout]}'");
while($row = mysql_fetch_assoc($result)){
$body .=<<<__HTML_END
<td align="center"><img_src="*linkhere*/MeritBadges_Files/{$row['filename']}"><br>{$row['merit']}</td>
__HTML_END;
}
if(!empty($body)){
$bodyhtml =<<<__HTML_END
Earned Merits:
<table border="0" cellpadding="5" cellspacing="0">
<tr>
$body
</tr>
</table>
__HTML_END;
}
else{
die ("There is nothing to display");
}
mysql_free_result($result);
$title="Merit Badges" ;
include("masterpage.php");
?>
All other pages may be created in similar fashion with the above example. Remember you just create one big string for your output and display it in your hmtl file with just one echo!
Just analyze the above codes and see how you can improve your php programming with heredoc!
Is it a bad way of coding or good way?
You mean, Is it good practice or is it bad practice?
Generally, there's nothing wrong with using it. I'd go as far to say there's something wrong with not using it:
You don't have to escape single and double quotes
Allows you to append to a previous variable while defining a "context"
Allows straight-up non-PHP markup (Such as Javascript) to be used alongside PHP; makes non-PHP code noticeable
No noticeable differences in performance[*]. Even so, it's trivial.
Of course there's nothing wrong with $var = "Welcome, <b>$user</b>;" - However, it's not ideal if you intend to work with large quantities of foreign code and wish to maintain readability:
Really, it's an ideal way to deal with markup such as HTML if you can't output it directly.
[*]Based off personal tests, but should remain true regardless.
Whilst it is acceptable to use HEREDOC, the major downside in my opinion is that most IDE's lose the ability to track the code, and often display messages where closing tags cannot be found - particularly if an opening tag inside a different HERDOC to the the end tag, or the code is a mixture of all styles. One of the applications I inherited was all over the place. For example.
echo <<<PART1
<table>
<tr>
....
</tr>
PART1;
//more php code
?>
</table>
This is a bad example, where some code is in a HEREDOC, such as the start of the table, and then much later down the code, the table is closed normally outside of PHP tags. My IDE (phpstorm 2017) complains that it cannot find matching table tags.
For this reason, I always convert the HEREDOCs I find into single quote versions. I also dislike escaping, so the single quote method looks tidiest for me.
But that's just my opinion....