views:

785

answers:

3

Hello,

I want to pass javascript variables to php using hidden input of a form.

But i have met a problem here, the $salarieid can't get the value from $_POST['hidden1']. Is there something wrong?

Here is the code:

<script type="text/javascript">
// view which the user has chosen
function func_load3(name){
    var oForm = document.forms["myform"];
    var oSelectBox = oForm.select3;
    var iChoice = oSelectBox.selectedIndex;
    //alert("you have choosen: " + 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 display the query result. 
</table>

Thanks for your advice.

+1  A: 

PHP runs on the server before the page is sent to the user, JavaScript is run on the user's computer once it is received, so the PHP script has already executed.

If you want to pass a JavaScript value to a PHP script, you'd have to do an XMLHttpRequest to send the data back to the server.

Here's a previous question that you can follow for more information: Ajax Tutorial

Now if you just need to pass a form value to the server, you can also just do a normal form post, that does the same thing, but the whole page has to be refreshed.

<?php
if(isset($_POST))
{
  print_r($_POST);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <input type="text" name="data" value="1" />
  <input type="submit" value="Submit" />
</form>

Clicking submit will submit the page, and print out the submitted data.

Zurahn
@Zurahn, yes, maybe i try to use XMLHttpRequest, so i can't pass javascript value to php script in the same page using javascript? I have searched a lot, someone says it can be done with hidden input in form. Do i need to try this solution furthur?
garcon1986
PHP generates the HTML (the page the user sees), and then sends it to them. So the user has just an HTML web page (the PHP does everything on the server), and once he has that HTML page, THEN the JavaScript executes. It's the difference between client-side and server-side: http://en.wikipedia.org/wiki/Client-server
Zurahn
+2  A: 

You cannot pass variable values from the current page javascript 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 html-form using another mechanism, such as 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>
Sergey Kuznetsov
@Sergey, I have used submitting form here with POST method, i don't know why it doesn't work. Do you know why? And how to solve that?
garcon1986
Why do you need to call some function before sending POST request? Maybe I'm wrong and didn't understand your wishes, but I made an example in my edit answer, where firstly we fill the forms dropdown field with values from "salarie" table, user need to choose the salaried from this field and if we send this form to server, it will get data from database and show it in the table below the input form.
Sergey Kuznetsov
@sergey, so i need to use ajax? Actually, i'm familliar with that.
garcon1986
Actually, I have used your method in the past one level select box. But now i have made a three-chained select box, so i can't use your method.
garcon1986
At this point: Yes, you need to use AJAX technology. If you have troubles with realization, I think I can help.
Sergey Kuznetsov
Thanks a lot. I'm trying and i appreciate your help very much.
garcon1986
A: 

Is your function, which sets the hidden form value, being called? It is not in this example. You should have no problem modifying a hidden value before posting the form back to the server.

Pestilence
@perstilence, you're right, i didn't call the function. Do you know how to call it in php?
garcon1986
hmm. It looks like you want to call it when someone selects something in your form / an onClick handler. Calling it from PHP doesn't make any sense. Someone selects a form value, your function fires, updates a hidden form value, then makes its way to php on form post. No?
Pestilence
@pestilence, i want to let someone select an item in selectbox, and when i click submit button, a search result will be displayed in a table. So at the backend, when i choose the item, its value will be passed to an mysql query in php as a variable. This is what i want to do. But it seems i can't find the solution in javascript. Do you have any clue?
garcon1986
Yes, post or get method in the form will be accepted if it works.
garcon1986