Error: Notice: Undefined index: D1

Asked

Viewed 226 times

1

I’m creating a filter for an online store, which will have these fields: inserir a descrição da imagem aqui

And so far select * was working, but when I started deploying the filter it stopped working.

Here is the form code and order to print:

<div class="conainer">
  <div class="card mx-auto w-100">
    <div class="card-header">
        <h5 style="color:#007BFF;">Molas de compressão Pesquisa</h5>
    </div>
    <div class="card-body">
      <form action="action.php" method="POST">
        Referencia:
        <input type="text" placeholder="&#xF002; Search" name="referencia"style="font-family:Arial, FontAwesome">
        <br><br>
        <div class="row">
          Diametro do Aço (d):
          <div class="col-md-4">
            <input type="text" name="d1" size="6" placeholder="min"> 
            <input type="text" name="d2"  size="6" placeholder="max">
          </div>
          Comprimento total (L0):
          <div class="col-md-4">
            <input type="text" name="comp1"  size="6" placeholder="min"> 
            <input type="text" name="comp2"  size="6" placeholder="max">
          </div>
        </div>
        <br>
        <div class="row">
          Diametro Exterior (DE):
          <div class="col-md-4">
            <input type="text" size="6" name="de1" placeholder="min"> 
            <input type="text" name="de2" size="6" placeholder="max">
          </div>
          Passo (P):
          <div class="col-md-4">
            <input type="text" name="passo1" size="6" placeholder="min"> 
            <input type="text" name="passo2" size="6" placeholder="max">
          </div>
          <br>
        </div>
        <br>
        <div class="row">
          <div class="form-group">
            <label for="exampleFormControlSelect1">Ordenar por:</label>
            <select class="form-control" name="ordenar">
              <option>Referencia</option>
              <option>Diametro do Aço (d)</option>
              <option>Comprimento total (L0)</option>
              <option>Diametro Exterior (DE)</option>
              <option>Passo (P)</option>
            </select>
          </div>
          <div class="col-md-9">
            <div align="right">
              <img src="imagens/desenho33.png" align="right" height="250" width="350" >
            </div>
          </div>
        </div>
        <br>
        <div align="right">
          <button type="reset" align="right" class="btn btn-primary">Reset</button>
          <button type="submit" align="right" class="btn btn-primary">Continuar</button>   
        </div>
      </form>
    </div>
  </div>
</div>
<br>
<div class="jumbotron">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-1">Referência</div>
      <div class="col-md-2">Diametro do Aço (DE)</div>
      <div class="col-md-2">Comprimento Total (L0)</div>
      <div class="col-md-2">Diametro Exterior</div>
      <div class="col-md-1">Passo</div>
      <div class="col-md-1">Preço</div>
      <div class="col-md-1"></div>
    </div>
  </div>
</div>
<div id="get_product"></div>

Here’s the other file you should select and then print on the other page:

<?php


$d1=$_POST["d1"];
$d2=$_POST["d2"];
$comp1=$_POST["comp1"];
$comp2=$_POST["comp2"];
$de1=$_POST["de1"];
$de2=$_POST["de2"];
$passo1=$_POST["passo1"];
$passo2=$_POST["passo2"];
$ordenar=$_POST["ordenar"];


include ("db.php");



if(isset($_POST["getproduct"])){
    $ordenar="nome";
}
else{
$ordenar=$_GET["ordenar"];		
}
switch($ordenar){
case "referencia":
	$ordenar_por="order by referencia";	
case "diametroaco":
	$ordenar_por="order by diametroaco";	
case "comprimentototal":
	$ordenar_por="order by comprimentototal";	
case "diametroexterior":
	$ordenar_por="order by diametroexterior";
case "passo":
	$ordenar_por="order by passo";
}
    
    // $molcomp_query="SELECT * FROM stock_comp";
    $molcomp_query="select * from stock_comp where diametroaco BETWEEN '".$d1."' and '".$d2."' $ordenar_por";
   $run_query = mysqli_query($con,$molcomp_query);
    if (mysqli_num_rows($run_query) > 0){
        while($row = mysqli_fetch_array($run_query)){
        $id_mol_comp=$row['id_mol_comp'];
        $referencia=$row['referencia'];
        $diametroaco=$row['diametroaco'];
        $comprimentototal=$row['comprimentototal'];
        $diametroexterior=$row['diametroexterior'];
        $passo=$row['passo'];
        $preco=$row['preco']; 

           echo"
            <div class='card mx-auto w-100'>
  <div class='card-body'>
   <div class='row'>
                <div class='col-md-1'>$referencia</div>
                <div class='col-md-2'>$diametroaco</div>
                <div class='col-md-2'>$comprimentototal</div>
                <div class='col-md-2'>$diametroexterior</div>
                <div class='col-md-1'>$passo</div>
                <div class='col-md-1'>$preco</div>
                <div class='col-md-1'><i style='font-size:30px' class='fa'>&#xf1c1;</i>  <i style='font-size:30px' class='fa'>&#xf217;</i>   </div>
                </div>
</div>
</div>
<br>
";
        }
    }



?>

And now when I click on the "continue" button of the form, the site exchange page, to http://localhost/fanamol/action.php due to <form action="action.php" method="POST">. As I can click the continue button, and the action is executed in the action.php file and return the information to the main file, without the main file leaving the screen?

  • 1

    To do what you want, search by Ajax

  • 1

    I know what ajax is, but I can’t just make a select with conditions and print on the same page where I already printed select * before?

  • 1

    Yes, with Ajax. Otherwise you will have to leave the page.

  • Just like @Andersoncarloswoss said.

