简体   繁体   中英

php, mysql, javascript dropdown list

I am new to coding and have no clue. I managed to grap this script from this site http://www.99points.info/2010/12/n-level-dynamic-loading-of-dropdowns-using-ajax-and-php/

I amended the titles and field inputs to match my project requirements instead of the ones used in the demo. I managed to get this working on my project with reference to the drop down lists.

The problem i am having now is how do you make the script accept the values and input it into the database after the customer selects the drop down lists and clicks the submit button.

If there is a better way to rewrite these codes? Please i am in need of help. Thank you!

here is the code i got on the "get_child_categories.php" file:

<?php

include('cn.php');

if($_REQUEST)
{
$id = $_REQUEST['parent_id'];

$query = "select * from tour where pid = ".$id;
$results = @mysql_query( $query);
$num_rows = @mysql_num_rows($results);
if($num_rows > 0)
{?>
    <select name="sub_category" class="parent">
    <option value="" selected="selected">-- Sub Category --</option>
    <?php
    while ($rows = mysql_fetch_assoc(@$results))
    {?>
        <option value="<?php echo $rows['tour_id'];?>"><?php echo $rows    
      ['category'];?></option>
    <?php
    }?>
    </select>   
<?php   
}
else{echo '<label style="padding:7px;float:left; font-size:12px;">No Record Found 
!</label>';}
}
?>

Also note that the "No Record Found" is always displayed after selecting all drop down lists at the end. How do i fix this to show every criteria is met before submission?

This is the "booking.php" form:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link href="style.css" rel="stylesheet" />

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Jurassic Tours/booking form</title>

<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="jquery.livequery.js"></script>
<script type="text/javascript">

$(document).ready(function() {

//$('#loader').hide();

$('.parent').livequery('change', function() {

    $(this).nextAll('.parent').remove();
    $(this).nextAll('label').remove();

    $('#show_sub_categories').append('<img src="loader.gif" style="float:left;  
    margin-top:7px;" id="loader" alt="" />');

    $.post("get_chid_categories.php", {
        parent_id: $(this).val(),
    }, function(response){

    setTimeout("finishAjax('show_sub_categories', '"+escape(response
                 )+"')", 400);

           });

    return false;
});
 });

 function finishAjax(id, response){
 $('#loader').remove();

 $('#'+id).append(unescape(response));
 } 

</script>
<style>
.both h4{ font-family:Arial, Helvetica, sans-serif; margin:0px; font-size:14px;}
#search_category_id{ padding:3px; width:200px;}

.parent{ padding:3px; width:150px; float:left; margin-right:12px;}
.both{ float:left; margin:0 0px 0 0; padding:0px;}
</style>
</head>
<?php
include('cn.php');?>
<body>

<div id="main">

     <div id="header">
        <img src="images/banner.jpg" alt="Banner" style="padding:10px"/>
     </div>

     <div id="mylinks">
        <div id="navcontainer">
           <ul>
           <li><a href="index.html">Home</a></li>
     <li><a href="login.php">Login</a></li>
           <li><a href="register.php">Register</a></li>
           <li><a href="confirmation.php">Booking</a></li>
           <li><a href="aboutus.php">About Us</a></li>
           <li><a href="services.php">Services</a></li>
           <li><a href="feedback.php">Feedback</a></li>
           <li><a href="agency_login.php">Agency Login</a></li>
     <li><a href="logout.php">Logout</a></li>
        </div> 
     </div>

     <div id="content">

        <h2><center>PLEASE PLACE YOUR BOOKING</center></h2>

     <div style="padding-left:30px; height:710px;">

<br clear="all" /><br clear="all" />

<form action="booking.php" method="post">
<div id="show_sub_categories">
    <select name="search_category" class="parent">
    <option value="" selected="selected">-- Categories --</option>
    <?php
    $query = "select * from tour where pid = 0";
    $results = mysql_query($query);

    while ($rows = mysql_fetch_assoc(@$results))
    {?>
    <option value="<?php echo $rows['tour_id'];?>"><?php echo $rows
                ['category'];?></option>
    <?php
    }?>
    </select>       

</div>

<br clear="all" /><br clear="all" />
<input type="submit" name="submit" value="submit">
</form>
<br clear="all" /><br clear="all" />

</div>


</body>
</html>

The booking form is getting data from the tour table in the database where there is tour,destination,and duration all dependant on drop down lists selected. This is the processing part where i do not know how to do after the submit button is pressed so i left it blank! The only data i can recover is the Primary key from the customer table as shown below.

      <?php
//When submit button is pressed.
if (isset($_POST['submit'])) {

//Include the server and database connection. 
include('cn.php');

session_start();


$userUsername = $_SESSION['loggedInUser'];

// Build the SQL query to retreive the variables ($) and input the data into the database.
$sql = "INSERT INTO booking   
      (user_id) 
    VALUES ((SELECT user_id FROM user WHERE user_username =  
                         '" . $userUsername . "'))";

// test the sql statement.
if(!mysql_query($sql,$cn)) {
die(mysql_error($cn));

}
// direct to this page when booking is successful.
header('Location: booking_success.php');
}

?>

I forgot to include the update.php file

<?php
if (!empty($_GET['tour_id']) && !empty($_GET['value'])) {

$id = $_GET['tour_id'];
$value = $_GET['value'];

try {

$objDb = new PDO('mysql:host=localhost;dbname=jurassicbase', 'root', '');
$objDb->exec('SET CHARACTER SET utf8');

$sql = "SELECT * 
FROM `categories`
WHERE `master` = ?";

$statement = $objDb->prepare($sql);
$statement->execute(array($value));
$list = $statement->fetchAll(PDO::FETCH_ASSOC);

if (!empty($list)) {

$out = array('<option value="">Select one</option>');

foreach($list as $row) {
$out[] = '<option value="'.$row['tour_id'].'">'.$row    
['name'].'</option>';
}

echo json_encode(array('error' => false, 'list' => implode('', $out)));

} else {
echo json_encode(array('error' => true));
}

} catch(PDOException $e) {
echo json_encode(array('error' => true));
}

} else {
echo json_encode(array('error' => true));
}   

I'm guessing you are never reaching the form processor as you don't have any <form> tag in your form page.

You would need:

<form action="your_form_processor.php" method="post">
  // your form fields
</form>

to get that to work.

Apart from that you need to switch to PDO (or mysqli) and prepared statements as your code is vulnerable to sql injection.

While you are developing your code, you should also remove all @ error suppressing operators.

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