When I try to echo JavaScipt code with normal text like this:
echo '<script type="text/javascript">document.getElementById("errorbox").innerHTML = "Please enter a valid invite link!";</script>';
It works fine, but when I try to echo something that uses an user input:
echo '<script type="text/javascript">document.getElementById("errorbox").innerHTML = "Your New Link: ' . $link . ' (websi.te/' . $link . ')";</script>';
It doesn't work, and I get this error:
SyntaxError: unexpected token: identifier
Your quotes didn't match. I checked your code in PhpStorm, and after replacing and escaping two quotes in the JavaScript string it din't show a syntax error anymore.
I changed
"Your New Link: ' . $link . ' (websi.te/' . $link . ')"
to
\'Your New Link: ' . $link . ' (websi.te/' . $link . ')\'
complete string:
echo
'<script type="text/javascript">' .
'document.getElementById("errorbox").innerHTML = \'Your New Link: ' . $link . ' (websi.te/' . $link . ')\';' .
'</script>';
I guess the double quote is wrong.
Try to this. I edited <a href=\"...
Good luck.
echo '<script type="text/javascript">document.getElementById("errorbox").innerHTML = "Your New Link: ' . $link . ' (websi.te/' . $link . ')";</script>';
Related
The result I want:
<div class="cat_cards_item" onclick="myFunction('http://website','name','description')">
I tried 2 methods:
1.
echo "<div class='cat_cards_item' onclick='myFunction(" . $resultwebsite['website'] . "," . $resultname['name'] . "," . $resultdesc['description'] . ")'>"
My result:
<div class="cat_cards_item" onclick="myFunction(http://website,name,description)">```
echo "<div class='cat_cards_item' onclick='myFunction('" . $resultwebsite['website'] . "','" . $resultname['name'] . "','" . $resultdesc['description'] . "')'>"
My result:
<div class="cat_cards_item" onclick="myFunction(" http:="" website="" ',' name="" ',' description=""')'="">
Im trying to call a javascript function that opens a modal with the information I pull from my php code.
I tried most styles of placing quotes and im getting a headache from it, any help?
By using the heredoc-syntax you can get a more readable string, and you can simply integrate your variables without concatenations:
<?php
$mydiv = <<<EOD
<div class="cat_cards_item" onclick="myFunction('{$resultwebsite['website']}','{$resultname['name']}','{$resultdesc['description']}')">
EOD;
echo $mydiv;
?>
If you would create multiple of the same HTML-structure, I would create a simple PHP function that generates the HTML code, so you don't have to worry about the quotes:
<?php
$HTML = createModal('cat_cards_item', "myFunction('http://website','name','description')");
echo $HTML;
function createModal($class, $onClick) {
return "<div class=\"${class}\" onclick=\"${onClick}\">";
}
<div class="cat_cards_item" onclick="myFunction('http://website','name','description')">
Try the PHP code online!
I need to output some strings in the onclick anchor function.I have used proper escaping but anyhow its returning error.
My code is:
$output .= '<a title="Minus this" href="#" onclick = removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ');></a>';
And also used this :
$output .= '<a title="Minus this" href="#" onclick = "removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ')"></a>';
But in both cases there is Uncaught SyntaxError: Unexpected token }
The quotes is totally wrong. Do this way, using the first one:
$output .= '<a title="Minus this" href="#" onclick=\'removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ');\'></a>';
See the 's I have added and removed spaces? And please, next time, don't mingle both PHP and JavaScript. It's confusingly dangerous.
See also:
Are single quotes allowed in HTML?
Is there a better way to write this php code?
You should use single quotations to represent string in HTML element
$output .= '<a title="Minus this" href="#" onclick = "removefromCart(\'' . $item . '\', \'' . $nonce . '\', ' . $deductQty . ')"></a>';
For greater than or equal to PHP5
$output .= '<a title="Minus this" href="#" onclick = removefromCart($item,$nonce,$deductQty);></a>';
For Less than PHP5 try it
$output .= '<a title="Minus this" href="#" onclick = removefromCart(' . $item . ',' . $nonce . ', ' . $deductQty . ');></a>';
So I am building a site in Wordpress and am allowing the user to register/login. However I would like to create a custom nav bar instead of the awful 'admin bar' and so I want to be able to load the user name into an html element. I know that the username can be retrieved with this php:
<?php wp_get_current_user(); ?>. But what I want to know is, what is the best way to pull that into a JS variable that can then be set to be the inner html of an element. Is there another more direct way to do this? I also looked at JSON but couldn't find much that relates to this kind of scenario.
Any help is much appreciated.
Use echo:
<div>
<?php echo wp_get_current_user(); ?>
</div>
You can add this to a JavaScript variable in the same way:
<script>
var username = "<?php echo wp_get_current_user();?>";
</script>
EDIT Looking at the WP function reference, the wp_get_current_user() is an object, and you can access its variables like so:
<?php
$current_user = wp_get_current_user();
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
?>
Therefore, you can use this script to output the display name into a div element:
<?php $current_user = wp_get_current_user(); ?>
<div>
<?php echo $current_user->display_name; ?>
</div>
I have a php query like so:
<?php
$query = "SELECT * FROM " . $usertable . " ORDER BY fname;";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo '<option value="' . $row['pkid'] . '">' . $row['fname'] . ' ' . $row['lname'] . '</option>';
}
?>
Within this same .php file I have some javascript. All I would like to do is return $row['fname'] as a a javascript variable.
Is this possible?
If you want to output the PHP variable in a JavaScript variable you could do something like this -
echo '<script>var name+' . $row['pkid'] . ' = ' .$row['fname'] . ';</script>';
This would give you a uniquely named variable for each row.
I have this line in php code:
$return .= ' <div class="acrDivP" onClick="javascript: window.location.href = \'' . $productUrl . '\';" id="acrLink">' . $resultText . '</div>';
I want to add a second onClick action as shown in bold (**) below:
$return .= ' <div class="acrDivP" onClick="javascript: window.location.href = \'' . $productUrl . '\';**ga('send', 'event', 'testEvent', 'click', 'ProductName');**" id="acrLink">' . $resultText . '</div>';
I know my syntax is horribly wrong since some charatcters have to be escaped. Could someone please help me fix the syntax? Or do I need a whole new approach?
$return .= ' <div class="acrDivP" onClick="javascript: ga(\'send\', \'event\', \'testEvent\', \'click\', \'ProductName\'); window.location.href = \'' . $productUrl . '\';" id="acrLink">' . $resultText . '</div>';
You can do it this way, it's just going to require a lot of gnarly escaping. This reduces both the readability and the maintainability of your code.
$return .= '<div class="acrDivP" onClick="window.location.href = \'' . $productUrl . '\'; ga(\'send\', \'event\', \'testEvent\', \'click\', \'ProductName\');" id="acrLink">' . $resultText . '</div>';
It would require some refactoring, but if you can break out of PHP for the code, it can be much cleaner:
<div class="acrDivP" onClick="window.location.href = '<?php echo $productUrl ?>'; ga('send', 'event', 'testEvent', 'click', 'ProductName');" id="acrLink"><?php echo $resultText ?></div>