-1

我有 3 个选择框 drop_1、drop_2 和 drop_3。最后 2 个是在前一个的选择上动态添加的。create_promo.php 是加载所有框的页面。但是我使用 func.php 加载动态选择框。因此我没有在 create_promo.php 调用的表单处理页面 promo_process.php 中获取 drop_2 和 drop_3 值。我想将我的 drop_2 和 drop_3 值发送回我的 create_promo .php。

create_promo.php

<script type="text/javascript">
$(document).ready(function() {
$('#wait_1').hide();
$('#drop_1').change(function(){
$('#wait_1').show();
$('#result_1').hide();
    $.get("func.php", {
    func: "drop_1",
    drop_var: $('#drop_1').val()
    }, function(response){
    $('#result_1').fadeOut();
        setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
    });
return false;
});
});

function finishAjax(id, response) {
    $('#wait_1').hide();
    $('#'+id).html(unescape(response));
    $('#'+id).fadeIn();
}
function finishAjax_tier_three(id, response) {
    $('#wait_2').hide();
    $('#'+id).html(unescape(response));
    $('#'+id).fadeIn();
}
</script>

<form action="promo_process.php" method="post">
<tr>
    <td class="normalfont">SELECTION 1</td>
    <td>
        <select name="drop_1" id="drop_1">
        <option value="" selected="selected" disabled="disabled">Select 1</option>
        <?php getTierOne(); ?>
        </select>
    </td>
</tr>
<tr>    
     <td class="normalfont">SELECTION 2</td>    
 <td>
     <span id="wait_1" style="display: none;">
     <img alt="Please Wait" src="images/ajax-loader.gif"/>
     </span>
     <span id="result_1" style="display: none;"></span>
     </td>
</tr>
<tr>
     <td class="normalfont">SELECTION 3:</td>
 <td>
    <span id="wait_2" style="display: none;">
        <img alt="Please Wait" src="images/ajax-loader.gif"/>
    </span>
    <span id="result_2" style="display: none;"></span>
</td>
</tr>
</form>
<?php
     include('connect.php');
     function getTierOne()
     {
         $result = mysql_query("SELECT s_name FROM selection1") or die(mysql_error());
         while($tier = mysql_fetch_array( $result )) 
     {
         echo '<option value="'.$tier['s_name'].'">'.$tier['s_name'].'</option>';
         }
     }
?>

函数.php

<?php
     include('connect.php');
     if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
          drop_1($_GET['drop_var']); 
     }
     function drop_1($drop_var)
     {
          $result = mysql_query("SELECT DISTINCT a_name FROM select_2 WHERE s_id=(select s_id from s_name where s_name='$drop_var')") or die(mysql_error());
          echo '<select name="drop_2" id="drop_2">
                   <option value=" " disabled="disabled" selected="selected">Select 2</option>
                   <option value="ALL" >ALL</option>';
          while($drop_2 = mysql_fetch_array( $result ))
          {
              echo '<option value="'.$drop_2['a_name'].'">'.$drop_2['a_name'].'</option>';
          }
      echo '</select>';
      echo "<script type=\"text/javascript\">
       $('#wait_2').hide();
       $('#drop_2').change(function(){
       $('#wait_2').show();
       $('#result_2').hide();
               $.get(\"func.php\", {
            func: \"drop_2\",
            drop_var: $('#drop_2').val()
               }, function(response){
               $('#result_2').fadeOut();
               setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
               });
           return false;
           });
           </script>";
   }
   if($_GET['func'] == "drop_2" && isset($_GET['func'])) {
          drop_2($_GET['drop_var']);
   }
   function drop_2($drop_var)
   {
          if($drop_var=="ALL")
              $result = mysql_query("SELECT DISTINCT h_name FROM select_3")or die(mysql_error());
      else  
      $result = mysql_query("SELECT DISTINCT h_name FROM select_3 WHERE a_id=(select a_id from select_2 where a_name='$drop_var')")or die(mysql_error());
      echo '<select name="drop_3" id="drop_3">
                <option value=" " disabled="disabled" selected="selected">Select 3</option>
            <option value="ALL">ALL</option>';
      while($drop_3 = mysql_fetch_array( $result ))
     {
    echo '<option value="'.$drop_3['h_name'].'">'.$drop_3['h_name'].'</option>';
         }
     echo '</select> ';
   }

?>

promo_process.php

<?php
include 'connect.php';
$errmsg = array(); //Array to store errors
$errflag = false; //Error flag

$drop = mysql_real_escape_string($_POST['drop_1']);
$drop_2 = mysql_real_escape_string($_POST['drop_2']);
$drop_3 = mysql_real_escape_string($_POST['drop_3']);
if($drop== ' ' && $drop_2== ' ' && $drop_3==' ')
{
    $errmsg[] = 'SELECT PLEASE'; //Error
    $errflag = true; //Set flag so it says theres an error
}

if($errflag) {
    $_SESSION['ERRMSG'] = $errmsg; //Write errors
    session_write_close(); //Close session
    header("location: create_promo.php"); //Rediect
    exit(); //Block scripts
}
?>

我在 promo_process.php 中出现“未定义索引 drop_1 和 drop_2”的错误消息。我如何保留这些价值观?

4

1 回答 1

0

我解决了。

我必须创建一个隐藏类型输入,并通过以下代码行给出选择值。

echo "<script type=\"text/javascript\">
        $('#wait_2').hide();
        $('#drop_2').change(function(){
        $(this).parent().parent().find('.drop2').val($(this).val());
        $('#wait_2').show();
        $('#result_2').hide();
    $.get(\"func.php\", {
        func: \"drop_2\",
        drop_var: $('#drop_2').val()
    }, function(response){
    $('#result_2').fadeOut();
    setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
    });
        return false;
        });
    </script>";

其中drop2是隐藏类型输入,而drop_2是其值存储在隐藏输入中的选择框。

谢谢大家。

于 2012-11-22T06:23:49.577 回答