I want to pass the php variable value in onClick function.
When i pass the php variable, in the UI i am getting the variable itself instead I need the value in the variable.
Below is code snippet, please help me.
<?php
print '<td>';
$node = $name->item(0)->nodeValue;
$insert= "cubicle"."$node<br>";
Echo '<a href= "#" onClick= showDetails("$node");>'. $insert .'</a> ';
print '</td>';
?>
Variable parsing is only done in double quoted strings. You can use string concatenation or, what I find more readable, printf [docs]:
printf('%s ', $node, $insert);
The best way would be to not echo HTML at all, but to embed PHP in HTML:
<?php
$node = $name->item(0)->nodeValue;
$insert = "cubicle" . $node;
?>
<td>
<a href= "#" onClick="showDetails('<?php echo $node;?>');">
<?php echo $insert; ?> <br />
</a>
</td>
You have to think less about quotes and debugging your HTML is easier too.
Note: If $node is representing a number, you don't need quotations marks around the argument.
you shouldn't be wrapping $node in '"':
Echo '<a href= "#" onClick= showDetails($node);>'. $insert .'</a> ';
If you want the value of $node to be in a string, thn i would do:
Echo '<a href= "#" onClick= showDetails("' . $node. '");>'. $insert .'</a> ';
$var = "Hello World!";
echo "$var"; // echoes Hello World!
echo '$var'; // echoes $var
Don't mix up " and ', they both have importance. If you use some " in your string and don't want to use the same character as delimiter, use this trick:
echo 'I say "Hello" to ' . $name . '!';
I think you are searching for PHP function json_encode which converts PHP variable into JavaScript object.
It's more secure than passing the value right in the output.
Echo '<a href= "#" onClick= showDetails("'.$node.'");>'. $insert .'</a> ';
I have been using curly braces lately instead of concatenation. I think it looks better/is more readable, and mostly I find it is easier and less prone to human error - keeping all those quotes straight! You will also need quotes around the contents inside of onClick.
Instead of this:
Echo '<a href= "#" onClick= showDetails($node);>'. $insert .'</a> ';
Try this:
Echo '{$insert} ';
As a side note, I usually use double quotes to wrap my echo statement and strictly use single quotes within it. That is just my style though. However you do it, be sure to keep it straight. So my version would look like this:
echo "<a href='#' onClick='showDetails({$node});'>{$insert}</a>";
Related
I want to insert a URL all into single quotes. I have tried using both single and double quotes, but it is not working. I checked this question - Escaping single quote in URL link, but it did not help:
<?php
echo '\'' base_url().'\'index.php?admin/product/delete/1\'';
echo '\''; echo base_url().index.php?admin/product/delete/1\'';
echo '\'. base_url().index.php?admin/product/delete/1\'';
echo "\'"; echo base_url().'index.php?admin/product/delete/1\'";
?>
I am trying to achieve this:
'http://localhost/my_site/index.php?admin/product/delete/1'
The URL in single quotes.
If you really want to use only single quotes, try this:
echo '\'' . base_url() . 'index.php?admin/product/delete/1\'';
Just simple:
echo "'" . base_url() . "index.php?admin/product/delete/1'";
You can use double-quotes to pass variables into strings in PHP. On top of enhancing readability, this also saves you the trouble of escaping single quotes.
So for example:
<?php
$foo = 'bar';
echo 'single-quoted string: $foo <br />';
echo "double-quoted string: $foo <br />"; // "$foo" works, "{$foo}" is better
echo "double-quoted string: \$foo <br />"; // Backslash escapes
?>
Prints (emphasis mine):
single-quoted string: $foo
double-quoted string: bar
double-quoted string: $foo
So to update your example:
<?php
/* ... */
$baseurl = base_url();
echo "'{$baseurl}index.php?admin/product/delete/1'";
?>
Or how about we put it all into a variable and gain a little readability:
<?php
/* ... */
$baseurl = base_url();
$fullurl = $baseurl . 'index.php?admin/product/delete/1';
echo "'{$fullurl}'";
?>
echo "'" . base_url() . "index.php?admin/product/delete/1'";
I have used below code:
$html = '';
$html. = '<br></br>';
$html .= 'Next 50 ';
but my PHP variables are not recognized.
Difference between Double quotes and single quotes
According to this Stackoverflow answer:
Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you what to echo "The $types are" That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
Solution
They are not recognized, because you have put all your string in single quotes (').
You should replace them all with double quotes (").
$html = "";
$html. = "<br></br>";
$html .= "Next 50";
Another solution
Also you have the option of separating your variables from text:
$html = '';
$html. = '<br></br>';
$html .= 'Next 50';
Yet another solution
If you don't want to escape so many double quotes, you may use single quotes instead:
$html = "";
$html. = "<br></br>";
$html .= "<a href='javascript:void(0)' class='test' onclick='get_data(\"$access_token\",1,\"$marketTagId\");'>Next 50</a>";
Try this, it might help you
<?php
$html = '';
$html. = '<br></br>';
$html .= 'Next 50';
?>
Try this
get_data('".$access_token."',1,'".$marketTagId."');
Or
get_data(<?php $access_token ?>,1,<?php $marketTagId ?> );
You need to write '' around your PHP variables otherwise in onclick function they will not worked.
Use below code:-
$html = '';
$html. = '<br></br>';
$html .= "Next 50 ";
Hope it will help you :)
Use the php variables within { and }
$access_token = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$marketTagId = "__AAA__B_123";
$html = "";
$html .= "<br></br>";
$html .= "Next 50";
echo htmlentities($html);
your code:
$html. = '<br></br>';
$html .= '<a href= "
change to this:
$html.= '<br></br>';
$html.= '<a href= "
try this:
$html = '';
$html.= '<br></br>';
$html.= 'Next 50 ';
can you expect this:
Hi All Thanks for helping.
This is worked for me :
$html .= '</table>';
$html .= '<br><br>';
$html .= '<a href= "javascript:void(0)" class= "test" onclick= \'get_data("'.$access_token.'","1","'.$marketTagId.'");\'>Next 50 </a>';
I can't find a solution how to combine single and double quotes to echo HTML and call a function at the same time:
foreach ($result as $r) {
echo "<a href='get_permalink(get_page($r->id))'>".get_permalink(get_page($r->id)).'</a><br>';
}
Problem is this part is parsed as text, not php
"<a href='get_permalink(get_page($r->id))'>"
Cansome one help me to combine this? get_permalinks and get page are wordpress built in functions, so they should have function behavior
You can't call a function inside double " quotes.
foreach ($result as $r)
{
echo "<a href='".get_permalink(get_page($r->id))."'>".get_permalink(get_page($r->id)).'</a><br>';
}
It's not possible to run PHP code when it's inside a string (unless with eval). However, you can use printf() to separate code from string:
$url = get_permalink(get_page($r->id));
printf('%1$s<br>', htmlspecialchars($url, ENT_QUOTES, 'UTF-8'));
The %1$s is a positional format specifier; this is done so that the encoded $url value only has to be passed once.
Just concat the string like this:
echo "<a href='". get_permalink(get_page($r->id)) . "'>" . get_permalink(get_page($r->id)) . "</a><br>";
Also if you want to know what's the difference between single and double quotes see this:
What is the difference between single-quoted and double-quoted strings in PHP?
try this way:
if($result as $r)
{
echo "<a href='" . get_permalink(get_page($r->id)) . "'>" . get_permalink(get_page($r->id)) . '</a><br>';
}
I am using like
$myPage .= '<td><a href=\'javascript:editProduct('
.$row['id']
.',"'
.$row['name']
.'")\'>Edit</a></td>';
where $row['name'] has quotes in its value. it breaks. how do i solve the issue both from php side and js side...
$row['name'] is value from DB. and it will have value like pradeep's and pradeep"s also
i used like
$myPage .= '<td><a href=\'javascript:editProduct('.addslashes($row['id']).',"'.addslashes($row['name']).'")\'>Edit</a></td>';
it solves the issue of double quotes. but when i have single quotes in value the javascrit link looks like
javascript:editProduct(28,"pradeep\
it actually breaks..
And how do i strip down the slashes added by addslashes in javascript..
UPDATE - FINAL CODE
$myPage .= '<td><a href=\'javascript:editProduct('.$row['id'].',"'.htmlentities($row['name'],ENT_QUOTES).'")\'>Edit</a></td>';
and js looks like
function editProduct(id,name){
alert(name);
}
can any one solve my issues
Try:
$myPage .= "<td><a href='javascript:editProduct({$row['id']},\""
. htmlentities( $row['name'] )
. "\")'>Edit</a></td>";
htmlentities default behaviour is to convert double quotes and leave single quotes alone, if you require converting single and double quotes, then call it like this:
htmlentities( $row[ 'name' ], ENT_QUOTES )
Also, using { .. } in "..." strings is the correct way to substitute variables.
The PHP string
'<a href=\'javascript:editProduct('.$row['id'].',"'.$row['name'].'")\'>';
outputs (assuming some values)
<td><a href='javascript:editProduct(123,"abc")'></td>
Presumably it breaks if $row['name'] contains a " quote. You could replace such quotes with a \" in the string before you output it using str_replace('"', '\"', $row['name'])
I am trying to print a variable between curly braces as
Product_number{product_version}
I tried
echo "$product_number{$product_version}";
But that does not work. I don't understand why :(
try using double braces:
echo "$product_number{{$product_version}}";
You can also do:
echo "$product_number{".$product_version."}";
{ followed by $ is treated specially. It is mainly used when you want to append a string immediately at the end of a variable's value:
$v = 'hack';
echo "I {$v}ed it";
echo $product_number . "{" . $product_version . "}";
Escape the "{":
echo "$product_number\{$product_version}";