How to make an onclick div in php [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
<script type="text/javascript">
function Invest()
{
alert("Mechanical /n Electrical /n Computer /n Civil");
}
function create()
{
alert("Graphic /n Interior /n Architecture");
}
function motivate()
{
alert("Business Administration /n Accounting and Finance");
}
</script>
The above code is my javascript code
<form action="main.html" method="post">
<?php
$v2 = $_GET['dropdown'];
if ($v2=="investigative" )
{
echo "<h3>It's recommended that you follow</h3><h2>Engineering School</h2>";
}
else if ($v2=="creative" )
echo "<h3>It's recommended that you follow</h3><h2>Arts School</h2>";
else if ($v2=="motivated" )
echo "<h3>It's recommended that you follow</h3><h2>Business School</h2>";
?>
What i want to do is to change my echo to a div onclick so it will call my function from javascript and show the information i have inside it.
Something like this
echo ""<div onclick=Invest();"<h3>It's recommended that you follow</h3><h2>Engineering School</h2>";
When I do that thought i get an error
Parse error: syntax error, unexpected 'onclick' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\MyProject\eval.php on line 35
What am I doing wrong and how to make that work? I am new at php :/

Not something like this echo ""<div onclick=Invest();...
You need to do it like this put div tag inside double quotes for onclick attribute open single quotes because double quotes are already opened
echo "<div onclick='Invest()'><h3>It's recommended that you follow</h3><h2>Engineering School</h2></div>";

Related

event on click not working in echo php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
How to echo in php?
<div class="city" onclick="openCity(event,'London')">
I am try write sintax :
echo ' < div class=" city " onclick=" openCity(event,'London') " > ';
But event onclick not working
You have an issue with your quotes. You must escape the ones that suround your "London" arguement. Try this instead:
echo ' < div class=" city " onclick=" openCity(event,\'London\') " > ';
You might getting javascript syntax error due to single quote. Please you started echo with single quote. So need to handle inner single quotes. You can handle it in two ways
echo '<div class=" city " onclick="openCity(event,\'London\') ">';
or
echo "< div class=\" city\" onclick=\" openCity(event,'London');\" >";

Hide div if database contains value - Angular [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to check if a MySQL database contains a specific value. If it is true I want to hide a HTML <div>. Is this possible?
Can I hide elements with CSS & JS, or what should I use to hide the div?
Also, How would we add it in the Div like a NgIF statement
Thanks!
If you are seriously thinking about hiding the element with JavaScript (due to some kind of restriction or requirement). You can use following method.
CSS + Javascript + PHP way:
If you are thinking more like the Run code snippet section in Stack Overflow. Show expanded ONLY if it is set in database.
Then in that case you can implement something like this
$(function() {
$("#btnToggle").on("click", function() {
$("#comments").toggleClass("hide");
});
});
.hide {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Hide using CSS way. Hide class can be echoed from your PHP based on the database value -->
<button type="button" id="btnToggle">Toggle hide/show</button>
<div id="comments" class="hide">Comment section to hide</div>
Try something like this in your PHP file. I have included an example of an item that is always rendered, and something that is conditionally rendered.
<div id="userinfo">
<?php // This is always rendered by the server ?>
Username: <?php htmlentities($row['username']) ?>
<?php // This is rendered by the server if the column is true ?>
<?php if ($row['admin']): ?>
<div class="admin">(admin)</div>
<?php endif ?>
</div>
Something along the line of
if (your statement condition) {
echo '<script type="text/javascript">document.getElementById(DIV_ID").style.display= "none";</script>';
exit;
}

How to implement a closure in jQuery [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I don't know much about jQuery. Here I am trying to implement a closure to attach a click event to each anchor tag so that each of them will alert a certain number once blicked. But I am getting the following error:
SyntaxError: missing ) after argument list
};})(i);
can anyone help me with this problem?thanks :)
Code:
<div id='pageBox'>
<ul id='pageContainer'>
<?php for($i=1;$i<=$pagenum;$i++){?>
<li><a href=''><?php echo $i;?></a></li>
<?php }?>
</ul>
</div>
<script>
for(i=1;i<=<?php echo $pagenum;?>;i++){
$('#pageContainer li a').click((function(i){return function(event){
event.preventDefault();
alert(i);
};})(i);
);
}
You don't need closure here. This can be achieved using simple approach. Persist php variable $i in data-* prefixed custom attribute, which can be fetch using .data()
Change your HTML as
<ul id='pageContainer'>
<?php for($i=1;$i<=$pagenum;$i++){?>
<li><a href='' data-id='<?php echo $i;?>'><?php echo $i;?></a></li>
<?php }?>
</ul>
Script
$('#pageContainer li a').click(function(){
alert($(this).data('id'));
});

How to Embedded JQuery code in a php variable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to embedded the below jquery code in the following php variable $var
JQUERY Code:
<img src="images/image.png" id="Image1" alt="" style="width:30px;height:30px;">
PHP Code:
<?php $var = 'my_jquery_code'; return $var; ?>
You just need to escape the single quotes:
<?php
$var ='<img src="images/image.png" id="Image1" alt="" style="width:30px;height:30px;">';
return $var;
The only reasonable scenario I can think of where you would want to use onclick would be if you're dynamically loading the anchor element.
If so, this is probably better.
[script]
$(document).on('click', '.my_class_here', function() {
$('jQueryDialog1').dialog('open');
});
[/script]
[link_template]
...
[/link_template]
Then you would use AJAX or whatever voodoo you like to retrieve the link template (maybe while passing it some variables?) and add it to the page.
Alternatively, you can use $(document).find('.my_class_here').on('click', function... , but this is bad, performance wise.

JQuery function not executing [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
This should be super simple. The first code block works, but when I wrap it in a function and attempt to call the function, it doesn't work.
This Works:
$(document).ready(function() {
$("#groups-image").hide();
$("#quiz-image").hide();
$("#members-image").hide();
});
This Doesn't Work:
$(document).ready(function() {
function hideatstart() {
$("#groups-image").hide();
$("#quiz-image").hide();
$("#members-image").hide();
}
hideatstart();
});
UPDATE: After looking at the accepted answer, I was able to get working code. It involved not having any spaces between the lines of code, which would allow p tags to be inserted. Best practice would be to remove the JQuery to a separate script, which I will do. The following code works:
$(document).ready(function() {
function hideatstart(){
$("#groups-image").hide();
$("#quiz-image").hide();
$("#members-image").hide();
}
hideatstart();
});
I inspected the HTML on your live site and found that your snippet is outputted like this:
<div class="art-postcontent">
<!-- article-content -->
<!-- [snipped] -->
<p><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><br />
<script type="text/javascript">
$(document).ready(function() {</p>
<p> function hideatstart() {</p>
<p> $("#groups-image").hide();
$("#quiz-image").hide();
$("#members-image").hide();
}</p>
<p> hideatstart();
});
</script></p>
<!-- [snipped] -->
</div>
As you can see, WordPress's wpautop filter wrapped your lines inside paragraphs.
If you need to inject a script somewhere, you better do it with a plugin or a theme function and not by pasting the code in a WordPress post or widget. All regular WordPress content types are escaped to prevent such possibly malicious injections. It seems like you're using a custom art post type, which normally gets a similar treatment. There are ways to prevent the content from being escaped, but you should seriously consider moving your script code somewhere else.

Categories