• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ajax concatenate query

#1
Hi, sorry for english, i have 3 select input , i want concatenate , by selecting a value of first select only some values are show in second select , the same for the third.


This is my html ,this code is for the first and second select :


Code:
<div class="col-md-4 text-center login_form">
                 
                 <?= form_label('Regione*:' ,'regione',"class='black'") ;?>
                  <br>
                 <div class="form-group">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                 <select id="regione_id" name="regione" >
                 <option value="" selected="selected">Regione --</option>
                   
                 <?php  foreach($regioni->result() as $regione) : ?>
                 <option value="<?php echo $regione->id ; ?>"><?php echo $regione->regione; ?></option>
                 <?php endforeach ; ?>
                 </select>
                    </div>
                 </div>
                 
             
           </div>
            <div class="col-md-4 text-center login_form">
                 <p>
                 <?= form_label('Provincia*:' ,'provincia',"class='black'") ;?>
                  <br>
                  <div class="form-group">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                 <select id="province_id" name="province" >
                 
                 </select>
                    </div>
                 </div>
                 
             </p>
           </div>

this is the ajax:

Code:
<script
 src="https://code.jquery.com/jquery-3.2.1.min.js"
 integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
 crossorigin="anonymous"></script>

<script>

$(document).ready(function() {
 $("#regione_id").change(function(){
   
   var regione = $("#regione_id").val();
   
   $.ajax({
     type: "POST",
     url: "<?php echo base_url(); ?>index.php/user/get_province",
     data: "regione=1"  ,
     dataType: "html",
     success: function(msg)
     {
       $("#province_id").html(msg);
     },
     error: function()
     {
       alert("Chiamata fallita, si prega di riprovare...");
     }
   });
 });
});
</script>

this is the get_province() :

Code:
 public function get_province(){
       
     $regioni_id=$this->input->post('regione');
     
     $query = $this->db->query("SELECT * FROM  province WHERE id_regione= $regioni_id  ");
     echo '<option value="" selected="selected">-- seleziona --</option>';
        foreach($query->result() as $province){
       
        $id=$province->id;
        $provincia=$province->provincia;
        echo "<option value='$id'>$provincia</option>";
 
        }
     }
     



But when i change a value of first select i have error alert :  "Chiamata fallita, si prega di riprovare..."

how can i solve ?
Reply

#2
You dont seem to pass the region value in the ajax

data: "regione=1"
I think that should be
data: "regione=" + regione,

otherwise region is always 1
On the package it said needs Windows 7 or better. So I installed Linux.
Reply

#3
One more thing.
You use $regioni_id=$this->input->post('regione');
But this is not a form request you are doing a ajax request. not sure that ->input will work here

I tyhink you need to do this here:
$regioni_id = $_POST['regione'];
On the package it said needs Windows 7 or better. So I installed Linux.
Reply

#4
(07-12-2017, 09:50 AM)rtenny Wrote: You dont seem to pass the region value in the ajax

data: "regione=1"
I think that should be
data: "regione=" +  regione,

otherwise region is always 1

To debug i set regione to 1 to  be sure....
Reply

#5
(07-12-2017, 09:55 AM)rtenny Wrote: One more thing.
You use $regioni_id=$this->input->post('regione');
But this is not a form request you are doing a ajax request. not sure that ->input will work here

I tyhink you need to do this here:
$regioni_id = $_POST['regione'];

Do $_POST['regione'] instead $this->input->post('regione') but same problem.
Reply

#6
(07-10-2017, 02:50 PM)pippuccio76 Wrote: Hi, sorry for english, i have 3 select input , i want concatenate , by selecting a value of first select only some values are show in second select , the same for the third.


This is my html ,this code is for the first and second select :


Code:
<div class="col-md-4 text-center login_form">
                 
                 <?= form_label('Regione*:' ,'regione',"class='black'") ;?>
                  <br>
                 <div class="form-group">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                 <select id="regione_id" name="regione" >
                 <option value="" selected="selected">Regione --</option>
                   
                 <?php  foreach($regioni->result() as $regione) : ?>
                 <option value="<?php echo $regione->id ; ?>"><?php echo $regione->regione; ?></option>
                 <?php endforeach ; ?>
                 </select>
                    </div>
                 </div>
                 
             
           </div>
            <div class="col-md-4 text-center login_form">
                 <p>
                 <?= form_label('Provincia*:' ,'provincia',"class='black'") ;?>
                  <br>
                  <div class="form-group">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                 <select id="province_id" name="province" >
                 
                 </select>
                    </div>
                 </div>
                 
             </p>
           </div>

this is the ajax:

Code:
<script
 src="https://code.jquery.com/jquery-3.2.1.min.js"
 integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
 crossorigin="anonymous"></script>

<script>

$(document).ready(function() {
 $("#regione_id").change(function(){
   
   var regione = $("#regione_id").val();
   
   $.ajax({
     type: "POST",
     url: "<?php echo base_url(); ?>index.php/user/get_province",
     data: "regione=1"  ,
     dataType: "html",
     success: function(msg)
     {
       $("#province_id").html(msg);
     },
     error: function()
     {
       alert("Chiamata fallita, si prega di riprovare...");
     }
   });
 });
});
</script>

this is the get_province() :

Code:
 public function get_province(){
       
     $regioni_id=$this->input->post('regione');
     
     $query = $this->db->query("SELECT * FROM  province WHERE id_regione= $regioni_id  ");
     echo '<option value="" selected="selected">-- seleziona --</option>';
        foreach($query->result() as $province){
       
        $id=$province->id;
        $provincia=$province->provincia;
        echo "<option value='$id'>$provincia</option>";
 
        }
     }
     



But when i change a value of first select i have error alert :  "Chiamata fallita, si prega di riprovare..."

how can i solve ?

$query = $this->db->query("SELECT * FROM  province WHERE id_regione= $regioni_id  "); <- bad idea. vulnerable to sql injection

from:
data: "regione=1"
to
data: { regione: regione }

you can use $this->input->post('regione');
Reply

#7
(07-12-2017, 10:43 AM)Paradinight Wrote:
(07-10-2017, 02:50 PM)pippuccio76 Wrote: Hi, sorry for english, i have 3 select input , i want concatenate , by selecting a value of first select only some values are show in second select , the same for the third.


This is my html ,this code is for the first and second select :


Code:
<div class="col-md-4 text-center login_form">
                 
                 <?= form_label('Regione*:' ,'regione',"class='black'") ;?>
                  <br>
                 <div class="form-group">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                 <select id="regione_id" name="regione" >
                 <option value="" selected="selected">Regione --</option>
                   
                 <?php  foreach($regioni->result() as $regione) : ?>
                 <option value="<?php echo $regione->id ; ?>"><?php echo $regione->regione; ?></option>
                 <?php endforeach ; ?>
                 </select>
                    </div>
                 </div>
                 
             
           </div>
            <div class="col-md-4 text-center login_form">
                 <p>
                 <?= form_label('Provincia*:' ,'provincia',"class='black'") ;?>
                  <br>
                  <div class="form-group">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                 <select id="province_id" name="province" >
                 
                 </select>
                    </div>
                 </div>
                 
             </p>
           </div>

this is the ajax:

Code:
<script
 src="https://code.jquery.com/jquery-3.2.1.min.js"
 integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
 crossorigin="anonymous"></script>

<script>

$(document).ready(function() {
 $("#regione_id").change(function(){
   
   var regione = $("#regione_id").val();
   
   $.ajax({
     type: "POST",
     url: "<?php echo base_url(); ?>index.php/user/get_province",
     data: "regione=1"  ,
     dataType: "html",
     success: function(msg)
     {
       $("#province_id").html(msg);
     },
     error: function()
     {
       alert("Chiamata fallita, si prega di riprovare...");
     }
   });
 });
});
</script>

this is the get_province() :

