Welcome Guest, Not a member yet? Register   Sign In
interesting problem about ajax,javascript,php,model =( ,please help me
#1

[eluser]C_O_S[/eluser]
hi everybody

i am getting interesting warning.

i am creating table with js in my view and this table has select boxes.when i click button ,adding new line.there is one select box in this line and options(this selectbox) are coming db but how?

one js file that creates this table.(external file)

i call one ajax request and this reguest is going to call one function in controller.This controller get data from one model.Model return data and controller is writing this data(php echo)and ajax request get this writen data and write view.

loadedview->click->creating ajx request and newline(selectbox in thisline)->ajax request->function(controller)->function(model)->DATA is returning->controller->writing DATA with echo in controller.->ajax request is getting this DATA->writing options where selectbox.

Code:
function loadAuctionListRate(){
        $query=$this->db->get('auction_rate');
        foreach($query->result() as $row){
            $option.="<option value=\"".$row->id."\">".$row->rate."</option>";
            
        }
        return $option;
    }
this is my model function

appending SELECTbox ->>>>>>>an external js file
Code:
var rate=new_job.insertCell(6);
        rate.className="rate";
        rate.width="50px";
        rate.align="center";
    var select_rate=document.createElement('select');
        select_rate.name=rate.className+group_id+line_number;
        
    $(document).ready(function(){
                    
            $.ajax({
                type: "POST",
                url:  "controller(not important)",
                success: function(answer) {
                    
                    select_rate[removed]=answer;
                    },
                dataType: "html"
                        
            });

    });
    
        rate.appendChild(select_rate);

controller
Code:
function AuctionListRateAjax(){
        $this->load->model('muser_profile','',TRUE);
        $options=$this->muser_profile->loadAuctionListRate();
        echo $options;
    }


and
A PHP Error was encountered Severity: Notice Message: Undefined variable: option Filename: models/muser_profile.php Line Number: 30



how can i solve this? PLEASE HELP ME=)
#2

[eluser]OliverHR[/eluser]
Code:
function loadAuctionListRate(){
   $option = NULL;

   $query = $this->db->get('auction_rate');
  
   foreach($query->result() as $row){
       $option .= '<option value="' . $row->id. '">' . $row->rate . '</option>';
   }
  
   return $option;
}
#3

[eluser]CodeIgniteMe[/eluser]
+1 point to OliverHR's answer
#4

[eluser]C_O_S[/eluser]
it worksBig Grin thank you so much=)but why i cant do $option.="asd"; directly?
#5

[eluser]OliverHR[/eluser]
[quote author="C_O_S" date="1312906052"]why i cant do $option.="asd"; directly?[/quote]

In fact it is an error, and no matter what it is a BAD BAD BAD practice.
#6

[eluser]jmadsen[/eluser]
[quote author="C_O_S" date="1312906052"]it worksBig Grin thank you so much=)but why i cant do $option.="asd"; directly?[/quote]

"$option .=" says "add $option to itself", but "itself" doesn't exist yet.
#7

[eluser]CodeIgniteMe[/eluser]
[quote author="OliverHR" date="1312920955"][quote author="C_O_S" date="1312906052"]why i cant do $option.="asd"; directly?[/quote]

In fact it is an error, and no matter what it is a BAD BAD BAD practice.[/quote]

It is because the declaration of $option is at the scope that is not seen by the return $option; It's the same for all languages. I think it's called variable life-span or scope. The variable is only seen inside and beneath the container where it is declared.
Code:
Class sample{
public $var;
function some_func(){
// you can modify it here
$this->var = 'something';
// or here
for(;;):
$this->var .= 'loop';
endfor;
echo $this->var;
}
}
the same as here.
Code:
function sample2(){
$var;
for(;;):
$var .= 'loop';
endfor;
echo $var;
}
but not like this
Code:
function sample3(){
if(isset($_POST]){
$var = 'yes';
}else{
$var = 'no';
}
echo $var;
}




Theme © iAndrew 2016 - Forum software by © MyBB