HTML Quotes are making a mess - javascript

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!

Related

Uncaught Syntax error on onClick function javascript

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>';

How to view a pop up box with jquery for many data?

My question, before I go further with how I did it, is: How can I make this work, and make it easier and better than what I have done?
I have a database with a lot of data, and I am viewing all these in a table. Each <td> has a <div title=""> included, so that all who mouseover each <td> can see more information about that data item. The trouble is, that it's only viewable as a table on mobile devices. To create a hyperlink for a new page is difficult, because that takes the user away from a <table> with comparrision to just that one data information. So I was thinking to use jQuery UI. And was reading a little over the "Basic Dialog" option. I tried to put that into my script, but I see I have way too much data. So I created a file called jq_scripts.php. In there I have a script to create dialog options. But first I include it like this in the header:
<script src=\"//code.jquery.com/jquery-1.10.2.js\"></script>
<script src=\"//code.jquery.com/ui/1.11.4/jquery-ui.js\"></script>
<script src=\"jq_scripts.php\" type=\"text/javascript\"></script>
Then in the script I have put this as a standard:
$(function() {
$( "#dialog" ).dialog({
autoOpen: false,
});
$( "#opener" ).click(function() {
$( "#dialog" ).dialog( "open" );
});
});
But since I have so many different dialogs, I need one #id to each button with unique information, so this is what I have as a script:
echo "\$(function() {\n";
$result = mysqli_query($con, "SELECT * FROM $database WHERE type = 1");
$num = mysqli_num_rows($result);
if(!$result) {
die('Invalid query: ' . mysqli_error($con));
}
while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
foreach($abo as $abo_vis) {
$abo_vis = strtolower(str_replace(' ', '', $abo_vis));
echo " \$( \"#dialog_" . $abo_vis . "_" . $id . "\" ).dialog({\n";
echo " autoOpen: false,\n";
echo " });\n\n";
echo " \$( \"#opener_" . $abo_vis . "_" . $id . "\" ).click(function() {\n";
echo " \$( \"#dialog_" . $abo_vis . "_" . $id . "\" ).dialog( \"open\" );\n";
echo " });\n\n";
}
}
echo "});\n";
This creates a viewing file with 12583 lines!!!
In a <td> I have put this _DATA_ and in the bottom of the viewing file, I have created this:
echo "<div id=\"dialog_" . $abo_vis . "_" . $id . "\" title=\"" . $abo_vis . "\">\n";
echo "<p>" . $dialog_info[$id] . "</p></div>\n\n";
This creates this file to be a lot longer than needed. And on top of that, none of my <a href="#"> works. Actually, the <div id="dialog..."> actually shows, while on the demo it was hidden.
Maybe your problem is the way you are calling it, instead of using IDs use classes:
echo "<div class=\"dialog_" . $abo_vis . "_" . $id . "\" title=\"" . $abo_vis . "\">\n";

Displaying result of PHP function in a div

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>

PHP Database Query: How to return the results from a while loop to a Javascript Variable?

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.

Javascript not acquiring field value

I have 2 php pages, htmldisp.php and classfn.php. And an alert used in hrmldisp.php is not working. It should actually give a dropdown value.
htmldisp.php is
<?php include("classfn.php"); $obj=new hello;?>
<script language="javascript">
function submitted(){
var select=document.getElementById('newdrop').value;
alert(select);
}
</script>
<?php
$id="newdrop";
echo $obj->hellofn($id); ?>
<input type="submit" onclick="submitted();"
classfn.php is
<?php
class hello{
function hellofn($id){
$s="select `Name`,`Value` from `Days`;
$q=mysql_query($s);
$p='<td>'.'<select id="$id">';
while($re=mysql_fetch_array($q)){
$value=$re["Value"];
$name=$re["Name"];
$p.='<option value="' . $value . '"' . ( $selected==$value ? ' selected="selected"' : '' ) . '>' . $name . '</option>';
}
$p.='</select>'.'</td>';
return $p;
}
?>
The problem is alert(select) not working. Thanks
As far as I can tell you never call the function submitted. On top of that your HTML is completely malformed. You have a bunch of html and javascript outside of your html tag. And you have 2 opening html tags with no close...

Categories

Resources