Code:
 public function get_province(){
       
     $regioni_id=$this->input->post('regione');
     
     $query = $this->db->query("SELECT * FROM  province WHERE id_regione= $regioni_id  ");
     echo '<option value="" selected="selected">-- seleziona --</option>';
        foreach($query->result() as $province){
       
        $id=$province->id;
        $provincia=$province->provincia;
        echo "<option value='$id'>$provincia</option>";
 
        }
     }
     



But when i change a value of first select i have error alert :  "Chiamata fallita, si prega di riprovare..."

how can i solve ?

$query = $this->db->query("SELECT * FROM  province WHERE id_regione= $regioni_id  "); <- bad idea. vulnerable to sql injection

from:
data: "regione=1"
to
data: { regione: regione }

you can use $this->input->post('regione');

This isn't the problem , the problem is :why get i  an error response from ajax?
Reply

#8
(07-12-2017, 11:58 AM)pippuccio76 Wrote:
(07-12-2017, 10:43 AM)Paradinight Wrote:
(07-10-2017, 02:50 PM)pippuccio76 Wrote: Hi, sorry for english, i have 3 select input , i want concatenate , by selecting a value of first select only some values are show in second select , the same for the third.


This is my html ,this code is for the first and second select :


Code:
<div class="col-md-4 text-center login_form">
                 
                 <?= form_label('Regione*:' ,'regione',"class='black'") ;?>
                  <br>
                 <div class="form-group">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                 <select id="regione_id" name="regione" >
                 <option value="" selected="selected">Regione --</option>
                   
                 <?php  foreach($regioni->result() as $regione) : ?>
                 <option value="<?php echo $regione->id ; ?>"><?php echo $regione->regione; ?></option>
                 <?php endforeach ; ?>
                 </select>
                    </div>
                 </div>
                 
             
           </div>
            <div class="col-md-4 text-center login_form">
                 <p>
                 <?= form_label('Provincia*:' ,'provincia',"class='black'") ;?>
                  <br>
                  <div class="form-group">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                 <select id="province_id" name="province" >
                 
                 </select>
                    </div>
                 </div>
                 
             </p>
           </div>

this is the ajax:

Code:
<script
 src="https://code.jquery.com/jquery-3.2.1.min.js"
 integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
 crossorigin="anonymous"></script>

<script>

$(document).ready(function() {
 $("#regione_id").change(function(){
   
   var regione = $("#regione_id").val();
   
   $.ajax({
     type: "POST",
     url: "<?php echo base_url(); ?>index.php/user/get_province",
     data: "regione=1"  ,
     dataType: "html",
     success: function(msg)
     {
       $("#province_id").html(msg);
     },
     error: function()
     {
       alert("Chiamata fallita, si prega di riprovare...");
     }
   });
 });
});
</script>

this is the get_province() :

Code:
 public function get_province(){
       
     $regioni_id=$this->input->post('regione');
     
     $query = $this->db->query("SELECT * FROM  province WHERE id_regione= $regioni_id  ");
     echo '<option value="" selected="selected">-- seleziona --</option>';
        foreach($query->result() as $province){
       
        $id=$province->id;
        $provincia=$province->provincia;
        echo "<option value='$id'>$provincia</option>";
 
        }
     }
     



But when i change a value of first select i have error alert :  "Chiamata fallita, si prega di riprovare..."

how can i solve ?

$query = $this->db->query("SELECT * FROM  province WHERE id_regione= $regioni_id  "); <- bad idea. vulnerable to sql injection

from:
data: "regione=1"
to
data: { regione: regione }

you can use $this->input->post('regione');

This isn't the problem , the problem is :why get i  an error response from ajax?

f12 and check the network tab and the console
Reply

#9
@pippuccio76

Use the browser development tools to see the response itself, especially the returned HTTP status code. Probably it is not 200, this is is why you see the message "Chiamata fallita, si prega di riprovare...".

If it is 404 - there is a problem about routing;
if it is 500 - there is an error within code - see the response body, the error would be echoed there.
Reply

#10
Replace the data part in your Ajax request:

Code:
data: { regione : 1 },
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.