I study Kohana, I wanted to learn how to use Kohana + Ajax together, I wanted to make out an example (the example on Php + Ajax works fine), transferred it to Kohana, but to me, besides the answer, Ayaksu returns another page of the main template, and not defined when i try to output.

expect: name: $ name, phone: $ phonenumber.

I receive (in more detail in console.log below): the page from the main template and name: undefined, phone: undefined.

code:

Controller / example.php:

<?php defined('SYSPATH') or die('No direct script access.'); class Controller_Example extends Controller_Common { public function action_index() { $content = View::factory('/example/show') ->bind('example', $example) ->bind('errors', $errors); if ($this->request->is_ajax()) { if (isset($_POST["name"]) && isset($_POST["phonenumber"]) ) { $result = array( 'name' => $_POST["name"], 'phonenumber' => $_POST["phonenumber"] ); print_r($result); echo json_encode($result); } } else $this->template->content=$content; }} 

View:

 <!doctype html> <html lang="en"> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> </head> <body> <form method="post" id="ajax_form" action="" > <input type="text" name="name" placeholder="NAME" /><br> <input type="text" name="phonenumber" placeholder="YOUR PHONE" /><br> <input type="button" id="btn" value="send" /> </form> <br> <div id="result_form"></div> </body> </html> <script> $( document ).ready(function() { $("#btn").click( function(){ sendAjaxForm('result_form', 'ajax_form'); return false; } ); }); function sendAjaxForm(result_form, ajax_form) { jQuery.ajax({ url: '', type: "POST", dataType: "html", data: jQuery("#"+ajax_form).serialize(), success: function(response) { //console.log(response); //result = jQuery.parseJSON(response); result = response; console.log(result); document.getElementById(result_form).innerHTML = "name: "+result.name+"<br>phone: "+result.phonenumber; }, error: function(response) { document.getElementById(result_form).innerHTML = "Error"; } }); } </script> 

Controller / Common:

 <?php defined('SYSPATH') or die('No direct script access.'); abstract class Controller_Common extends Controller_Template { public $template = 'main'; public function before() { parent::before(); View::set_global('title', 'My Site'); View::set_global('description', 'My Site'); $this->template->content = ''; $this->template->styles = array('main'); $this->template->scripts = ''; } } // End Common 

Main Template View:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo $title; ?></title> <meta name="description" content="<?php echo $description; ?>" /> <?php foreach($styles as $style): ?> <link href="<?php echo URL::base(); ?>public/css/<?php echo $style; ?>.css" rel="stylesheet" type="text/css" /> <?php endforeach; ?> </head> <body> <div class="layer"> <div class="container"> <div class="header"><h1>Logo</h1></div> <div class="left"> <h3>Menu</h3> <br /> <ul> <li><a href="<?php echo URL::site(); ?>">Home</a></li> <li><a href="<?php echo URL::site('about'); ?>">about</a></li> <li><a href="<?php echo URL::site('reg'); ?>">Reg</a></li> <li><a href="<?php echo URL::site('contacts'); ?>">contacts</a></li> <li><a href="<?php echo URL::site('articles'); ?>">Articles</a></li> </ul> <li><a href="<?php echo URL::site('articles/article'); ?>">Article1</a></li> <li><a href="<?php echo URL::site('articles/article1'); ?>">Article2</a></li></ul> </div> <div class="content"><?php echo $content; ?></div> <div class="clearing"></div> <div class="footer">2011 All rights reserved</div> </div> </div> </body> </html> 

Console.log:

 Array ( [name] => 1 [phonenumber] => 1 ) {"name":"1","phonenumber":"1"}<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My site</title> <meta name="description" content="My site" /> <link href="/public/css/main.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="layer"> <div class="container"> <div class="header"><h1>Logo</h1></div> <div class="left"> <h3>Menu</h3> <br /> <ul> <li><a href="/">Home</a></li> <li><a href="/about">about</a></li> <li><a href="/reg">Reg</a></li> <li><a href="/contacts">contacts</a></li> <li><a href="/articles">articles</a></li> </ul> <li><a href="/articles/article">article1</a></li> <li><a href="/articles/article1">article2</a></li></ul> </div> <div class="content"></div> <div class="clearing"></div> <div class="footer">2011 All rights reserved</div> </div> </div> </body> </html> 

    1 answer 1

    You have the main pattern in the output. Try after the output of the answer json put exit; And do not forget print_r.

    • Tried it? What happened ? - croll
    • thanks, partially corrected, the template ceased to be transmitted, the data seems to be transmitted, but still writes undefined - rtv
    • I fixed everything, I had to uncomment result = jQuery.parseJSON(response); instead of result = response; - rtv