Assign a jquery value to text inside the same PHP passage - javascript

This presently resides inside some PHP in the web page source:
echo("<span class=\"somevalue\"></span>");
It works, the value is expressed on the page where the PHP instruction is invoked. But now--instead of an echo() announcement--how can the value instead be assigned to a text variable I can use elsewhere in the PHP? Thanks for any help.

You can assign the string to a variable
$mySpan = "<span class=\"somevalue\"></span>";
And then echo the variable anywhere later in the code. You can define $mySpan at the top of the script or define it in a separate file and include that file in any other file you will need the variable.

i think its work
echo '<span class=\"'.$somevalue.'"\></span>';

Related

Get variable from PHP and use it on javascript

I googled about it and didn't find an solution, can you help me with this issue?
I have a javascript variable that is the name of a php session address, and I want to use this session value in my javascript code, but I don't know how to pass this name to the php and return the session value to my js code.
I'm using the session to set values of my dynamic form fields. So I use
var field_name = "field";
document.getElementById(field_name).value = "<?php if(isset($_SESSION['field_name'])) echo $_SESSION['field_name'];?>";
The field ID have the same name of the session address. I can't simply use the word "field" because it's dynamic.
I thank you in advance.
Just use:
var info= <?php echo json_encode("some_php_variable_here", JSON_HEX_TAG); ?>;
This is probably only with a data attribute in the HTML. In the JS code one should definitely (actually never) run PHP code.
The data attributes can then be filled with PHP and read out in the JS.

TextArea saving to MySQL with php and showing out in html

I want to save value in a textarea to MySQL database using PHP. And then I want to pass it to a variable in javascript
The value in a textare could be like this:
Hi I'm Son!!
Nice to meet you
So my 1st question is: How can I add the text to my database using php with the absence of special chars? (Like which encoding...)
I'm gonna pass that text through an event of a hyperlink click. It's something like this:
<?php
echo '<a href="#" onclick=passExample('$myText')>Something</a>';
?>
After all, the javascript I coded is like this:
function passExample(Exa){
$("#ExampleShow").empty();
var node = document.createElement("p");
if (Exa=='') Exa=' (No example)';
var textnode = document.createTextNode(html_entity_decode(Exa));
node.appendChild(textnode);
document.getElementById("ExampleShow").appendChild(node);
}
However, the result is not the way I wanted it to be. Line breaks are missing. And if I type some character like ''", it results in errors. How can I fix this?
echo '<a href="#" onclick="passExample(\''.addslashes($myText).'\')";>Something</a>';
also read:
What's the best method for sanitizing user input with PHP? (for clearing too much data from user input).
Pass a PHP string to a JavaScript variable (and escape newlines) (for using php output in javascript and escaping it).

Pass variable to a javascript function in a c# code

I need to pass a variable to a javascript function,but I got a little trouble.
In the .cs file,I write like this:
string id = "someid";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction("+id+")\">"));
I need to use the value of this id,but in the console.log(id),it just shows "object",not the "someid",what's the problem?
Look at the generated HTML:
<input type="button" onClick="myFunction(someid)">
You are generating a variable name when you want a string literal.
Add some quote marks.
Whenever you have a problem that manifests in the browser: Look at the code the browser is dealing with first. You should always start by determining if the JS you want is not working or if the server side code is not generating the JS you want.
just add '' around id as i did below will resolve your issue
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));
Use the single quotes around id:
'"+id+"' (notice the single quotes then double quotes)
The problem is that it is looking for a saved variable on the page named someid, which is why you are getting the object in the console. Pranay Rana has a good solution which is to make sure the string someid is surrounded by single quotes.

In Yii, pass PHP variables to JavaScript

