We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Ajax POST

hi

public function winPostAction()
    {

        $this->view->disable();

        if($this->request->isPost() == true) 
        {

            if($this->request->isAjax() == true) 
            {               

                $email = $this->request->getPost('email', 'email');
                $password = $this->request->getPost('password');

                $user = new Users();

                $user->password = $password;

                $user->email = $email;

                $user->save();
            }
        }
        else
        {
            $this->response->setStatusCode(404, "Not Found");
        }
    }

i want to POST data with ajax , but i can not post data to my database

it is my ajax code

<script type="text/javascript">

$(document).ready(function()
{
  $.ajax({
    url: "<?php echo $this->url->get('win/win') ?>"
  }).done(function(data) 
  {
      console.log(data)
  });

  $("#form").on("submit", function(e)
  {
    e.preventDefault();
    $.post("<?php echo $this->url->get('win/winPost') ?>", $(this).serialize() , function(data)
    {
             console.log(data);
      }).done(function() { 

          alert("cool");

      }).fail(function() {

          alert("error"); 
      })
  })
})

</script>

<?php 
    echo $this->tag->form(
        array(
            "win/winPost", "method" => "post", "id" => "form"
        )
    ); 
?>

    <p>
        <label for="email">Email:</label>
        <?php 
            echo $this->tag->textField("email");
        ?>
        <label for="password">Password</label>
        <?php 
            echo $this->tag->passwordField("password");
        ?>
    </p>

    <p>
        <?php echo $this->tag->submitButton("Registrarme"); ?>
    </p>
</form>



51.2k
edited May '15

Any save() errors ?

if (false == $user->save()) {
    foreach ($user->getMessages() as $message) {
        echo $message->getMessage(), "\n";
    }
}
edited May '15

Any save() errors ?

u can see :) i can not insert data to database with AJAX POST request



51.2k

Sorry. It is $user->getMessages() . getMessages() is a function

Any save() errors ?

u can see :) i can not insert data to database with AJAX POST request

edited May '15

thank u calin rada, i solved this