Get variable from PHP and use it on javascript - 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.

Related

My php is not interpreted in my Javascript

I create my HTML this way
$('<form>')
.attr('method',"POST")
.attr('action',"postMetier.php?metier="+"<?php echo $GET['metier'] ?>")
How can I make the echo work ?
$GET variable doesn't exists, correct name is $_GET.
.attr('action',"postMetier.php?metier="+"<?php echo $_GET['metier'] ?>")
^
Of course, you JS code has to be in PHP file (or other which is parsed by server).
it is not $GET['metier'] but $_GET['metier'], if there is value recieved on your page by get method then echo $GET['metier'] will give you some value.
Note: if $GET['metier'] is your defined variable, then it should have some value in it so that it can be echoed.

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

How to encrypt php variable in javascript?

When I creat a php variable in javascript, I use this method:
var jArray = <?php echo json_encode($myArray); ?>;
It's very good but if i view the source code, ther is my full array in the script area.
My problem is that, my php array contains secret data, and I want to use this data in javascritp.
How can I hide from my sourc code, or what can I do?
I tride javascript obfuscation but it can't work with <?php ?> tag.
Thanks!

Assign a jquery value to text inside the same PHP passage

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

Javascript: How to not directly post a variable

I am in need of help again, this time with a little Javascript snippet. There's also a little bit of PHP involved. The PHP is as follows (to generate random numbers)
$no1 = rand(0,9);
$no2 = rand(0,9);
$no3 = $no1+$no2;
I'm using this as a captcha method, for a very simple contact form. I'm not a fan of the bulky methods that I find all over google searches.
To validate this code, I use the following Javascript
if(document.forms["feedback"].check.value !== "<?php echo $no3; ?>"){
window.alert("Incorrect security code");
return false;
}
Then inside my form, I just use this:
<?php echo $no1; ?> + <?php echo $no2; ?> = <input name="check" type="text" id="check" size="3">
It all works as it's supposed to, but if there was a way for me to not to just directly echo $no3 in my javascript here:
if(document.forms["feedback"].check.value !== "<?php echo $no3; ?>")
Then that would eliminate any way of viewing the source to find the answer. I'm very much aware that it's a simple math problem, and if anyone knows how to view source, they most definitely can add, but a colleague of mine pretty much insisted on this.
So, does anyone know a way to help me out? I pretty much wrote most of this by hand, including the javascript, so please be nice when telling me that my code is horrible and out dated..
Thanks!
Put the $no1 and $no2 variables inside of span tags (or anything else you can directly target with Javascript) and then get the values from those spans with JS and use that to calculate the sum in JS rather than echoing it into the source with PHP, e.g. give the spans IDs like #captcha-1 and #captcha-2 use getElementByID to retrieve the values, then do the validation entirely in Javascript.
It will still be easy for anyone with a brain to figure out how to bypass but at least the answer will not be present anywhere in the source code other than as a JS variable.
If you want to hide the validation process entirely, you will need to use JS and AJAX to send the sum input by the user to a serverside PHP function which checks to see if it is equal to $no1 + $no2 and then submit the form as part of the response success function.
All that being said, this is still a very weak spam deterrent method.

Categories

Resources