Cannot echo .load function - javascript

For some reason this code doesn't work. Adding \ to the front of the $ doesn't change anything. Can anyone see the problem?
$commentid = 2; //for example
echo "<script>";
echo "<div id = 'commentinput".$commentid."'>";
echo "</div>";
echo "$('#commentinput".$commentid."').load('editcomments2.php')";
echo "</script>";

Do it like this:
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';

I'd suggest not using echo to output HTML (in most cases anyway), but closing the PHP tag instead.
This becomes (edited with a loop example, see comments below):
<?php while ($commentid = $dbObject->dbMethod()): ?>
<div id="commentinput<?= $commentid ?>">
</div>
<script>
$('#commentinput<?= $commentid ?>').load('editcomments2.php');
</script>
<?php endwhile ?>
Note that in this case, the <script> portion should probably be outside of the loop, with a jQuery selector matching all the comment inputs.

So besides all of the other answers, I found another solution to!
When echoing out js code, like
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
//and
echo "alert('stuff')";
it might then print it out like: $("#commentinput2").load("editcomments2.php")alert('stuff') and the code will get confused. So lastly I need to added a semicolon to prevent the program from reading all my code as just 1 line:
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';

Related

How do I add a php variable to a onclick function argument?

Beginner JS here.
I am trying to add a PHP variable to a Javascript onclick function. I converted the PHP variable to a JS variable just fine. However, when adding the JS variable to the function I'm not receiving the desired output. What am I doing wrong?
<script>
js_logo_number = "<?php echo $logo_number; ?>";
</script>
<img src='<?php echo $image1[0]; ?>' onclick="openModal();currentSlide(js_logo_number)">
You can do like this :
<img src='<?php echo $image1[0]; ?>' onclick="openModal();currentSlide(<?php echo $logo_number; ?>)">
This code works for me. Be sure you use a let or const (or the outdated var) prefix to declare js_logo_number a new variable. Also make sure that $logo_number is set like this:
<script>
let js_logo_number = <?php isset($logo_number)?$logo_number:null; ?>;
</script>
Then make sure you convert it to an integer in Javascript, if that's how you want to use it:
function currentSlide(num){
console.log('currentSlide fired', parseInt(num));
}
let js_logo_number = "5";
function openModal(){
console.log('openModal fired');
}
function currentSlide(num){
console.log('currentSlide fired', parseInt(num));
}
<img src='https://placekitten.com/200/200' onclick="openModal();currentSlide(js_logo_number)">
When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.
See: PHP tags
1) echo
<?php echo 'if you want to serve PHP code in XHTML or XML documents, use these tags'; ?>
Solution:
<img src='<?php echo $image_src ?>'
onclick="openModal();currentSlide(<?php echo $logo_number ?>)">
2) Short echo
You can use the short echo tag to <?= 'print this string' ?>.
It's equivalent to <?php echo 'print this string' ?>.
Solution:
<img src='<?= $image_src ?>'
onclick="openModal();currentSlide(<?= $logo_number ?>)">
3) If short_open_tag is enabled
<? echo 'this code is within short tags, but will only work '
.'if short_open_tag is enabled'; ?>
Solution:
<img src='<? echo $image_src ?>'
onclick="openModal();currentSlide(<? echo $logo_number ?>)">
See: PHP tags

echo alert() in php show everything in the string. octobercms plugin

I'm having issue when echoing alert() in php. It show everything in the string.
echo "<script type='text/javascript'>alert('Thank you for your enquiry, we will contact you as soon as possible, have a nice day!');</script>";
Output
You can use javascript code between ending and starting php tags like below,
?>
<script type="text/javascript">
alert('Thank you for your enquiry, we will contact you as soon as possible, have a nice day!');
</script>
<?php
// rest of the php code will go here.
?>
OR try this please,
<?php
echo '<script type="text/javascript">';
echo 'alert("Thank you for your enquiry, we will contact you as soon as possible, have a nice day!");';
echo '</script>';
?>
Solution: I fixed it by removing everything. and just echo the message directly without the alert(). I'm not sure why it behave like this because this is my custom plugin. I will update if I found the source of this behavior. Thank you for all the replies.
echo 'Email Successfully Sent. We will reply soon!';

How can I use HTML code to print it in PHP?

I want to use this HTML code and print it in PHP.
In the HTML code I use this:
<button onMouseover="htmlcode('<img src=\'http://www.chinavalue.net/Special/images/20080529/image/top.jpg\'></img>');">View</button>
How should it be used it in PHP?
I've tried this code:
echo '<button onMouseover="htmlcode('<img src=\'http://www.chinavalue.net/Special/images/20080529/image/$test.jpg\'></img>');">View</button>';
But unfortunately, this is not working.
you mix up your quotes
if you use ' quotes in the echo statement, you cannot use them (or have to escape them) in the string
the best solution here is escape the single quotes in your string, so they don't mark the end of the echo command
echo('<button onMouseover="htmlcode(\'<img src=\'http://www.chinavalue.net/Special/images/20080529/image/top.jpg\'></img>\');">View</button>');
If you want to use it in some kind of an if statement then you can go for this:
<?php if( /*your condition*/ ) :?>
<button onMouseover="htmlcode('<img src=\'http://www.chinavalue.net/Special/images/20080529/image/top.jpg\'> </img>');">View</button>
<?php endif; ?>
Note this way you can write long snippets of html easily, which is not the case with echo.
If this is not conditional, then just do something like this:
<?php // some php code
// some more php
?>
<html>
Your html here
</html>
<?php // next section of php
// more php
?>
The parts between the php sections will be outputed just as if they were put into an echo statement.
If you want to see the html in the browser then you can use.
print htmlspecialchars("<button onMouseover="htmlcode('<img src=\'http://www.chinavalue.net/Special/images/20080529/image/top.jpg\'></img>');">View</button>")

