0

我是编码新手,不知道。我设法从这个网站http://www.99points.info/2010/12/n-level-dynamic-loading-of-dropdowns-using-ajax-and-php/ 获取了这个脚本

我修改了标题和字段输入以匹配我的项目要求,而不是演示中使用的那些。参考下拉列表,我设法让这个在我的项目上工作。

我现在遇到的问题是,在客户选择下拉列表并单击提交按钮后,如何使脚本接受值并将其输入数据库。

如果有更好的方法来重写这些代码?请我需要帮助。谢谢!

这是我在“get_child_categories.php”文件中得到的代码:

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

另请注意,在最后选择所有下拉列表后,始终显示“未找到记录”。我如何解决这个问题以显示在提交之前满足每个标准?

这是“booking.php”表格:

 <!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>

预订表格从数据库中的旅游表中获取数据,其中旅游、目的地和持续时间都取决于所选的下拉列表。这是处理部分,按下提交按钮后我不知道该怎么做,所以我将其留空!我可以恢复的唯一数据是客户表中的主键,如下所示。

      <?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');
}

?>

我忘了包含 update.php 文件

<?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));
}   
4

1 回答 1

0

我猜您永远不会到达表单处理器,因为您的表单页面中没有任何<form>标签。

你需要:

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

让它发挥作用。

除此之外,您需要切换到 PDO(或 mysqli)和准备好的语句,因为您的代码容易受到 sql 注入的影响。

在开发代码时,您还应该删除所有@错误抑制运算符。

于 2013-03-13T17:05:09.717 回答