Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在ajax连接选择中提交后维护选项_Php_Jquery_Ajax - Fatal编程技术网

Php 在ajax连接选择中提交后维护选项

Php 在ajax连接选择中提交后维护选项,php,jquery,ajax,Php,Jquery,Ajax,我用这种方式制作了一个带有动态/串联选择的表单 表单HTML(index.php) CLASS.PHP public function show_cat_1() { $result = $this->db->mysqli->query ("SELECT * FROM cat_1 ORDER BY idcat_1 ASC"); $cat_1 = '<option value="">all</option>'; while($row = $result

我用这种方式制作了一个带有动态/串联选择的表单

表单HTML(index.php)

CLASS.PHP

public function show_cat_1() 
{
$result = $this->db->mysqli->query
("SELECT * FROM cat_1 ORDER BY idcat_1 ASC");

$cat_1 = '<option value="">all</option>';

while($row = $result->fetch_assoc()) 
{
$cat_1 .= '<option value="' . $row['idcat_1'] . '"';
$cat_1 .= '>' . $row['idcat_1'] . '</option>';
}
return $cat_1;
}

public function show_cat_2() 
{
$result = $this->db->mysqli->query
("SELECT * FROM cat_2 ORDER BY idcat_2 ASC");

$cat_2 = '<option value="">all</option>';

while($row = $result->fetch_assoc()) 
{
$cat_1 .= '<option value="' . $row['idcat_2'] . '"';
$cat_1 .= '>' . $row['idcat_2'] . '</option>';
}
return $cat_2;
}

etc..
public function show_cat_1()
{
$result=$this->db->mysqli->query
(“由idcat_1 ASC从cat_1订单中选择*);
$cat_1=‘全部’;
而($row=$result->fetch_assoc())
{
$cat_1.=''.$row['idcat_1'].';
}
返回$cat_1;
}
公共功能展示(第2类)
{
$result=$this->db->mysqli->query
(“由idcat_2 ASC从cat_2订单中选择*);
$cat_2=‘全部’;
而($row=$result->fetch_assoc())
{
$cat_1.=''.$row['idcat_2'.'”;
}
返回$cat_2;
}
等

我的目标是在提交表单(重新加载页面)后在字段中保留选项(选择值),同时保持其功能。你能做到吗?我怎么能这么做?谢谢

simple将selecte选项传递给action,并在输出页面集时选择您以前存储的选项。我尝试过,但没有成功,您可以给我一个示例吗?谢谢您是否已尝试使用attr“selected”?
$(function() {

// Ajax post Page
var urlpage = 'ajax-php/con-select.php';

// Vars 
var wait = '<option value="">wait</option>';
var all = '<option value="">all</option>';

// Default
$("select#catid_2").prop("disabled", true);
$("select#catid_3").prop("disabled", true);

// CATID_1 Change
$("select#catid_1").change(function(){

// set var
var catid_1 = $("select#catid_1 option:selected").prop("value");

// laod
$("select#catid_2").html(wait);


$.post(urlpage, 
{ 
    catid_1:catid_1 
}, 
function(data){

    switch (data) {

        case all:
            $("select#catid_2").html("");
        return false;

        default:
            $("select#catid_2").prop("disabled", false);
            $("select#catid_2").html(data);
        return false;
        }
    });
});

// CATID_2 Change ... etc

}); 
if(isset($_POST['catid_1']))
{
    echo $cs->show_cat_2();
    die;
}

if(isset($_POST['catid_2']));
{
    echo $cs->show_cat_3();
    die;
}
public function show_cat_1() 
{
$result = $this->db->mysqli->query
("SELECT * FROM cat_1 ORDER BY idcat_1 ASC");

$cat_1 = '<option value="">all</option>';

while($row = $result->fetch_assoc()) 
{
$cat_1 .= '<option value="' . $row['idcat_1'] . '"';
$cat_1 .= '>' . $row['idcat_1'] . '</option>';
}
return $cat_1;
}

public function show_cat_2() 
{
$result = $this->db->mysqli->query
("SELECT * FROM cat_2 ORDER BY idcat_2 ASC");

$cat_2 = '<option value="">all</option>';

while($row = $result->fetch_assoc()) 
{
$cat_1 .= '<option value="' . $row['idcat_2'] . '"';
$cat_1 .= '>' . $row['idcat_2'] . '</option>';
}
return $cat_2;
}

etc..