dongna2498 2016-09-29 20:14
浏览 38
已采纳

将“值”从表单中的选择传递到具有javascript问题的php变量

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条回答 默认 最新

  • douyiqi9640 2016-09-29 21:15
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测