Jump to content

Add input form dynamically in admin page


tarek.fellah

Recommended Posts

 

I'm extending a page (emplyee clone)  in admin prestashop to add an input text zip code and add cities checkboxs dynamically after entring the zipcod, cities will be the ajax result, i used a function to retrieve cities and create checkboxs but it doesn't work, this is the ajax function in the controller

public function ajaxProcessGetVille(){
$zip_cod = Tools::getValue('zip_cod');
$html = file_get_contents("http://www.cp-ville.com/cpcom.php?cpcommune=".$zip_cod);
$i = 0;
$s = strlen($html);
$htmlF = "";

for( $i = 0; $i < ($s - 188) ; $i++){

$htmlF = $htmlF.$html[$i + 186];

}


$c = $json = json_decode($htmlF)->count;
$f = "";

$options = array();
for( $i = 1; $i <= $c ; $i++){

$f = "".$i."";
$ville = json_decode($htmlF)->$f->ville;

$options[] = array(
"id" => $ville,
"name" => $ville
);

}
//var_dump($this->fields_form);exit();
//$this->fields_form =$this->fileds_temp;

$this->fields_form['input'][]= array(
'type' => 'checkbox', // This is an <input type="checkbox"> tag.
'label' => $this->l('Options'), // The <label> for this <input> tag.
'desc' => $this->l('Choose options.'), // A help text, displayed right next to the <input> tag.
'name' => 'options', // The content of the 'id' attribute of the <input> tag.
'values' => array(
'query' => $options, // $options contains the data itself.
'id' => 'id_option', // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.
'name' => 'name' // The value of the 'name' key must be the same as the key for the text content of the <option> tag in each $options sub-array.
));
return parent::renderForm();

//die($htmlF);
}

When i tested the form variable $this->fields_form it retrun null, before adding the checkbox adding code with $this->fields_form['input'][],

this is the js

$(document).ready(function(){
$('input[name=postal_cod]').keyup(function(){
if ($(this).val().length>= 4){


$.ajax({
url: "{$link->getAdminLink('AdminPrestataires')|addslashes}",
cache: false,
data : {
ajax : '1',
action : 'GetVille',
zip_cod : $(this).val()
},
dataType : 'json',
success : function(resp,textStatus,jqXHR)
{
ch ='';
//json =$.parseJSON(resp);
$.each(resp, function(idx, obj){
if(obj.ville != undefined) //alert(obj.ville);
ch += '<input type="checkbox" value="'+obj.ville+'"/> '; 
});
//alert(resp); 
$('input[name=ville]').val(ch);
}

});

}
});

Any one have any idea and thanks.

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...