Viewed   81 times

First I thought that I had to convert JavaScript to PHP, but then I found out that I cannot because of server and client side executions. So now I simply want to send ONE variable

<script type="text/javascript">
function scriptvariable()
    var theContents = "the variable";

to a PHP variable


That function in the JavaScript executes when let's say I click on a button.

Now I have no idea on how to assign that phpvariable to the JavaScript one to use the phpvariable to look up stuff in my database. I know I can add it to my url or some thing and refresh the page, but I would like to do it with AJAX as I might have to use this Ajax method further in my webpage.

So is there an easy way to do this without having to dump pages of code on my page to do one simple thing?



As Jordan already said you have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery.

Maybe the most easiest approach for you is something like this

function myJavascriptFunction() { 
  var javascriptVariable = "John";
  window.location.href = "myphpfile.php?name=" + javascriptVariable; 

On your myphpfile.php you can use $_GET['name'] after your javascript was executed.


Friday, September 30, 2022

If your array has more then 1 dimension or is an associative array you should use JSON.

Json turns a complete array structure into a string. This string can easily send to your php application and turned back into a php array.

More information on json:

var my_array = { ... };
var json = JSON.stringify( my_array );

In php you can decode the string with json_decode:

Sunday, November 27, 2022

Use the jQuery selector to do that. OR use a hidden form if you don't want the user to see it.

For example, do this.

<input type="hidden" name="YOUR_OWN_UNIQUE_NAME" id="UNIQUE_ID" value >

After that, just use jQuery selector


That's about it, not much problems!

Sunday, November 20, 2022

If the client supports Blob and XHR2 (FormData), then you're gold. Supposing that your data is kept in a string data, all you have to do is

// Set the file media type appropriately
var blob = new Blob([ data ], { type: "image/png" });

var formData = new FormData();
formData.append("theFile", blob, "filename.png");

var xhr = new XMLHttpRequest();"POST", "/destination");
xhr.onload = function() {
    // ...

UPDATE: if your client doesn't support XHR2, there is a way to send files using AJAX only, although it's a bit complicated.

Basically, you have to mimic a multipart/form-data request, and manually build the parts of the body of the request. Not very easy, but feasible, and should work with IE6-9.

Friday, September 16, 2022

$twoDArr = array( array('Greg', 44, 'Owner'),
                  array('Joe', 23, 'Renter'),
                  array('Susan', 39, 'Owner'),
                  array('John', 32, 'Renter)

twoDArr = JSON.parse(<?=json_encode($twoDArr)?>)
alert(twoDArr[0][0]) //alerts 'Greg'
alert(twoDArr[0][1]) //alerts '44'
alert(twoDArr[1][0]) //alerts 'Joe'
Thursday, October 20, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :