0

Possible Duplicate:
How to get the selected option value of a drop down box in PHP code

Drop Down :

 <SELECT NAME='action' class="action" id="action" onchange="a();"> 
        <OPTION VALUE="IN"> IN </OPTION>
        <OPTION VALUE="OUT"> OUT </OPTION>
    </SELECT>

Script to get drop-down text:

 <script type="text/javascript">
    function a(){
    var e = document.getElementById("action");
    var strUser = e.options[e.selectedIndex].text;
    document.write(strUser);
    }
   </script>

//got the script from Get selected value in dropdown list using JavaScript? Here i need to print the value strUser in php. or else on changing the drop down values should print in same page not in new page.

Community
  • 1
  • 1
Parthi04
  • 1,121
  • 4
  • 21
  • 39
  • 1
    http://stackoverflow.com/questions/10485988/print-value-of-javascript-in-php check this link – Nikson Kanti Paul May 31 '12 at 06:09
  • What do you mean "print the value strUser in php"? If you want to pass it to the PHP script (and later save it as a global or in a database) you could use cookies or forms. – Raekye May 31 '12 at 06:10
  • yes i need to pass it to php script.How to do that? i need to get 3 more values same as above . @Raeki – Parthi04 May 31 '12 at 06:13

5 Answers5

0

PHP is server-side script run before javascript (client side) code. you can try

Ajax for sending this value to server and generate your desire output.

see detail Ajax

Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
0

If you want to pass the data selected in the Dropbox to a PHP script, you must use forms or a submit() call from JavaScript over a dynamically generated form.

But if you don't want the page to be reloaded, then you must rely to AJAX technique.

Take a look here and here. There are tons of examples in the net.

Community
  • 1
  • 1
felixgaal
  • 2,403
  • 15
  • 24
0

< html> < head>

< script> //document.getElementById('yourSelectBoxId').options[document.getElementById('yourSelectBoxId').selectedIndex].value//or

function onchg(){ alert(document.getElementById('yourSelectBoxId').value); } < /script> < /head>

< select id="yourSelectBoxId" onchange="onchg();">

< option value=1> a< /option> < option value=2> b< /option> < option value=3> c< /option>

< /select>

< body>

< /body>

< /html>

Community
  • 1
  • 1
shridatt
  • 896
  • 4
  • 15
  • 39
0

use innerHTML instead of document.write

Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52
0

If you want to pass it to the PHP script (and later save it as a global or in a database) you could use cookies, or submit it as part of a form (what I'd suggest).

So you have

<form action="form_receiver.php" method="get">

Which basically means, submit all the (named) fields in this form to the form_receiver page. The "get" could be replaced by "post" and they are just two methods for sending data ("get" data is encoded in the URL so it can be saved, useful for simple things, and "post" is "sent hidden by the browser" so is better for things like large ammounts of data or confidential stuff).

Now for the data in each field to be sent, you need to give it a name, as you have provided. So for that input field, you would only need to enclose it in a form.

On form_reciever.php, you would be able to access the variables by name using $_GET['name'] or $_POST['name'] depending on which method you used. You could then save it somewhere else to use later.


Cookies should be for preserving data about the user (such as a session id), and I would not recommend using them to pass data between pages. However, if for some reason you require this, you could set cookie data with javascript and get it using $_COOKIE['cookie_name'] in php. If you're setting a cookie in php use setcookie("name", "value") (more details here http://php.net/manual/en/function.setcookie.php).

There are many ways to set cookies in javascript. I would recommend using a library like jQuery and you could set and get cookies in a similar way: $.cookie("name", "new_value") or $.cookie("name") to get the value.

Raekye
  • 5,081
  • 8
  • 49
  • 74