Posting a javascript variable into a text area using PHP - javascript

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

Related

Auto-fill form on WordPress

I am using the Scripts n Styles plugin with this code to auto-fill a logged in user's email into form on WordPress:
<script type="text/javascript">
window.onload = function () {
document.getElementsByName("Element-Name")[0].value = "<?php $current_user->user_email ?>";
}
</script>
However, when I refresh the page I get the following text (the quoted value in the code above) instead of the actual value.
<?php $current_user->user_email ?>
Any idea what I am missing here? Is this an issue with the plugin?
The first thing I see is that you don't output anything.
Try
<?php echo $current_user->user_email; ?>
But in your example it should have been just an empty value. There is something fishy with the PHP parser not detecting the PHP part.

how to call php function through hypertext link

I want to call a PHP function passing an argument on clicking a hyperlink (text with <a> tag) on the same page, i.e. href='#' onclick='loadpic($id).
Where $id is the variable to be passed to PHP function.
You can do that by ajax easily. Create a loadpic function in javascript instead and then pass $id to the php file via ajax and do something when you get the result.
Your javascript file should look like this-
(make sure you've inserted a link for jquery in your index file)
function loadpic(id_param){
$.post("filename.php",
{
id: id_param
},
function(data){
alert("Data received:" + data);
});
};
The data parameter in function(data) would be the value that you've echoed in the php file.
If you want to use onclick() method in your HTML element then you have to write your method is javascript and make an AJAX call to PHP. THis is one simple way to do this using php and javascript. And for this please see javascript and ajax with PHP.
But if you dont want to use javascript then here is a simple way and very simple code.
<?php
if(isset($_GET['myMethod'])){
myMethod($_GET['id']);
}
function myMethod($id){
echo $id."<br>";
}
?>
<?php
$myId = 4;
?>
<a href="index.php?id=<?php echo $myId ?>&myMethod" >MyLink</a>
Note: Copy this code and paste into a file called index.php because notice the hyper link tag and its attribute href="index.php".
Also note that you can not directly call php method using js like onclick() from your HTML tag. You should use AJAX or something. But using above demo code you can easily call myMethod() which is a PHP method using the hyper link tag.
In your case:
<?php
if(isset($_GET['call_loadpic'])){
loadpic($_GET['id']);
}
function loadpic($id){
echo "My loadpic method has been called! and the id is".$id."<br />";
}
?>
<!-- your other codes -->
<?php
$id = 4;
?>
<a href="this-same-file-name.php?id=<?php echo $id ?>&call_loadpic" >MyLink</a>
This is because your php function is in the same page and you want to call that function using same file and hyperlink in the same file. (According to your question)
You still will have to make javascript handle the exception because php has no onclick.
onClick=\"loadpic("'.$id.'");"\
//this will only send a php variable javascript will still have to loadpic();

How to Set inner data to CKEDITOR using PHP

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.

How to pass the value from url of php to .js

Hi I am new to javascript and I am trying to pass a value from Get in url of php to javascript.
This is my code.
this is the url
someproject/index.php?roomId=2
index.php
room = $_GET['roomId'];
<input hidden id="room" value=<?=$room;?>>
script.js
I want to get the value from php and I want something like:
$('#room').getValueFromPHP();
but I do not know how to do it. Please help me.
This should do it:
PHP
<input type="hidden" id="room" value="<?=$room;?>">
JavaScript
$(function() {// make sure the document is ready.
var url = $("#room").val();
});
In order to pass a value from PHP to JavaScript, you can use echo as follows:
<?php $room = $_GET['roomId']; ?>
<script> var room = <?php echo "'$room'"; ?>; </script>

JavaScript innerHtml not changing to php session

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 :)

Categories