tags:

views:

69

answers:

2

I have been trying to retrieve data via AJAX. I can't seem to be able to 'read' what was sent to me by PHP. Here's the code

  $('create_course').addEvent('submit', function(e){
   e.stop();
   flash.setStyle('display', 'none');
   this.set('send', {
    onComplete: function(resp){
     if ($chk(resp))
     {
      console.log($type(resp));
      if (resp == 'true')
      {
       flash.set('html', resp);
       flash.reveal();
      }
      elseif (resp == 'false')
      {
       $$('div.information').dissolve();
       $$('div.options').reveal();
      }

     }
    }
   }).send();
  });

A different action will happen when I receive a true and a different one on false.

This was the code.

  if (is_ajax())
  {
   if ($this->form_validation->run('course_create') === TRUE)
   {
    $course = array(
     'name'  => $this->input->post('name'),
     'description'  => $this->input->post('desc'),
     'price' => $this->input->post('price')
    );


    if ($this->course->create($course))
    {
     echo 'true';
    }
    else
    {
     echo 'false';
    }
   }
   else
   {
    echo validation_errors('<div class="message error"><p>', '</p></div>');
   }
  }

Note: I've modified the php and js to just read if there's a response. So this php and js is what is used to be. My point is how do I 'read' the return value in Mootools/JS.

A: 

if you add an

alert(res);

what does it give you ?

instead of

if ($this->course->create($course))
    {
     echo 'true';
    }
    else
    {
     echo 'false';
    }

could you do

if ($this->course->create($course))
    {
     die 'true';
    }
    else
    {
     die 'false';
    }
RageZ
I've never checked using alert but console.log gives me atrue
Thorpe Obazee
yes but you are doing `$type(resp)` I would have preferred `console.log(resp);`
RageZ
I did that already before I thought of looking at the type. it gave me 'true', without the quotes.
Thorpe Obazee
so `console.log(resp == 'true');` give ?
RageZ
yeap. What I don't understand is why doesn't the conditional state work.
Thorpe Obazee
+1 for you... I must be missing something..
Thorpe Obazee
you must have a character outputed by PHP that's why I would have use `die`
RageZ
+1  A: 

I've solved it. I think. I used Element.match()

$('create_course').addEvent('submit', function(e){
      e.stop();
      flash.setStyle('display', 'none');
      this.set('send', {
       onComplete: function(resp){

        var is_validated = resp.match("true");

        if (is_validated)
        {
         flash.set('html', resp);
         flash.reveal();
        }
        else
        {
         $$('div.information').dissolve();
         $$('div.options').reveal();
        }
       }
      }).send();
     });
Thorpe Obazee