简体   繁体   中英

PHP, MYSQL + javascript? to produce a result (price) based on three user SELECT inputs

I'm very new (one month) into coding and using MySQL, PHP, and (just started last night) Javascript. I am trying to find a code example to do the following:

a) User uses a drop list (SELECT) to choose a product (say a hotel).
b) Same user also uses a second drop list (SELECT) to choose the related time period (different rates weekends vs. weekdays for example).
c) finally, user selects the room type.

assuming availability (got that part figured), the three inputs have an inpact on the price result (which I would like to calculate based on these user inputs).

For example:
input a) Hotel California (yes, the Eagles song is playing right now)
input b) March 23, 2012
input c) a 1 bedroom Suite

I know I need to use Java to do this next part which is to have it update totals on the same page as the user adds rooms and options (breakfast, etc).

Anyone's help is, in advance, much appreciated (even if just a link to a relevant example).

* AS an update in response to a comment, yes, I definitely would want the price to update through the server and post/fetch to/from MySQL dbase to PHP to the screen.

Well obviously you're going to have to have your own databases but here's an example version of the code(the PHP side) since yous seem to be able to do the javascript side on your own.

<?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;
?>

And that's the code on the serverside. It's pretty simplified but it does what it should. If you're wanting more information about it then I'd check the PHP manual on mysql They'll have a lot more information about what to do and where in there.

Escape all user input, the easiest way if it's a string is to just use the mysqli_real_escape_string($connect,$unsafe_input); or if it's supposed to be an integer use intval($unsafe_numerical_value);

OK then, since the person who asked this question asked some more information about it. You're going to want to do it via ajax. If you have jquery installed(which is what I use) it's going to be a lot easier. It'll be something like this.

//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);
        }
    }

for more information about it. go here. jquery Ajax api documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM