0

I'm wondering if there's a way to bring a variable from JS to PHP. I've got creating the variable in JS, I just wanna know if there's a way to bring it to PHP. HTML:

<form id="form" onsubmit="return false;">
    <input class="mainwrite" id="userInput">
    <input type="submit" onclick="save()">
</form>

JS:

function save() {
    var input = document.getElementById("userInput").value;
    alert(input);
}

I want to take the input variable and add it to a PHP file as a string.

YMJA
  • 17
  • 2

1 Answers1

-3

Try this:

<script>
var var= "big boss";
</script>

<?php
echo "<script>document.writeln(var);</script>";
?>
  • 2
    This won't work. PHP runs before the page is sent to the browser, javascript after. The only way to get a variable from JS to PHP is via Ajax or submitting to request. – aynber May 10 '22 at 15:35
  • 3
    This doesn't provide the value `"big boss"` to PHP. The only value PHP has is the literal string `""`. Additionally, naming a JavaScript variable `var` is just asking for a variety of problems. – David May 10 '22 at 15:36