-
pippuccio76 Senior Member
   
-
Posts: 547
Threads: 231
Joined: Jun 2017
Reputation:
2
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 ?
-
rtenny Member
  
-
Posts: 134
Threads: 2
Joined: Jun 2016
Reputation:
3
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.
-
rtenny Member
  
-
Posts: 134
Threads: 2
Joined: Jun 2016
Reputation:
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.
-
Paradinight Senior Member
   
-
Posts: 445
Threads: 6
Joined: Jun 2015
Reputation:
25
(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');
-
pippuccio76 Senior Member
   
-
Posts: 547
Threads: 231
Joined: Jun 2017
Reputation:
2
(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?
-
Paradinight Senior Member
   
-
Posts: 445
Threads: 6
Joined: Jun 2015
Reputation:
25
(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
-
ivantcholakov Senior Member
   
-
Posts: 668
Threads: 17
Joined: Oct 2014
Reputation:
37
@ 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.
|