PHP、MYSQL + javascript?根据三个用户 SELECT 输入产生一个结果(价格)

标签 php javascript mysql

我对编码和使用 MySQL、PHP 和(昨晚刚开始的)Javascript 还很陌生(一个月)。我试图找到一个代码示例来执行以下操作:

a) 用户使用下拉列表 (SELECT) 选择产品(比如酒店)。
b) 同一用户还使用第二个下拉列表 (SELECT) 来选择相关时间段(例如周末与工作日的不同费率)。
c) 最后,用户选择房型。

假设可用性(计算出那部分),这三个输入对价格结果有影响(我想根据这些用户输入来计算)。

例如:
输入 a) 加州旅馆(是的,老鹰乐队的歌曲正在播放)
输入 b) 2012 年 3 月 23 日
输入 c) 一间卧室套房

我知道我需要使用 Java 来完成下一部分,即在用户添加房间和选项(早餐等)时在同一页面上更新总计。

非常感谢任何人的帮助(即使只是相关示例的链接)。

* 作为响应评论的更新,是的,我绝对希望价格通过服务器更新,并将 MySQL 数据库发布/获取到 PHP 到屏幕。

最佳答案

很明显,您将不得不拥有自己的数据库,但这是代码的示例版本(PHP 端),因为您似乎能够自己完成 javascript 端。

<?PHP 
//we're checking if they posted anything since I think you're going to include this on the same page, if not it's still a good check to avoid overhead.
if($_POST){
//The $_POST variable is all data that is being posted by the client. It's an associative array, and I've taken liberty of naming the "name" field on various inputs(the select) on this side.
    $db_password='your_database_users_password';
    $db_username='your_database_users_username';
    $db_host='your databases host(likely localhost)';
    //if you're using php 5.3+ use mysqli_connect, if below that, use mysql_connect and use mysql_ functions
    $connection=mysqli_connect($db_password,$db_username,$db_host);
    mysqli_select_db($conn,'hotel_database');
    //if you're going to have users to have a special rate on certain dates e.g. holidays, you're going to have to code that up, later.This is just a very very basic one.
    $query="SELECT room_rate FROM room_info WHERE hotel=".mysqli_real_escape_string($connection,$_POST['hotel_name']);
    $result=mysqli_query($connection,$query);
    while($row=mysqli_fetch_assoc($connection,$result)){
        $room_rate=$row['room_rate];
    }
    $days_spent=$_POST['time_staying_end']-$_POST['time_staying_start'];
    $cost=$days_spent*$room_rate;
    echo $cost;
?>

这是服务器端的代码。它非常简单,但它做了它应该做的。如果您想了解更多信息,我会查看 PHP manual on mysql他们将获得更多关于在那里做什么和在哪里的信息。

转义所有用户输入,如果是字符串,最简单的方法是使用 mysqli_real_escape_string($connect,$unsafe_input); 或者如果它应该是一个整数使用 intval($unsafe_numerical_value);

那么好吧,因为问这个问题的人问了一些关于它的更多信息。你会想要通过 ajax 来完成它。如果你安装了 jquery(这是我使用的),它会容易得多。它会是这样的。

//as you can see, you want to seperate your posted variables with an &.
var posts='hotel_name='+$('hotel_name_identifier').val()+'&start_date='+$('start_date').val()+'&end_date='+$('end_date).val();
$.ajax({
        type:"POST",
        url:"the_page_containing_your_sql_code.php",
        data:posts,
        dataType:"json", //I chose JSON since it's the best format for sending data
        success:function (datum){ //you need this to handle your returned data
            use_data(datum);
        }
    }

了解更多相关信息。到这里。 jquery Ajax api documentation

关于PHP、MYSQL + javascript?根据三个用户 SELECT 输入产生一个结果(价格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8015616/

相关文章:

php - OOP - 没有 GET/SET

javascript - 如何使用 javascript 使用来自 php 函数的数组元素填充 <div> 元素?

javascript - 无法在 Cloudflare Worker 脚本上创建 MD5 哈希

PHP/MySQL - 尝试选择 table1 中的 id 等于 table2 中的 id 的两列时出现问题

PHP 在一个共享主机上输出语法错误,但在另一个共享主机上运行完美

PHP 搜索并删除数字数组值

java - 表单提交和 HttpServletRequest

javascript - iframe 是否与所有者在同一线程上运行?

mysql - VB.NET MySQL 应用程序无法在客户端 Windows 10 64 位 PC 上连接

mysql - 我可以在实体类中定义关系而不将它们存储在数据库中吗?