Viewed   71 times

let's say i have:

    var jsString="hello";

and i want it to pass into php string:

$phpString = jsString;

how do i do that correctly? please tell me the right way. thanks in advance.



You need a Ajax call to pass the JS value into php variable

JS Code will be (your js file)

var jsString="hello";
    url: "ajax.php",
    type: "post",
    data: jsString

And in ajax.php (your php file) code will be

$phpString = $_POST['data'];     // assign hello to phpString 
Thursday, November 10, 2022

alert('my name is: <?php echo $man; ?>' );

Friday, August 19, 2022

When someone visits a website, this is generally what happens:

  1. Their browser sends a request to the server.
  2. The server evaluates that request.
  3. The server realizes, "Egad, the page they're requesting has PHP!"
  4. The server evaluates the PHP, and only sends the results to the browser.
  5. The browser parses the content that it receives.
  6. The browser realizes, "Egad, the page I received has JavaScript!"
  7. The browser evaluates the JavaScript, entirely on the client's machine.

So PHP and JavaScript are basically at different ends of the process. Only the server handles PHP, and only the client handles JavaScript.

To "give" a string to PHP, you'd have to make a request of the PHP page, sending that string as a GET variable:

There are a few ways to do this with JavaScript.

  1. If you only care about making that request on the PHP page, and you don't need to worry about receiving any information back, you can just create an image and use the URL as the source:

    var fakeImg = new Image();
    fakeImg.src = '';

    Even though you're requesting an image, the server doesn't know that, and will process your request by calling the PHP evaluating it, etc.

  2. You can make an actual AJAX request. Start by creating an XMLHttpRequest object:

    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');

    There are some issues in IE with cached responses on AJAX requests, so make the url unique:

    var url = '';

    Tell your XHR where you want it to go, and how you want it to get there:'GET', url, true);
    // The "true" parameter tells it that we want this to be asynchronous

    Set up a method that will check for when a response is received:

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status < 400) {

    And finally, send the request:

    // We set "null" because some browsers are pissy

    Some notes to keep in mind:

    • You have to build the success function yourself, to handle the string that your PHP page will return.
    • You can pass that function xhr.responseXML if you want, but that's usually just a hassle for me.
    • Using onreadystatechange the way I have will (I believe) introduce memory leaks in some versions of IE
Thursday, October 6, 2022

What Assaf said is correct. There is a built in function in PHP to do exactly that.

substr_compare($str, $test, strlen($str)-strlen($test), strlen($test)) === 0;

If $test is longer than $str PHP will give a warning, so you need to check for that first.

function endswith($string, $test) {
    $strlen = strlen($string);
    $testlen = strlen($test);
    if ($testlen > $strlen) return false;
    return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
Monday, November 7, 2022

@bobince's answer has (luckily) become a bit dated; you can now simply use

var chars = Array.from( text )

to obtain a list of single-codepoint strings which does respect astral / 32bit / surrogate Unicode characters.

Thursday, September 29, 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 :