Passing javascript variable to PHP

Asked
Active3 hr before
Viewed126 times

9 Answers

passingvariablejavascript
90%

You cannot pass variable values from the current page JavaScript code to the current page PHP code... PHP code runs at the server side, and it doesn't know anything about what is going on on the client side.,There are several ways of passing variables from JavaScript to PHP (not the current page, of course).,We cannot pass JavaScript variable values to the PHP code directly... PHP code runs at the server side, and it doesn't know anything about what is going on on the client side.,The best and most secure way to pass JavaScript variable into PHP is via AJAX

You need to pass variables to PHP code from the HTML form using another mechanism, such as submitting the form using the GET or POST methods.

<DOCTYPE html>
<html>
  <head>
    <title>My Test Form</title>
  </head>

  <body>
    <form method="POST">
      <p>Please, choose the salary id to proceed result:</p>
      <p>
        <label for="salarieids">SalarieID:</label>
        <?php
          $query = "SELECT * FROM salarie";
          $result = mysql_query($query);
          if ($result) :
        ?>
        <select id="salarieids" name="salarieid">
          <?php
            while ($row = mysql_fetch_assoc($result)) {
              echo '<option value="', $row['salaried'], '">', $row['salaried'], '</option>'; //between <option></option> tags you can output something more human-friendly (like $row['name'], if table "salaried" have one)
            }
          ?>
        </select>
        <?php endif ?>
      </p>
      <p>
        <input type="submit" value="Sumbit my choice"/>
      </p>
    </form>

    <?php if isset($_POST['salaried']) : ?>
      <?php
        $query = "SELECT * FROM salarie WHERE salarieid = " . $_POST['salarieid'];
        $result = mysql_query($query);
        if ($result) :
      ?>
        <table>
          <?php
            while ($row = mysql_fetch_assoc($result)) {
              echo '<tr>';
              echo '<td>', $row['salaried'], '</td><td>', $row['bla-bla-bla'], '</td>' ...; // and others
              echo '</tr>';
            }
          ?>
        </table>
      <?php endif?>
    <?php endif ?>
  </body>
</html>
load more v
88%

JavaScript is the client side and PHP is the server side script language. The way to pass a JavaScript variable to PHP is through a request.,How to pass JavaScript variables to PHP ?,How to pass variables and data from PHP to JavaScript ?,How to pass a PHP array to a JavaScript function?

Method 1: This example uses form element and GET/POST method to pass JavaScript variables to PHP. The form of contents can be accessed through the GET and POST actions in PHP. When the form is submitted, the client sends the form data in the form of a URL such as:

https: //example.com?name=value
load more v
72%

You can easily get the JavaScript variable value on the same page in PHP. Try the following codeL.,How to name variables in JavaScript?,How to declare variables in JavaScript?,How to declare boolean variables in JavaScript?

You can easily get the JavaScript variable value on the same page in PHP. Try the following codeL.

<script>
   var res = "success";
</script>
<?php
   echo "<script>document.writeln(res);</script>";
?>
65%

<script>
   var res = "success";
</script>
<?php
   echo "<script>document.writeln(res);</script>";
?>
load more v
75%

You cannot pass variable values from the current page JavaScript code to the current page PHP code. PHP code runs at the server side, and it doesn't know anything about what is going on on the client side., How do I pass JavaScript variables to PHP ,I want to pass JavaScript variables to PHP using a hidden input in a form.,You need to pass variables to PHP code from the HTML form using another mechanism, such as submitting the form using the GET or POST methods.

Here is the code:

<script type="text/javascript">
    // View what the user has chosen
    function func_load3(name) {
        var oForm = document.forms["myform"];
        var oSelectBox = oForm.select3;
        var iChoice = oSelectBox.selectedIndex;
        //alert("You have chosen: " + oSelectBox.options[iChoice].text);
        //document.write(oSelectBox.options[iChoice].text);
        var sa = oSelectBox.options[iChoice].text;
        document.getElementById("hidden1").value = sa;
    }
</script>

<form name="myform" action="<?php echo $_SERVER['$PHP_SELF']; ?>" method="POST">
    <input type="hidden" name="hidden1" id="hidden1" />
</form>

<?php
   $salarieid = $_POST['hidden1'];
   $query = "select * from salarie where salarieid = ".$salarieid;
   echo $query;
   $result = mysql_query($query);
?>

<table>
   Code for displaying the query result.
</table>
load more v
40%

passing javascript variable to php ,passing javascript variable to php ... ,<head><script type="text/javascript">  function passVariable(){        var x = '123';         // get the current url and append variable        var url = document.location.href + '?x=' + x;         // to prevent looping        var exists = document.location.href.indexOf('?x=');        ,[jQuery] passing variable from java...

<head>
<script>
  $(document).ready(function(){
    $('#myButtons input:radio').change(function() {
      var buttonValue = $("#myButtons input:radio:checked").val();
        $("#myDiv").load('myPHPfile.php', {selectedButtonValue : buttonValue});
    });
  });
</script>
</head>

<body>
  <div id="myButtons">
    <input type="radio" name="category" value="10" />ButtonA 
    <input type="radio" name="category" value="20" />ButtonB
    <input type="radio" name="category" value="30" />ButtonC
  </div>
  <div id="myDiv">Click the button to load results</div>
</body>
load more v
22%

Example 2: using js variable in php ,Example 1: js var to php ,how to get javascript variable value in php in same page? ,how to print javascript variable in html tag?

var val = "<?php echo $val ?>";
load more v
60%

It depends on the way your page behaves. If you want this to happens asynchronously, you have to use AJAX. Try out "jQuery post()" on Google to find some tuts.,To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible):,On your server, you would need to receive the variable sent in the post:,Maybe the most easiest approach for you is something like this

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";
}
</script>

to a PHP variable

< ? php
$phpvariable
   ?
   >
load more v
48%

1. Enter the following code into your HTML. 2. Enter the following code into your PHP code on your server. 3. Test your code. , This code lets the user to your website enter information.[1] X Research source ,Even though the user entered information in a JavaScript environment, their data will be passed through to PHP on the server-side.,In many instances, JavaScript is used on the client-side and PHP is used on the server-side of a website. This wikiHow will teach you how to pass variables (or data) between JavaScript and PHP using either a "GET/POST" method or using cookies.

<!DOCTYPE html>
<html>

<head>
   <title>
      Passing JavaScript variables to PHP
   </title>
</head>

<body>
   <h1 style="color:green;">
      GeeksforGeeks
   </h1>

   <form method="get" name="form" action="destination.php">
      <input type="text" placeholder="Enter Data" name="data">
      <input type="submit" value="Submit">
   </form>
</body>

</html>
load more v

Other "passing-variable" queries related to "Passing javascript variable to PHP"