In my JavaScript (using jQuery) there are a whole set of PHP variables to which I need access. While I've got it working by just producing the JavaScript file as a view, and then using renderPartial() to echo the JavaScript inside the main view.
However, this is obviously not very elegant, so I would like to know the 'Yii' way of doing it. I've been looking at the Assets Manager but that seems to be for static JavaScript files - you can't include PHP in there (unless I'm wrong).
Is there another way of doing it?
You may consider registerScript. In my opinion, it is better since there is a param named $position, which can help you control the process output of render().
There's nothing inherently wrong or inelegant with your approach, and yes assets are static content (JS, CSS, etc) -- unrelated to the issue.
Fundamentally you can only expose the value of a PHP variable in JS by writing it as part of PHP code. If you will only need this value in a limited scope then you could just write it as an inline constant (which is what e.g. some widgets do). If you need it to be available throughout your JS code the only option is to produce JS code like you do now.
It's not strictly necessary to make a new partial view for your PHP-to-JS variables, but it's also not a bad idea. If you are happy with it, by all means use it.
The one of approaches is to set global variable by a small script and redisterScript(), and then use this variable in real js
Setting your variables in a key=>value array and using CJSON::encode works really well. You can access all your variables via the object created by jQuery's parseJSON. For example:
$myVarList = array(
'nameOne'=>$valueFromAnotherVar,
'nameTwo'=>$object->coolValue,
'nameThree'=>$cat->hoursSleptToday()
);
Yii::app()->clientScript->registerScript("myVarList",
'myVarList = jQuery.parseJSON('.CJSON::encode($myVarList).');'
You can then access the values from the global var.
myVarList.nameOne || myVarList.nameTwo || myVarList.nameThree
Refer to Yii2 - Client Scripts Documentation, you can use registerJS function to pass variables to javascipt. For example:
/* #var $this yii\web\View */
$this->registerJs(
"var calenderEvents = ".Json::encode($calenderEvents).";",
yii\web\View::POS_HEAD,
'calender-events-script'
);
Try to do this that way:
$myVarList = array(
'nameOne'=>$valueFromAnotherVar,
'nameTwo'=>$object->coolValue,
'nameThree'=>$cat->hoursSleptToday()
);
$json = addslashes(CJSON::encode($myVarList));
Yii::app()->clientScript->registerScript("myVarList", 'myVarList =
jQuery.parseJSON("' . $json . '");');

Javascript syntax to pass variables

Still muddling my way through Javascript, I'm trying to pass the contents of variable playnoyes to the long line of code below to decide whether or not to autoplay the flash movie, but doing it as I have below, the resultant line of code has the variable in quotes and therefore the code doesn't execute it as expected. My question is, how can I pass the variable so that the resulting line of code doesn't have the quotes around the variable value.
Many thanks, and sorry for the noobness of the question.
var playnoyes='true';
var testtext = "<script type='text\/javascript'>AC_FL_RunContent ('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width','320','height','220','id','HTIFLVPlayer','src','HTIFLVPlayer','flashvars','&MM_ComponentVersion=1&skinName=HTI_Skin&streamName=nigel&autoPlay=\""+playnoyes+"\"&autoRewind=true','quality','high','scale','noscale','name','HTIFLVPlayer','salign','lt','pluginspage','http://www.macromedia.com/go/getflashplayer','wmode','transparent','movie','HTIFLVPlayer');<\/script>";
alert (testtext);
Thats because you are explicitly adding the quotes:
change
nigel&autoPlay=\""+playnoyes+"\"&autoRewind=true'
to
nigel&autoPlay=" + playnoyes + "&autoRewind=true'
Try this:
var playnoyes='true';
var testtext = "<script type='text\/javascript'>AC_FL_RunContent ('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width','320','height','220','id','HTIFLVPlayer','src','HTIFLVPlayer','flashvars','&MM_ComponentVersion=1&skinName=HTI_Skin&streamName=nigel&autoPlay="+playnoyes+"&autoRewind=true','quality','high','scale','noscale','name','HTIFLVPlayer','salign','lt','pluginspage','http://www.macromedia.com/go/getflashplayer','wmode','transparent','movie','HTIFLVPlayer');<\/script>";
alert (testtext);
Remove the two '\"' on each side of the variable, no?

Categories

Resources