1 answer

1


The <select> was with the <option> valueless, use:

<select class="form-control" name="ordenar">
    <option value="referencia">Referencia</option>
    <option value="diametroaco">Diametro do Aço (d)</option>
    <option value="comprimentototal">Comprimento total (L0)</option>
    <option value="diametroexterior">Diametro Exterior (DE)</option>
    <option value="passo">Passo (P)</option>
</select>

Using the <form method="POST"> all parameters passed to the file PHP of action="action.php" should be as $_POST, for example, here is correct:

$d1=$_POST["d1"];
$d2=$_POST["d2"];
$comp1=$_POST["comp1"];
$comp2=$_POST["comp2"];
$de1=$_POST["de1"];
$de2=$_POST["de2"];
$passo1=$_POST["passo1"];
$passo2=$_POST["passo2"];
$ordenar=$_POST["ordenar"];

But here, this with $_GET:

if(isset($_POST["getproduct"])){
    $ordenar="nome";
}else{
    **$ordenar=$_GET["ordenar"];**
}

But as the variable $ordenar has already been defined previously, just do the if():

if(isset($_POST["getproduct"])){
    $ordenar="nome";
}

If it’s for $.ajax():

$('#send').click(function(){
    var arr = [];
    $.each($('input[type="text"]'),function(){
        var key = $(this).attr('name');
        arr[key] = $(this).val();
    });
    arr['ordenar']=$('#ordenar option:selected').val();
    $.ajax({
        type:"POST",
        url:"action.php",
        data:arr,
        success:function(callback){
            $('#get_product').html(callback);
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="conainer">
    <div class="card mx-auto w-100">
        <div class="card-header">
            <h5 style="color:#007BFF;">Molas de compressão Pesquisa</h5>
        </div>
        <div class="card-body">
            <section>
                Referencia:
                <input type="text" placeholder="&#xF002; Search" name="referencia"style="font-family:Arial, FontAwesome">
                <br><br>
                <div class="row">
                    Diametro do Aço (d):
                    <div class="col-md-4">
                        <input type="text" name="d1" size="6" placeholder="min"> 
                        <input type="text" name="d2"  size="6" placeholder="max">
                    </div>
                    Comprimento total (L0):
                    <div class="col-md-4">
                        <input type="text" name="comp1"  size="6" placeholder="min"> 
                        <input type="text" name="comp2"  size="6" placeholder="max">
                    </div>
                </div>
                <br>
                <div class="row">
                    Diametro Exterior (DE):
                    <div class="col-md-4">
                        <input type="text" size="6" name="de1" placeholder="min"> 
                        <input type="text" name="de2" size="6" placeholder="max">
                    </div>
                    Passo (P):
                    <div class="col-md-4">
                        <input type="text" name="passo1" size="6" placeholder="min"> 
                        <input type="text" name="passo2" size="6" placeholder="max">
                    </div>
                    <br>
                </div>
                <br>
                <div class="row">
                    <div class="form-group">
                        <label for="exampleFormControlSelect1">Ordenar por:</label>
                        <select class="form-control" name="ordenar" id="ordenar">
                            <option value="referencia">Referencia</option>
                            <option value="diametroaco">Diametro do Aço (d)</option>
                            <option value="comprimentototal">Comprimento total (L0)</option>
                            <option value="diametroexterior">Diametro Exterior (DE)</option>
                            <option value="passo">Passo (P)</option>
                        </select>
                    </div>
                    <div class="col-md-9">
                        <div align="right">
                            <img src="imagens/desenho33.png" align="right" height="250" width="350" >
                        </div>
                    </div>
                </div>
                <br>
                <div align="right">
                    <button type="reset" align="right" class="btn btn-primary">Reset</button>
                    <button align="right" class="btn btn-primary" id="send">Continuar</button>   
                </div>
            </section>
        </div>
    </div>
</div>
<br>
<div class="jumbotron">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-1">Referência</div>
            <div class="col-md-2">Diametro do Aço (DE)</div>
            <div class="col-md-2">Comprimento Total (L0)</div>
            <div class="col-md-2">Diametro Exterior</div>
            <div class="col-md-1">Passo</div>
            <div class="col-md-1">Preço</div>
            <div class="col-md-1"></div>
        </div>
    </div>
</div>
<div id="get_product"></div>

  • 1

    this part is giving an error: Undefined index: D1 which corresponds to this part :$D1=$_POST["D1"]; $D2=$_POST["D2"]; $comp1=$_POST["comp1"]; $comp2=$_POST["comp2"]; $de1=$_POST["de1"]; $de2=$_POST["de2"]; $Passo1=$_POST["Passo1"; $Passo2";$Passo1"]; $Passo2=$Passo2=$_POST["Passo2"]; $sort=$_POST["sort"];

  • 1

    makes a post checker

  • 1

    if(isset($_POST)&&!Empty($_POST)){ //code }

  • 1

    opah I’m sorry for the inconvenience, but it hasn’t worked yet, it continues with the same error, even after having put if(isset($_POST)&&!Empty($_POST)){ //code } if(isset($_POST)&!Empty($_POST))"#Xa; $D1=$_POST["D1"]; $D2=$_POST["D2"]; $comp1=$_POST["comp1"]; $comp2=$_POST["comp2"]; $de1=$_POST["de1"]; $de2=$_POST["de2"]; $Passo1=$_;}

  • 1

    You’re using form or ajax?

  • I’m using form

  • 1
Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.