I am using CKEDITOR for the advanced Post Insert purpose, when i am going to update a Post then i want to fetch text to CKEDITOR from the database. So how can i set the value to CKEDITOR?
Here is the Code which i tried(Failed),
<script>
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace('post_value');
CKEDITOR.instances['editor1'].setData(<?php echo $obj->postdata; ?>);
</script>
The instance key should be the same as the element ID
Try changing:
CKEDITOR.instances['editor1'].setData(<?php echo $obj->postdata; ?>);
to
CKEDITOR.instances['post_value'].setData(<?php echo $obj->postdata; ?>);
This assumes that $obj->postdata; returns an html string also
<script>
CKEDITOR.replace('editor1');
CKEDITOR.instances.editor1.on('instanceReady', function(evt) {
evt.editor.setData('<?php echo $obj->postdata; ?>');
});
</script>
or
<textarea id="editor1"><?php echo $obj->postdata; ?></textarea>
.
.
.
<script>
CKEDITOR.replace('editor1');
</script>
Either way, make sure the script comes after textarea.
Related
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
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")';
Ive struggled with a bug which I can simplify to this:-
if I set a variable $test in php and try to access it in javascript it sometimes works, but often doesnt
Im using alert to display the data in javascript... alert( php tag echo $test; close php tag ) if it doesn't show below
<script>
alert(<?php echo $test; ?>);
</script>
example, if in php I set $test="hello"; it does not work. Nothing!
but if I set $test=time(); it does work
why cant I set a simple string and access it in javascript? Its strange it can access a more complicated timestamp but not a string!!
You need to tell JavaScript that you want to alert a string.
alert('<?php echo $test; ?>');
Notice the quotes wrapped around the PHP statement.
You're missing out "".Try this instead.
<script>
alert("<?=$test?>");
</script>
You need to add quotes
alert('<?php echo $test; ?>');
I am trying to change html button's text with php and javascript but it doesn't give errors and i don't know what is wrong.
My code that i am trying to get working.
At start of the file:
<?php
session_start();
$_SESSION["LOGGED"] = "Log in";
?>
Then comes the button code
<button class='button' id='login'>button</button>
<script type="text/javascript">
<?php
echo "var msg = '" .$_SESSION["LOGGED"] . "'";
?>
document.getElementById('login').innerHTML = msg;
</script>
What is wrong? The text just doesn't change.
Edit: The 2 sciprts php part crashes the script? It says unexpected token <. Removed the php part and putted to .innerHTML = "test"; and the button changed to test. So it should be somewhere at the php part?
If you're using the <button> tag,
document.getElementById('login').innerHTML = msg;
should work.
But if you're using <input type="button">, then you will have to use .value instead of .innerHTML
document.getElementById('login').value = msg;
EDIT: Try this...
<script>
var msg = <?php echo $_SESSION["LOGGED"]; ?>; //Don't forget the extra semicolon!
document.getElementById('login').innerHTML = msg;
</script>
Is your file a .php file and does the php work correctly?
try testing your php by putting
<?php echo "Hello world!"; ?>
at the top of your page :)
I am using PHP to post into a textarea:
<textarea name="">
<?php echo $name?>
</textarea>
I have tried to do this.
Jquery:
var yo = "Hello"
$.post(window.location, {variable: yo});
PHP:
<?php
$name = $_POST['variable'];
?>
Why does this not write "Hello" in the textarea / Why is the php not receiving the .post?
can you try <?php $_SERVER['PHP_SELF']); ?> as URL at your jQuery post function instead of window.location as follow
$.post(<?php $_SERVER['PHP_SELF']); ?>, {variable: yo});
Assuming you want to change a value inside a textarea with a value you calculate or set in javascript, you can assign it this way using JQuery:
<script>
var yo = "hello";
$("#test").html(yo); //where test is the ID from the textarea
</script>
<textarea id="test">
</textarea>
FIDDLE: http://jsfiddle.net/YcgsR/
You can modify your logic a bit here by making your PHP script take in your JS variable and do some logic/manipulation/query/whatever (that you can't do via JS) and return a value which you want to appear in the textarea. You can then leverage this return value via an AJAX request like so:
var yo = "Hello";
$.post(window.location, {variable: yo}, function(value) {
$('textarea').val(value);
});
and in your PHP script, something like:
<?php
$name = $_POST['variable'];
/*
* do manipulation here that you can't do purely via JS
* otherwise why would you be posting to a PHP script?
*/
echo $some_return_value;
?>