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");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败