douming4359 2012-07-21 11:38
浏览 35
已采纳

从JavaScript调用php脚本

I have spent all morning trying to figure out how I can call a PHP script from my JavaScript function and then return a value to output. I am not sure whether I can do this with just JavaScript or whether I have to include some Ajax aswell?

Here is the main page

<script>
    $("document").ready(function (){       
        $("select").change(function(){
            var currentValue = parseInt($(this).find("option:selected").val());
            //Call Script script.php here
            $("#output").html(result);
        })
    }); 
</script>   
</head>
<body>
    <select name="select">
        <option value='1' >1</option>
        <option value='2'>2</option>
        <option value='3'>3</option>
    </select>
    <div id="output"><!---OUTPUT HERE----></div>

And here is the script I want to call (I have simplified it all down to make it easier to understand)

<?php
    $result = $currentValue + 1;
?>

Basically I want it to read in the currentValue variable and output the result variable back to the webpage, to be outputted again.

  • 写回答

2条回答 默认 最新

  • dongzecai0684 2012-07-21 11:53
    关注
    // Javascript:
    $.ajax({
                type: "GET",
                url: "example.com/script.php?currentValue="+currentValue ,
                dataType: "json",
                statusCode: {
                    200: function (result)
                    {
                        $("#output").html(result.value);
                    }
                }
            });
    
    
    // PHP 
    <?php 
    $result = $_GET["currentValue"] + 1;
    echo json_encode(array("value" => $result));
    
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果