doumeng06063991 2013-12-02 22:54
浏览 38
已采纳

创建一个Jquery函数将值发送给php [关闭]

I have created a simple select php script that return data from my db. I was was wondering how i would go about creating a Jquery function that passes a variable to the php page. Am i right in thinking this will not require the html page to be refreshed?

The function is called when the drop down menu choice changes. I wish to pass the value of the drop down menu to PopulateBoxes.php.

HTML

<head>
<link href="../UserTemplate.css" rel="stylesheet" type="text/css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>Tours</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js">   </script>

<script type="text/javascript">
function popBox(str)
{
//Jquery function to pass variable to PopulateBoxes.php
}
</script>
</head>
<body>
<select name="lst_MonthDrop" style="background-color:#FF9933; color:#FFF; border:none; margin-top:10px; margin-left:10px;" onchange="popBox(this.value);">
    <option>Please Select</option>
    <?php 
    include 'populatedrodown.php';
    foreach ( $results as $option ) : ?>
        <option value="<?php echo $option->Date; ?>"><?php echo $option->Date; ?></option>
    <?php endforeach; ?>
</select>
</body>
</html>

PHP

<?php
    $passed = $_POST['passedvariable'];

    $mysql_db_hostname = "localhost"; 
    $mysql_db_user = "root"; 
    $mysql_db_password = "pwd"; 
    $mysql_db_database = "db";

    $con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or         die("Could not connect database");
    mysql_select_db($mysql_db_database, $con) or die("Could not select database"); 

    $sql="SELECT * FROM Tour WHERE Date = '".$passed."'";

    $result = mysqli_query($con,$sql);


    while($row = mysqli_fetch_array($result))
    {

       $Duration = $row['Duration'] ;
       $Vessel = $row['Vessel_Name'] ;
       $Location = $row['Location'] ;
       $Available = $row['Available'];
       $Price = $row['Price'];
       echo $Price;
    }

    mysqli_close($con);
    ?>

If any could help i'd really appreciate it, I have been struggling to come to terms with jquery and especially this function for hour. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dphg63476 2013-12-02 22:59
    关注

    You can do this with via AJAX. jQuery offers $.ajax to do this, but also shorthands like $.post

    function popBox(str) {
        $.post('PopulateBoxes.php', { passedvariable: str }, function(data) {
            // data will be what the PHP page echoes
        });
    }
    

    If you want to have a somewhat complex result (more than one value), you can use JSON. In PHP create an object or an array and when you print it, do so after calling json_encode. Now change your code to something like this

    function popBox(str) {
        $.post('PopulateBoxes.php', { passedvariable: str }, function(data) {
            // data will be the JSON object
        }, "json");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路