How do I pass JavaScript variables to PHP

+1 vote

I want to pass JavaScript variables to PHP using a hidden input in a form.

But I can't get the value of $_POST['hidden1'] into $salarieid. Is there something wrong?

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>
Jul 6, 2020 in Java-Script by kartik
• 37,520 points
19,642 views

1 answer to this question.

0 votes

Hello @kartik,

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.

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) :
      ?>

Hope it helps!!

In order to know more about javascript variables, you should join our Java certification training today.

Thank You!!

answered Jul 6, 2020 by Niroj
• 82,840 points
Hello niroj

Tried your code but not successful....please help me

What error you are getting?

Thanks for your response....It doesn't have any error but its not working

You would need to execute the JavaScript first and then send the result to the server via a FORM or AJAX call.

Related Questions In Java-Script

0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,840 points
3,243 views
0 votes
1 answer

How do I get PHP errors to display?

Hello @kartik, To display all errors you need ...READ MORE

answered Jun 20, 2020 in Java-Script by Niroj
• 82,840 points
966 views
0 votes
1 answer

How do I embed PHP code in JavaScript?

Hello @kartik, If you have individual .js files, and you ...READ MORE

answered Jul 7, 2020 in Java-Script by Niroj
• 82,840 points
8,583 views
0 votes
1 answer

How do I copy to the clipboard in JavaScript?

Hello @kartik, To copy HTML , you can ...READ MORE

answered Aug 28, 2020 in Java-Script by Niroj
• 82,840 points
875 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,995 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,696 views
0 votes
1 answer

How do I pass variables and data from PHP to JavaScript?

Hello @kartik, Simply use one of the following ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,840 points
1,839 views
0 votes
1 answer

How can I pass PHP objects to javascript?

Hello @kartik, You can convert the PHP object ...READ MORE

answered Jul 7, 2020 in Java-Script by Niroj
• 82,840 points
5,683 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP