Using PHP & MySQL to populate dropdown

后端 未结 1 428
我寻月下人不归
我寻月下人不归 2021-01-17 00:33

I am trying to populate a second dropdown list using the value selected from a first dropdown list using PHP and MySQL, and without refresh

1条回答
  •  梦毁少年i
    2021-01-17 01:26

    Like other members have says, you should use PDO (with prepared statements) instead of mysql_.

    One possible implementation:

    HTML (form.php)

    
    
    
    
    
    

    PHP (get_list2.php)

    require_once("config.php");
    
    $id = $_GET['id'];
    
    if (!isset($id) || !is_numeric($id))
        $reponse = array('success' => FALSE);
    else {
        // Where $db is a instance of PDO
    
        $query = $db->prepare("SELECT * FROM mytable WHERE id = :id");
        $query->execute(array(':id' => $id));
        $rows = $query->fetchAll(PDO::FETCH_ASSOC);
    
        $options = "";
        foreach ($rows as $row) {
            $options .= '';
        }
    
        $response = array(
            'success' => TRUE,
            'options' => $options
        );
    }
    
    header('Content-Type: application/json');
    echo json_encode($response);
    

    PS : not tested but it should works... I guess.

    0 讨论(0)
提交回复
热议问题