How to get a variable from php to JS and back [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
First of all, I'm relatively new to this so I'm sorry if I word stuff weird.
So let's say I have the variable $x with the value 1 in PHP. I want to get the variable to my JS-Script and modify it and then send the variable x with the modified value back to my PHP document. Note I don't want to send it to a server and I don't want to use AJAX (have yet to learn it). If there's no other way It'd be nice if someone could show me an example of how to do this with AJAX.

You should use AJAX if possible it is more comfortable and with jQuery not hard to use/understand. But if this topic is new for you, you can try to send it via a window.location.href.
I looked for some good examples for you:
PHP to JS
Without AJAX
Video - JS to PHP with jQuery POST
To save PHP variables from PHP to Javascript you often just print them. If you're more familar with Javascript and PHP, you should try to work with JSON-Strings. But I think that would be to much for your first try.
Hope that helps.

Related

Insert javascript alert into url [duplicate]

This question already has answers here:
Call Javascript function from URL/address bar
(14 answers)
Closed 2 years ago.
I am currently learning some web programming related stuff. I'm a little confused on how I insert some javascript into say a random URL. I'm trying to insert an alert message with an echo payload so I couldn't do javascript:alert("testtestesttest"); which will work.
Should this not work?
echo?payload=javascript:alert("test");
I am using an older version of my browser so that javascript execution is possible. But for some reason javascript:alert("test"); works by itself but when i add it onto the end of the url with the echo payload it's just echoing the text after the payload.
EDIT: I have found my solution. Sorry.
you can do it ) of course everything is possible! Not sure modern browsers will allow this request. Need to check.
Buth this approach is usafe from the user prospective.
Imagine that somewone will put tricky code and use your site domain as trusted and will send a spam using this link.
Hey bro here is discount you can get!
https://someknownSite.com?javascript::getyourpasswordcode
Then why do you want to make this happen via URL? Let say you have a page, called "myAlert.php".
So what you can do is, you can write a script on this page, which will simply show the alert when somebody with this URL will access this.
Or you can also, write a simple condition that when a request has been made to this URL, you can show the alert box.
But If you are trying to insert the javascript in the URL and expecting the result then it's not possible. Because modern browsers won't let you run a script as the "< >" symbols will be changed.

How to use javascript variable in php code in javascript function itself? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
I want to get php array element using javascript variable as an index of a php array. I have tried this below method but it doesn't work.
// I want to get value of $arr[0] and here array index is a javascript variable
<script type="text/javascript">
function select_seller()
{
d = document.getElementById('seller_id').value;
document.write("<?php echo $arr['<script>document.write(d)</script>'];?>");
}
</script>
Please Help me
You can't do this. PHP code is executed first so once the user loads the page, there's no way for PHP to detect a JavaScript variable.
If you posted some more code we could probably tell you how to do what you are trying to do in JavaScript.
You don't seem to know the difference between server-side and client-side, your php code is interpreted on the server to produce html data and send it to the browser to render it to the user, hence you cannot use php function on the client-side .. php role is preparing and sending data to the client-side.
so whatever you are trying to achieve here you have the option of using ajax calls to get data from the server and process it with js upon receiving it.

Accesing JavaScript variable in JSP [duplicate]

This question already has answers here:
How can i access javascript variables in JSP?
(3 answers)
how to access the javascript variable in jsp [duplicate]
(3 answers)
Closed 5 years ago.
I have a JavaScript variable and I want to access it in JSP without using submit button. Actually I have a javascript variable and i want to use it for running a query in JSP to fetch a value from database and again using the value from JSP to javascript.
I do not want to have a button in the form tag for submitting the value and then getting it as a paramneter in JSP. Is there any other way of doing that.
Actually i have atextbox where username is coming. I want to use that variable so that i can run a query in JSP and find its user Id. Code for getting value of text box is.
var selUserInfo = thisFrm.SelUser.value
Now i want to pass this selUserInfo to a JSP.
Not possible unless you send a GET or POST request. See this explanation.
Edit: after understanding that you're aiming for an autocompletion text-input, I would recommend to look for a jQuery solution like this one, and using an Ajax request to retrieve the data from your database (inside a .ready() event).

How do I include a HTML file and write it out via javascript? [duplicate]

This question already has answers here:
Javascript read html from url into string
(4 answers)
Closed 7 years ago.
I'm using CodeKit and would like to continue to do so, and thus I haven't got any access to PHP. So what I would like to do is basically include a plain external .html file locally or on the codekit server with javascript.
The idea is to have an if statement and if foo=true then write out foo.html, else write out no-foo.html.
How do I do this?
Provided you have jQuery tagged in your question, you can use it's $.get implementation, which is a higher level version of the $.ajax function except $.get is limited to GET requests, which is likely what you need.
When using Ajax, you're sending an asynchronous HTTP request to something on the server, this may be a HTML document, or chunk of HTML code, anything that the server responds with on a specific request can be used.
$.get('/data/foo.html', function(data){
$('.bar').html(data);
});

How to protect javascript function? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I obfuscate JavaScript?
Is there any way to protect my js file or javascript function from user to view?
You could minify/obfuscate it but the function would still be visible to the user.
Nope but you can always obfuscate. just look up online for javascript obfuscator. It makes code harder to read but it'll still be decodable.
If you need to hide code may I suggets something serverside such as php, aspx etc..
Like Darin sayd, you can obfuscate the code.
But if you want execute a js file on a browser, the user can get the source code. There is no way to avoid this.

Categories

Resources