1

I'm trying to pass a value of a option in a select from my HTML form to a PHP variable through Javascript

<script type="text/javascript">
function changeDir(ID){
    var dir = document.getElementById(ID).value;
    //For debuging purposes i've used an alert to show the value of dir on screen
    //alert (dir);
    //At this point of the function I want to assign the value of "dir" to the variable $folder
    }
</script>

there's my .php with html

<SELECT name="course" ID="course" onchange='changeDir("course")' >
        <OPTION value="artes>'">artes</OPTION>
        <OPTION value="ingles">inglés</OPTION>
</SELECT>
<?php $folder= ...?>

I'm using this with a php upload lib, all working at the same file. If anyone can explain me how to assign the value of the option to a php var will be great.

Thanks and Regards.

2
  • You can't assign a value to the webserver, when that value is gotten after the user does something in the browser. At that point the page has left the server and now lives in the browser, and can no longer do anything to the server at all. Your options are ajax, sockets or reloading the page, all making a new request to your server to actually send the data back from the browser to the server. Commented Sep 29, 2016 at 20:17
  • You can't with php. You can with nodejs :P Commented Sep 29, 2016 at 21:17

2 Answers 2

1

You can choose 2 different ways. First is AJAX, second is to redirect the page and GET the value of the <select> element.

HTML:

<SELECT name="course" ID="course" onchange='changeDir("course")' >
   <OPTION value="artes>'">artes</OPTION>
   <OPTION value="ingles">inglés</OPTION>
</SELECT>

JAVA:

<script type="text/javascript">
function changeDir(ID){
var dir = $("course").val();
$.post("php_file.php",
    {
      posted_dir: dir
    },
    function(data){
        $("id_of_result_elemnt").html(data);
    });
    }
</script

PHP:

<?php
$folder=$_POST['posted_dir'];
echo "This text will be display in the element with id_of_result_elemnt ID";
?>

Other method is redirect

HTML+PHP:

<SELECT name="course" ID="course" onchange='changeDir("course")' >
        <OPTION value="artes>'">artes</OPTION>
        <OPTION value="ingles">inglés</OPTION>
</SELECT>
<?php if (iset($_GET['folder']))
 {$_GET['folder']=$folder;}...?>

JAVA:

<script type="text/javascript">
function changeDir(ID){
    var dir = document.getElementById(ID).value;
    window.location = "http://example.com/myphp?folder=" + dir;
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

The fact is that you can't. They both live in different parts of your page. While you can write them in the same file, all PHP code is processed in the server side and Javascript is not able to see anything from the client side.

What you could do is whether pass it as a AJAX request in order to get back some information to show to the user or make the <select> refresh the page when user changes the option by submitting the form, so you don't lose the "state" of your form, and then verify in your php block if there's any option selected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.