Ajax concatenate query - pippuccio76 - 07-10-2017
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 ?
RE: Ajax concatenate query - rtenny - 07-12-2017
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
RE: Ajax concatenate query - rtenny - 07-12-2017
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'];
RE: Ajax concatenate query - pippuccio76 - 07-12-2017
(07-12-2017, 08: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....
RE: Ajax concatenate query - pippuccio76 - 07-12-2017
(07-12-2017, 08: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.
RE: Ajax concatenate query - Paradinight - 07-12-2017
(07-10-2017, 01: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');
RE: Ajax concatenate query - pippuccio76 - 07-12-2017
(07-12-2017, 09:43 AM)Paradinight Wrote: (07-10-2017, 01: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?
RE: Ajax concatenate query - Paradinight - 07-12-2017
(07-12-2017, 10:58 AM)pippuccio76 Wrote: (07-12-2017, 09:43 AM)Paradinight Wrote: (07-10-2017, 01: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
RE: Ajax concatenate query - ivantcholakov - 07-12-2017
@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.
RE: Ajax concatenate query - Wouter60 - 07-12-2017
Replace the data part in your Ajax request:
Code: data: { regione : 1 },
|