jQuery Show php GET variable value in a textarea dynamically - php

Im trying to show GET variable data in a textarea. I tried may solutions in StackOverflow but none of them worked. I have no idea why. Can you please tell me if I'm missing something.
My Story
I have a link on a page and href is /index.php?page=contact-us&performance=Lamborghini
and it takes me to the Contact Us page and the url is still /index.php?page=contact-us&performance=Lamborghini which is what I want.
So, Im tyring to grab performance variables's content which is Lamborghini and print it in a textarea which is
<textarea name="m62b34fbrp__32" cols="19" rows="7" class="cms_textarea" id="fbrp__32"></textarea>
So what I did is added <?php $performanceInfo= $_GET['performance']; ?> to the top of my page. Then added $("#fbrp__32").val(<?php echo $performanceInfo; ?>); in my script.js file. But it never prints Lamborghini in the textarea.
When I try $("#fbrp__32").val('<?php echo $performanceInfo; ?>'); (Notice the ' marks) it prints <?php echo $performanceInfo; ?> in the textarea.
When I <?php echo $_GET["performance"]; ?> it prints Lamborghini fine too.
Can you guys tell me why its not working for me?
NOTE : I cant edit the textarea manually as it is generated using a plugin Thanks a lot.

PHP Parse will not parse .js files, you will need to do this outside of the .js file. If not you will need to in your webserver config file set to parse .js files as PHP files.
EDIT
You mentioned that you added <?php $performanceInfo= $_GET['performance']; ?> to the top of the page and then $("#fbrp__32").val(<?php echo $performanceInfo; ?>); to your script.js file. This will not work as per default configuration script.js or any .js file will not be interpreted by the PHP parser. so your statement $("#fbrp__32").val(<?php echo $performanceInfo; ?>); will cause a syntax error.
The best way to handle it without changing server configuration would be to do this at the top of your page.
<script type='text/javascript'>
var performance = "<?php echo $_GET['performance']; ?>";
</script>
and in your script.js file do $("#fbrp__32").val(performance);

Try this
<textarea name="m62b34fbrp__32" cols="19" rows="7" class="cms_textarea" id="fbrp__32">
<?php echo $_REQUEST['performance']; ?></textarea>
OR in JS
$(document).ready(function(){
var performance='<?php echo $_REQUEST["performance"]; ?>';
$("#fbrp__32").val(performance);
});

Just add the following right before the textarea (in your PHP/HTML-file, not JS-File!):
var performance = <?php echo '"' . htmlspecialchars( $_GET['performance'] ) . '"'; ?>
$("#fbrp__32").val(performance);

Just set value directly using PHP
<?php
$performanceInfo= $_GET['performance'];
?>
JQuery is a bit tricky.Instead try simple javascript for setting value:
document.getElementById("Enter Here Textarea Id").value = <?PHP echo $_GET['performance']; ?>

Ok. Finally this is the solution which worked for me.
In the php file I put
<?php
$performanceInfo= $_GET['performance'];
echo "<script>";
echo "$('#fbrp__32').val('$performanceInfo');";
echo "</script>";
?>
Its weird other solutions didnt work for me for somereason.

Try this
var value = <?php $performanceInfo= $_GET['performance']; ?>
$("#fbrp__32").val(value);
NB: php tags will not not render in js files

Related

How to echo javascript code which contains php tags inside?

I usually echo script alert by simply
echo '<script type="text/javascript">alert("'.$my_message.'");</script>';
but how to echo the script when the script contains php tags? (see my example code)
Because of my php obfuscator script, I have to use only one <?php ?> tag. so in this case I need to echo the javascript without having <?php tags. What are the possible solutions in this case?
<?php
...some php code...
<script type="text/javascript">
jQuery(document).ready(function($){
$i = 0;
$('.wrapper_vertical_menu .megamenu_menu').append('<div class="more-wrap"><span class="more"><?php echo $this->__("More"); ?></span></div>');
$('.wrapper_vertical_menu .megamenu_menu > li.megamenu_lv1').each(function(){
$i ++;
if($i>13){
$(this).css('display', 'none');
}
});
... more js code ...
JavaScript doesn't "contain PHP tags". All your PHP code needs to do is build the resulting output string (which happens to be JavaScript code, but that doesn't matter to PHP) and echo it.
So where you have something like this:
echo "some javascript code <?php echo some_php_value; ?> more javascript code";
What you really want, quite simply, is this:
echo "some javascript code " . some_php_value . " more javascript code";
Don't try to nest <?php ?> tags within each other. Just concatenate the actual output you want and echo that output.

get activeTextArea value to Jquery in yii

I am new in Yii.
I have a view file with following code
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::activeTextArea($model,'adremarks');?>
<?php echo CHtml::submitButton('Swap',array('id'=>'swap'));?>
<?php echo CHtml::endForm(); ?>
I want to access the value of text area to my script, here my jQuery script
var adremarks= $("textarea#adremarks").val();
alert(adremarks);
But I cant get the value there, Is this the right way else how can I get a text area value to script
Thanks in advance
The problem is your activeTextArea does not have html ID. You need to add id in this way:
<?php echo CHtml::activeTextArea($model,'adremarks', array("id" => "adremarks"));?>

PHP How to edit HTML element Content?

Just like $('#elementid').html('html code'); but in PHP. Is there a way to change live content with PHP without reloading page? Tried
<?php
echo '<script type="text/javascript">
$('#element').html('<p>I am a text</p>');
</script>'
?>
<div id='element'>I will be changed!</div>
Is there any shorter way, that one also not works everytime.
$.post('test.php','',function(response){
$('#element').html(res);
});
test.php
$name = "sham";
echo "this is my variable $name";
Output of Html
<div id='element'>this is my variable sham</div>

Echoing PHP TAG showing blank output HTML

I am testing this on windows 7 xammp 1.8.1 php 5.4.7
I am trying to show dynamic php codes in html as example
my code is
<?php
$output="<?php echo $ti ?>";
echo $output;
?>
but output html is blank! i am not sure if its a bug, can some help me.thanks in advance
When you run this, this DOES produce an output, which is a blank page, because the output is:
<?php echo ?>
To a browser which renders html, it will look like an open tag with nothing in value.
Run your script and view the page source...
You need to use single quotes
i.e.
change
<?php
$output="<?php echo $ti ?>";
echo $output;
?>
to
<?php
$output='<?php echo $ti ?>';
echo $output;
?>
Just change your double quotes to single quotes:
<?php
$output='<?php echo $ti ?>';
echo $output;
?>
Example: http://ideone.com/bUJAxb
There are two things you have to change. First, if you use double quotes PHP will evaluate variables in it, so your output will be <?php echo ?>:
$output='<?php echo $ti ?>';
Now the output will be <?php echo $ti ?>.
Next, the browser will interpret this as HTML and since it is only a single tag it will display nothing. You need to run this through htmlentities():
echo htmlentities($output);
This will output <? echo $ti ?;gt; which will be displayed by the browser in the way you intend it.
You need to escape the characters
This can be done by adding the entire line you want to output in an htmlentities function call, like:
$output = htmlentities("<?php echo \$ti ?>");

cant write varibles into php file using file_put_contents PHP

I'm trying to edit a file using the file_put_contents
Everything works fine (the data shows up in the test.php, well most of it) but the variables do not.
heres an example of my problem
code for the file that will perform the file_put_contents:
<?php
file_put_contents("test.php","<?
$title = "title_name_goes_here"
echo $title;
?>");
?>
Code that shows up in the target file (test.php):
<?
= this_is_my_name
echo ;
?>
What should show up in test.php:
<?
$title = "title_name_goes_here"
echo $title;
?>
Also, I'm using Dreamweaver to write the code in and it seems to have problems (code errors) when i insert the quote for the $title's value so its ok if i used $title = title_name_goes_here but it doesn't like $title = "title_name_goes_here". When i say its okay, i mean dreamweaver doesn't show any code errors but it still doesn't do what it should.
any ideas?
Escape the quotes (and the dollar signs):
<?php
file_put_contents("test.php","<?
\$title = \"title_name_goes_here\"
echo \$title;
?>");
?>
If you don't want to see the ugly escapes just use single quotes:
<?php
file_put_contents('test.php",'<?
$title = "title_name_goes_here"
echo $title;
?>');
?>

Categories