Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to pass JavaScript variables to PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 159
    Comment on it

    Many a times we got stuck in a case or situation where we have to pass JavaScript variables to PHP using a hidden input in a form. The solution for this situation is:


    Solution:We cannot pass variable values from the current page javascript to the current page PHP code. PHP code runs on the server side and it is unaware about what is going on on the client side. So, for passing JavaScript variables to PHP we have to submitting form on 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>
    </doctype>
    

    By this way we will pass JavaScript variables to PHP.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: