Partage
  • Partager sur Facebook
  • Partager sur Twitter

Récupérer la valeur d'une liste déroulante

23 septembre 2018 à 20:44:19

Bonsoir

J'ai une liste déroulante sur ma page qui est rempli grace à une de mes table de base donnée .
Je voudrai quand je chosi un champ de ma liste déroulante cela maffiche un tableau par rapport a ce choix.
En faite, il faut juste que je recupere la valeur pour ensuite la mettre dans une requete qui afficha ma table.
Mais je n'arrive pas a recuperer celle-ci, il s'affice um message:

Notice: Undefined index: choix in C:\xampp\htdocs\pdf_export\index.php on line 7, 

Voila mon code 

<?php  
 function fetch_data()  
 {  
      $output = '';  
      $conn = mysqli_connect("localhost", "root", "", "tut");  
     
      	     $ligne_ok = mysqli_query($conn, 'SELECT * FROM pdf_export where age = \''.$_POST['choix'].'\'');
								
	  
	  
	  while($row = mysqli_fetch_array($ligne_ok))  
      {       
      $output .= '<tr>  
                          <td>'.$row["id"].'</td>  
                          <td>'.$row["name"].'</td>  
                          <td>'.$row["age"].'</td>  
                          <td>'.$row["email"].'</td>  
                     </tr>  
                          ';  
      }  
      return $output;  
 }  
 if(isset($_POST["generate_pdf"]))  
 {  
      require_once('tcpdf/tcpdf.php');  
      $obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);  
      $obj_pdf->SetCreator(PDF_CREATOR);  
      $obj_pdf->SetTitle("Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP");  
      $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
      $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
      $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
      $obj_pdf->SetDefaultMonospacedFont('helvetica');  
      $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
      $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);  
      $obj_pdf->setPrintHeader(false);  
      $obj_pdf->setPrintFooter(false);  
      $obj_pdf->SetAutoPageBreak(TRUE, 10);  
      $obj_pdf->SetFont('helvetica', '', 11);  
      $obj_pdf->AddPage();  
      $content = '';  
      $content .= '  
      <h4 align="center">Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP</h4><br /> 
      <table border="1" cellspacing="0" cellpadding="3">  
           <tr>  
                <th width="5%">Id</th>  
                <th width="30%">Name</th>  
                <th width="15%">Age</th>  
                <th width="50%">Email</th>  
           </tr>  
      ';  
      $content .= fetch_data();  
      $content .= '</table>';  
      $obj_pdf->writeHTML($content);  
      $obj_pdf->Output('file.pdf', 'I');  
 }
 
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>SoftAOX | Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP</title>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />            
      </head>  
	  
	  
	  
      <body>  
           <br />
           <div class="container">  
                <h4 align="center"> Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP</h4><br />  
                <div class="table-responsive">  
                	<div class="col-md-12" align="right">
                     <form method="post">  
                          <input type="submit" name="generate_pdf" class="btn btn-success" value="Generate PDF" />  
						  	 <br/>
<?php
    //Include database configuration file
    //include('dbConfig.php');
    $db = mysqli_connect("localhost", "root", "", "tut");  
      
    //Get all country data
     $query = $db->query("SELECT * FROM  pdf_export ");
    
    //Count total number of rows
    $rowCount = $query->num_rows;
    ?>

    <select name="choix" id="choix"  class="form-control" >
        <option value="">Show All</option>
        <?php
        if($rowCount > 0){
				
            while($row = $query->fetch_assoc()){ 
              echo '<option value="'.$row['id'].'">'.$row['age'].'</option>';
            }
        }else{
            echo '????????????????????????';
        }
        ?>
    </select>
                     </form>  
                     </div>
                     <br/>
                     <br/>
					 
					 <!----------------------------------------->
					 
					 
				
        <br/>
                     

					 <!-------------------------------------------->
                     <table class="table table-bordered">  
                          <tr>  
                               <th width="5%">Id</th>  
                               <th width="30%">Name</th>  
                               <th width="15%">Age</th>  
                               <th width="50%">Email</th>  
                          </tr>  
                     <?php  
                     echo fetch_data();  
                     ?>  
                     </table>  
                </div>  
           </div>  
      </body>  
 </html>  

Quelqu'un peut m'aider?

Merci d'avance.

-
Edité par nouno 23 septembre 2018 à 20:59:57

  • Partager sur Facebook
  • Partager sur Twitter
23 septembre 2018 à 20:55:58

Bonjour,

Ta variable $_POST n'existe pas tant que tu n'as pas envoyé le formulaire.

  • Partager sur Facebook
  • Partager sur Twitter
24 septembre 2018 à 12:43:35

j'ai fais une modification, mais affiche un message: "variable n'existe pas"

<?php  
 function fetch_data($choix=NULL)  
 {  
      $output = '';  
      $conn = mysqli_connect("localhost", "root", "", "tut");  
     
      	    if(! empty ($choix)){
   $ligne_ok = mysqli_query($conn, 'SELECT * FROM pdf_export where age = "'.$choix.'"');
	$ligne_ok = mysqli_query($conn, 'SELECT * FROM pdf_export where age =\''.mysqli_real_escape_string($conn,$choix).'\'');

  }
  else{
    $ligne_ok = mysqli_query($conn, 'SELECT * FROM pdf_export');
  }
								
	  
	  
	  while($row = mysqli_fetch_array($ligne_ok))  
      {       
      $output .= '<tr>  
                          <td>'.$row["id"].'</td>  
                          <td>'.$row["name"].'</td>  
                          <td>'.$row["age"].'</td>  
                          <td>'.$row["email"].'</td>  
                     </tr>  
                          ';  
      }  
      return $output;  
 }  
if(isset($_POST['generate_pdf']) && isset($_POST['choix']) ) 
 {  
      require_once('tcpdf/tcpdf.php');  
      $obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);  
      $obj_pdf->SetCreator(PDF_CREATOR);  
      $obj_pdf->SetTitle("Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP");  
      $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
      $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
      $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
      $obj_pdf->SetDefaultMonospacedFont('helvetica');  
      $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
      $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);  
      $obj_pdf->setPrintHeader(false);  
      $obj_pdf->setPrintFooter(false);  
      $obj_pdf->SetAutoPageBreak(TRUE, 10);  
      $obj_pdf->SetFont('helvetica', '', 11);  
      $obj_pdf->AddPage();  
      $content = '';  
      $content .= '  
      <h4 align="center">Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP</h4><br /> 
      <table border="1" cellspacing="0" cellpadding="3">  
           <tr>  
                <th width="5%">Id</th>  
                <th width="30%">Name</th>  
                <th width="15%">Age</th>  
                <th width="50%">Email</th>  
           </tr>  
      ';  
     $content .= fetch_data($_POST['choix']);   
      $content .= '</table>';  
      $obj_pdf->writeHTML($content);  
      $obj_pdf->Output('file.pdf', 'I');  
 }
 
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>SoftAOX | Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP</title>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />            
      </head>  
	  
	  
	  
      <body>  
           <br />
           <div class="container">  
                <h4 align="center"> Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP</h4><br />  
                <div class="table-responsive">  
                	<div class="col-md-12" align="right">
                     <form method="post">  
                          <input type="submit" name="generate_pdf" class="btn btn-success" value="Generate PDF" />  
						  	 <br/>
<?php
    //Include database configuration file
    //include('dbConfig.php');
    $db = mysqli_connect("localhost", "root", "", "tut");  
      
    //Get all country data
     $query = $db->query("SELECT * FROM  pdf_export ");
    
    //Count total number of rows
    $rowCount = $query->num_rows;
    ?>
	
	
    <select name="choix" id="choix"  class="form-control" >
        <option value="">Show All</option>
        <?php
        if($rowCount > 0){
				
            while($row = $query->fetch_assoc()){ 
              echo '<option value="'.$row['id'].'">'.$row['age'].'</option>';
            }
        }else{
            echo '????????????????????????';
        }
        ?>
    </select>
                     </form>  
                     </div>
                     <br/>
                     <br/>
					 
					 <!----------------------------------------->
					 
					 
				
        <br/>
                     

					 <!-------------------------------------------->
                     <table class="table table-bordered">  
                          <tr>  
                               <th width="5%">Id</th>  
                               <th width="30%">Name</th>  
                               <th width="15%">Age</th>  
                               <th width="50%">Email</th>  
                          </tr>  
                     <?php  
					 
					 $choix = ( isset($_POST['choix']) )? $_POST['choix'] : '';
					 if(isset($_POST['choix'] ))
								{
								
												if(!empty($_POST['choix']))
												  {
													  echo "est rempli";
												  }
												else
												
												{
														echo "est vide";
												}
								}
								else
								{
									echo "variable n'existe pas";
								}
					 
					 
					 
                     echo fetch_data( $choix );
                    
                     ?>  
                     </table>  
                </div>  
           </div>  
      </body>  
 </html>  



  • Partager sur Facebook
  • Partager sur Twitter
24 septembre 2018 à 13:23:51

nouno a écrit:

j'ai fais une modification, mais affiche un message: "variable n'existe pas"

Oui, quelle modif, quelle variable ?
  • Partager sur Facebook
  • Partager sur Twitter
24 septembre 2018 à 16:25:07

bonsoir phlodick

Variable que je veux dit " choix".

-
Edité par nouno 24 septembre 2018 à 16:30:06

  • Partager sur Facebook
  • Partager sur Twitter
24 septembre 2018 à 16:48:48

Quelle modification ? Quel est le nouveau code ? A quelle ligne est l'erreur ?
  • Partager sur Facebook
  • Partager sur Twitter
24 septembre 2018 à 16:49:42

que renvoi un var_dump($_POST['choix']); ?
  • Partager sur Facebook
  • Partager sur Twitter
24 septembre 2018 à 23:12:25

Bonsoir phildick

ajouter le code suivante pour  chercher le problème,

if(isset($_POST['choix'] ))
    {
                                 
                                                
      if(!empty($_POST['choix']))
         {
                                                      
                echo "est rempli";
         }
         else
                                              
         {
                                                        
            echo "est vide";
         }
    }
    else
    {
        echo "variable n'existe pas";
    }


affiche message que " $_POST['choix'] " :variable n'existe pas. 

  • Partager sur Facebook
  • Partager sur Twitter
25 septembre 2018 à 9:25:22

Mais tu ne montres aucun formulaire avec un input de ce nom... d'où est censée venir cette variable ?
  • Partager sur Facebook
  • Partager sur Twitter
25 septembre 2018 à 9:45:56

<select name="choix" id="choix"  class="form-control">

<option value="'.$row['id'].'">'.$row['age'].'</option>

</select>


c'est vrai que ton code est vraiment pas propre et difficile a lire.

Nouno, que donne le fameux var_dump après avoir validé le formulaire? null?

si oui peux tu me copier coller le code source (ctrl + u) du select avant envoie du formulaire stp?

  • Partager sur Facebook
  • Partager sur Twitter
25 septembre 2018 à 11:43:56

bonjour. 

le plus simple 

<select name="choix[]" id="choix"  onChange = 'submit()'>
 
<option value="'.$row['id'].'">'.$row['age'].'</option>
 
</select>
 

puis pour récupérer la valeur

 echo $_POST["choix"][0];

qui te renverra la valeur de ta liste déroulante sur laquelle tu as cliqué.

noublies pas de faire  le select name ="choix" un tableau; comme ca name='choix[]'

-
Edité par hubeert 25 septembre 2018 à 11:49:20

  • Partager sur Facebook
  • Partager sur Twitter
25 septembre 2018 à 23:33:24

Bonsoir Monsieur shooo

le fameux var_dump après avoir validé le formulaire:

string(1) "23"

1: id

23: age

voilà le code  de select:

	
    <select   name="choix" id="choix"   class="form-control">
        <option value="">Show All</option>
        <?php
        if($rowCount > 0){
				
            while($row = $query->fetch_assoc()){ 
              echo '<option value="'.$row['age'].'">'.$row['age'].'</option>';
            }
        }else{
            echo '????????????????????????';
        }
        ?>
    </select>

-
Edité par nouno 25 septembre 2018 à 23:47:17

  • Partager sur Facebook
  • Partager sur Twitter