PHP - echo JavaScript is not working

Hello everyone !
I'm working on a board based on bootstrap, i've got a database filled with sounds informations (name / genre / link soundcloud ...)
In my PHP page, I echo a button which call a JS function with a PHP argument inside.
echo "<button onclick='SCM.queue({title:'".$name[$j]."',url:'".$liensc[$j]."'});'>Click me</button>";
My problem is, when I look at the source code of my page, I'm supposed to have something like :
<button onclick="SCM.queue({title:'Marvin Gaye - Sexual Healing (Kygo Remix)',url:'http://soundcloud.com/kygo/marvin-gaye-sexual-healing'});">Click me</button>
But instead i've got :
<button onclick="SCM.queue({title:" marvin="" gaye="" -="" sexual="" healing="" (kygo="" remix)',url:'http:="" soundcloud.com="" kygo="" marvin-gaye-sexual-healing'});'="">Click me</button>
So, I'm looking for the right synthax :)
Hope someone can help me !
Thank you all
You quote improperly
Try this
echo "<button onclick='SCM.queue({title:\"".$name[$j]."\",url:\"".$liensc[$j]."\"});'>Click me</button>";
https://eval.in/139055
Try as
echo '<button onclick="SCM.queue({title:\''.$name[$j].'\',url:\''.$liensc[$j].'\'});">Click me</button>';
use
echo "<button onclick=\"SCM.queue({title:'".$name[$j]."',url:'".$liensc[$j]."'});\">Click me</button>";
try this
echo "<button onclick=\"SCM.queue({title:'".$name[$j]."',url:'".$liensc[$j]."'});\">Click me</button>";
Example
Use this:
echo "<button onclick='SCM.queue({title:'{$name[$j]}',url:'{$liensc[$j]}'});'>Click me</button>";
If I isolate the code like so:
<html>
<body>
<pre>
<?php
$name[1] = 'Marvin Gaye - Sexual Healing (Kygo Remix)';
$liensc[1] = 'http://soundcloud.com/kygo/marvin-gaye-sexual-healing';
$j = 1;
$line = "SCM.queue({title:'".$name[$j]."',url:'".$liensc[$j]."'});";
echo $line . "\n";
echo "<button onclick='SCM.queue({title:'".$name[$j]."',url:'".$liensc[$j]."'});'>Click me</button>";
?>
</pre>
</body>
</html>
$line and the button show as:
SCM.queue({title:'Marvin Gaye - Sexual Healing (Kygo Remix)',url:'http://soundcloud.com/kygo/marvin-gaye-sexual-healing'});
Which means your syntax is correct.
I'm wondering if you've somehow managed to enter the line you provided in this example correctly but it is different to the one you're using in your real app!
It may also be worth checking that the $name[] variable is indeed passing normal string data.
Normally, I'd make sure that something in the onclick='' part is a variable as it makes it simpler to read because you're not getting as lost in nested single and double quotes.
eg:
$onclick = "SCM.queue({title:'" . $name[$j] . "',url:'" . $liensc[$j] . "'});";
echo "<button onclick='$onclick'>Click me</button>";
Several of the other answers have suggested escaping the quotes out (with a \") which is good, but its probably still better to not have it inline in the echo statement, unless you're really sure of understanding what you're trying to do!
Edit:
Also see: https://eval.in/139090
for an exapmple

include variable in javascript?

I need some help with the following.
I have a script that close down the web page using the following:
<a href=\"Javascript:void(0);\" onclick=\"parent.location.reload();window.parent.Shadowbox.close();\">
It works as it should. My problem is that I need to change parent to another page. For this I used the following:
<a href=\"Javascript:void(0);\" onclick=\"parent.location='http://www.google.com';window.parent.Shadowbox.close();\">
It also works, but now I do not want it to go to google but one of my pages where it should include a variable. Normally I would use:
<?=$array["id"]?>
But since it is a javascript then I cant use PHP and the following solution does not work:
<a href=\"Javascript:void(0);\" onclick=\"parent.location='new-new.php?$array[‘id’];window.parent.Shadowbox.close();\">
So I tried this:
function getNewURL() {
var root="http://minside.net";
var id="topic/#entry667119";
var nURL=root+"/"+id;
return nURL;
}
With the javascript:
<a href=\"Javascript:void(0);\" onclick=\"parent.location.replace(getNewURL);window.parent.Shadowbox.close();\">
For in this way to get my variables with me, but it does not work.
Someone who can help me? Or have another idea for how to resolve this?
you can assign a php variable to javascript variable.
var a=<?php echo $array[‘id’]?>;
you are using echo for that right since you use \ for ", so it should be like this:
<a href=\"Javascript:void(0);\" onclick=\"parent.location='new-new.php?'.$array[‘id’]'.window.parent.Shadowbox.close();\">
The best way to assign a variable in Javascript is
var id = <?php echo json_encode($array["id"]); ?>
You need to use echo:
<? echo $array["id"]; ?>
Edit
var id = <?php echo $array["id"]; ?>;
var link = "<a href=\"Javascript:void(0);\" onclick=\"parent.location='new-new.php?id="+id+";window.parent.Shadowbox.close();\">";
assuming you use $_GET['id'] in new-new.php (you omitted it in the URL)
Edit
if(strlen($large_photo_exists) > 0 && strlen($thumb_photo_exists) > 0)
{
echo $thumb_photo_exists;
echo "<p>Use this photo as my profile photo</p>";
echo "<br> OR <br>";
echo "<p>Upload and use another photo</p>";
}

Categories