I am struggling with including this javascript snippet into PHP code and make it work.
How can I change it to not only successfully include it but also get it to work as expected?
<a class="popuptweet" href="http://twitter.com/share?url=http%3A%2F%2Fdev.twitter.com%2Fpages%2Ftweet-button" onclick="return popitup('http://twitter.com/share?url=http%3A%2F%2Fdev.twitter.com%2Fpages%2Ftweet-button')"></a>
Good challenge!
What about this code? If and only if I understood your question right!
<html>
<head>
<script type="text/javascript">
function popitup(link){
window.showModalDialog(link, "", "dialogWidth:500px; dialogHeight:400px; center:yes");
return false;
}
</script>
</head>
<body>
<?php
echo "<a class=\"popuptweet\" href=\"http://twitter.com/share?url=http%3A%2F%2Fdev.twitter.com%2Fpages%2Ftweet-button\" onclick=\"return popitup('http://twitter.com/share?url=http%3A%2F%2Fdev.twitter.com%2Fpages%2Ftweet-button')\">Click Me!</a>";
?>
</body>
</html>
<?php
echo "<a class=\"popuptweet\" href=\"http://twitter.com/share?url=http%3A%2F%2Fdev.twitter.com%2Fpages%2Ftweet-button\" onclick=\"return popitup('http://twitter.com/share?url=http%3A%2F%2Fdev.twitter.com%2Fpages%2Ftweet-button')\"></a>";
?>
Should work. It's hard to give you more help with the small amount of information you've provided.
Related
I didn't find any post related to my problem, so here I go :
I added trumbowyg (it's a WYSIWYG editor) to edit the content in my <textarea></textarea>, and it works just fine when I post it in my database.
Only problem is : how do I echo it ?
The parsing method of trumbowyg takes this form : you click on, let's say B in the toolbar on top of the textarea, and it will put your text in bold weight. But in the server, once posted, it takes actually this form : <strong>some text</strong>.
Obviously, when I echo the var stocking the data in this part of my sql request, it output it the same way : <strong>some text</strong> and not some text.
I don't know if it's actually so simple that I can't seem to find the solution, or if I'm trying something impossible... ?
Thanks by advance guys !
Well... Guess it was so obvious that I didn't find the answer in here. If it can help people who find themselves in the same situation as me : just wrap your var containing html with html_entity_decode($var)
That's it.
See below (textarea is showed if the user consulting the profile is the one wich it belongs to, else it just echo the descrption (called in an Action.php file, I didn't put the "requires" before the <!DOCTYPE html> declaration.
<!DOCTYPE html>
<html lang="en">
<?php include "includes/head.php"; ?>
<body>
<?php include ("includes/navbar.php") ?>
<div class="container">
<h2><?= $user_pseudo;?></h2>
<h5><?= $user_access_level;?></h5>
<?php
if($_SESSION['id'] == $user_id){
?>
<form method="POST">
<textarea id="parse" name="description"><?= $user_description; ?></textarea>
<button class="btn btn-primary" name="validate" type="submit">Mettre à jour</button>
</form>
<?php
} else {
?>
<br/><br/><br/>
<div class="container">
<?= html_entity_decode($user_description) ?>
</div>
</div>
<?php
}
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-3.3.1.min.js"><\/script>')</script>
<script src="trumbowyg/dist/trumbowyg.min.js"></script>
<script>
$('#parse').trumbowyg();
</script>
</body>
</html>
and thank you in advance for your great work here, i need help with some coding, i got a web page where i show a list of records of a mysql table, with a hyperlink when you click on it creates a variable like world.php?id=1&xx=1050&yy=300 so when the user clicks it takes him to next page and scroll down and up to the x and y it, works fine in web browsers but not working in mobiles. can you help me? the page world.php has
,)">
this is the code i'm using
echo '<a href="world.php?xx='. $_SESSION['x'] .'&yy='. $_SESSION['y'] .'">';
echo '<b>';
echo $row['usuario_atacante'];
echo '</b>';
echo '</a>';
Can you do something like this ?
Row 5
and make links in your world.php file.
<table>
<tr id="row1"><td><h2>Row 1</h2></td></tr>
<tr id="row2"><td><h2>Row 2</h2></td></tr>
<tr id="row3"><td><h2>Row 3</h2></td></tr>
<tr id="row4"><td><h2>Row 4</h2></td></tr>
<tr id="row5"><td><h2>Row 5</h2></td></tr>
</table>
Use a link like: world.php?x=345&y=200 to scroll to that location.
<?php
if(isset($_GET["x"]) && isset($_GET["y"])) {
$x = $_GET["x"];
$y = $_GET["y"];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>World Map</title>
<script>
function scrollWin() {
window.scrollTo(<?php echo $x.",".$y; ?>);
}
</script>
</head>
<body onload="scrollWin()">
<img src="https://upload.wikimedia.org/wikipedia/commons/e/e2/Geoffroy's_Cat.jpg" />
</body>
</html>
This is just a example to get you started.
I'm using this line of php in my main page
echo generateRadioButtons("fbresponse.php", "moRating1", 6);
Which when posting the following on the response file
echo $_POST['moRating1']
It works fine and displays the correct result, but! my question is how would i add text to that so..
Blah blah blah, you rated x question: 'moRating1'
I've tried doing
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $moRating1 ?></p>
</body>
</html>
inside the response file but that just doesnt load anything..
Any help please!
It's probably because this function uses eval() to execute its content (I guess it from lack of PHP tags in your first example).
If it's true, then you should be able to close PHP tag, print HTML and open it again.
?>
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $_POST['moRating1'] ?></p>
</body>
</html>
try doing:
$mRating1 = $_POST['moRating1'];
...
?>
...
<p>How well did you rate it: <?php echo $mRating1?></p>
My PHP is not PHPing, so made simple test... must be missing something obvious.
<html>
<head>
</head>
<body>
<?php echo '<script language=\"javascript\">confirm("Do you see this?")</script>;'; ?>
</body>
</html>
In code body, I get: confirm("Do you see this?");'; ?>
When I "View Source", I see:
<html>
<head>
</head>
<body>
<?php echo '<script language=\"javascript\">confirm("Do you see this?")</script>;'; ?>
</body>
</html>
what extension has your file? is a webserver running? how are you calling your php script?
make sure it has a .php extension, the webserver is running, the file resides under the webroot directory and you call it via http://localhosti/path/to/file.php
also make sure you don't escape quotation marks when not needed, echo '<script type="text/javascript">…</script>'; should do the job
You should remove the backslashes from the \"javascript\".
<?php echo '<script language="javascript">confirm("Do you see this?")</script>;'; ?>
In PHP you can put strings in single ' or double " quotes. This is quite hard to explain (and/or understand) in a few lines, so here's a few valid ways to write down a string containing quotes:
echo 'This has "double quotes"...';
echo 'This has \'single quotes\'...';
echo "This has \"double quotes\"...";
echo "This has 'single quotes'...";
There are many more subtleties to this, but this should get you started.
Take out the \ from the double quotes and the extra semi colon
<html>
<head>
</head>
<body>
<?php echo '<script language="javascript">confirm("Do you see this?")</script>'; ?>
</body>
</html>
I am new to PHP. I need to output the following JavaScript with PHP. This is my code:
<html>
<body>
<?php
echo "<script type="text/javascript">";
echo "document.write("Hello World!")";
echo "</script>";
?>
</body>
</html>
But it's showing the error:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /var/www/html/workbench/person/script.php on line 4
Can anyone please help? I also need some simple tutorials on how to use PHP, HTML and JavaScript for an application.
You should escape the JavaScript string delimiters inside the PHP string. You're using double quotes for both PHP and JavaScript strings. Try like this instead:
<html>
<body>
<?php
// Here, we use single quotes for PHP and double quotes for JavaScript
echo '<script type="text/javascript">';
echo 'document.write("Hello World!")';
echo '</script>';
?>
</body>
</html>
You have to escape quotes on both JavaScript and PHP when the string delimiter are the same as the quotes:
echo "\""; // escape is done using a backslash
echo '\'';
Same in JavaScript:
alert("\""); // escape is done using a backslash
alert(echo '\'');
But because it's very hard to read a string with such escape sequences, it is better to combine single with double quotes, as needed:
echo '"';
echo "'";
The error you get if because you need to escape the quotes (like other answers said).
To avoid that, you can use an alternative syntax for you strings declarations, called "Heredoc"
With this syntax, you can declare a long string, even containing single-quotes and/or double-quotes, whithout having to escape thoses ; it will make your Javascript code easier to write, modify, and understand -- which is always a good thing.
As an example, your code could become :
$str = <<<MY_MARKER
<script type="text/javascript">
document.write("Hello World!");
</script>
MY_MARKER;
echo $str;
Note that with Heredoc syntax (as with string delimited by double-quotes), variables are interpolated.
Another option is to do like this:
<html>
<body>
<?php
//...php code...
?>
<script type="text/javascript">
document.write("Hello World!");
</script>
<?php
//....php code...
?>
</body>
</html>
and if you want to use PHP inside your JavaScript, do like this:
<html>
<body>
<?php
$text = "Hello World!";
?>
<script type="text/javascript">
document.write("<?php echo $text ?>");
</script>
<?php
//....php code...
?>
</body>
</html>
Hope this can help.
An easier way is to use the heredoc syntax of PHP. An example:
<?php
echo <<<EOF
<script type="text/javascript">
document.write("Hello World!");
</script>
EOF;
?>
You need to escape your quotes.
You can do this:
echo "<script type=\"text/javascript\">";
or this:
echo "<script type='text/javascript'>";
or this:
echo '<script type="text/javascript">';
Or just stay out of php
<script type="text/javascript">
You need to escape the double quotes like this:
echo "<script type=\"text/javascript\">";
echo "document.write(\"Hello World!\")";
echo "</script>";
or use single quotes inside the double quotes instead, like this:
echo "<script type='text/javascript'>";
echo "document.write('Hello World!')";
echo "</script>";
or the other way around, like this:
echo '<script type="text/javascript">';
echo 'document.write("Hello World!")';
echo '</script>';
Also, checkout the PHP Manual for more info on Strings.
Also, why would you want to print JavaScript using PHP? I feel like there's something wrong with your design.
The following solution should work quite well for what you are trying to do.
The JavaScript block is placed very late in the document so you don't have to
worry about elements not existing.
You are setting a PHP variable at the top of the script and outputting just
the value of the variable within the JavaScript block.
This way, you don't have to worry about escaping double-quotes or HEREDOCS
(which is the recommended method if you REALLY must go there).
Javascript Embedding Example
<div id="helloContainer"><div>
<script type="text/javascript">
document.getElementById('helloContainer').innerHTML = '<?= $greeting; ?>';
</script>
You want to do this:
<html>
<body>
<?php
print '
<script type="text/javascript">
document.write("Hello World!")
</script>
';
?>
</body>
</html>
instead you could easily do it this way :
<html>
<body>
<script type="text/javascript">
<?php
$myVar = "hello";
?>
document.write("<?php echo $myVar ?>");
</script>
</body>
You are using " instead of ' It is mixing up php syntax with javascript. PHP is going to print javascript with echo function, but it is taking the js codes as wrong php syntax. so try this,
<html>
<body>
<?php
echo "<script type='text/javascript'>";
echo "document.write('Hello World!')";
echo "</script>";
?>
</body>
</html>
<?php
echo '<script type="text/javascript">document.write(\'Hello world\');</script>';
?>
Try This:
<html>
<body>
<?php
echo "<script type="text/javascript">";
echo "document.write("Hello World!");";
echo "</script>";
?>
</body>
</html>