I'm working on a AJAX sign up form which will be submitted to the Zend Framework. Right now, the form's action doesn't seem to be executing. The response text is a duplicate of the current HTML document. Can someone please inform me of what I'm doing wrong, and what the correct URL should be? I've set the project up using modules. For example, should the correct url from the example below be: '/account/register/auth'
#application.ini
resources.router.routes.register.route = /register
resources.router.routes.register.defaults.module = account
resources.router.routes.register.defaults.controller = register
resources.router.routes.register.defaults.action = index
#application/modules/accout/controllers/RegisterController.php
<?php
class Account_RegisterController extends Zend_Controller_Action{
public function init(){
$this->view->register = new Account_Form_Register();
}
public function indexAction(){
}
public function authAction(){
$request = $this->getRequest();
$form = new Account_Form_Register();
if($request->isPost()){
print('submit');
}else{
print('nothing yet');
}
}
}
?>
#application/modules/account/views/scripts/register/index.phtml
<div class="register">
<form id="registerForm">
<h2 class="formHeader">Register</h2>
<div class="floatLeft">
<?php echo $this->register->fName; ?>
</div>
<div class="remaining">
<?php echo $this->register->lName; ?>
</div>
<div class="floatLeft">
<?php echo $this->register->uEmail; ?>
</div>
<div class="remaining">
<?php echo $this->register->aEmail; ?>
</div>
<div class="">
<?php echo $this->register->phone; ?>
</div>
<div class="floatLeft">
<?php echo $this->register->oPassword; ?>
</div>
<div class="remaining">
<?php echo $this->register->cPassword; ?>
</div>
<div id="formButton">
<?php echo $this->register->submit; ?>
</div>
</form>
</div>
As I said, maybe you should disable the view processing when doing AJAX action in order to return a json_encode($some_data) instead of HTML content.
Try
$this->_helper->viewRenderer->setNoRender(true);
Take a look at this article
Related
I have a controller named : user_login_controller.php and view : user_login_view.php
code of user_login_view.php
<?php echo form_open('user_login_controller/login', 'class="form-horizontal" id="userloginform"');?>
<fieldset>
<legend>Student Login</legend>
<div class="form-group">
<div class="col-lg-6 col-md-6 col-xs-10">
<?php echo form_input(['name'=>'rno','class'=>'form-control','placeholder'=>'Roll Number'])?>
</div>
</div>
<div class="form-group">
<div class="col-lg-6 col-md-6 col-xs-10">
<?php echo form_password(['name'=>'pwd','class'=>'form-control','placeholder'=>'Password'])?>
</div>
</div>
<div class="col-lg-6 col-md-6 col-lg-offset-2 col-md-offset-2">
<?php echo form_reset(['name'=>'Reset','value'=>'Cancel','class'=>'btn btn-primary'])?>
<?php echo form_submit(['name'=>'Submit','value'=>'Login','class'=>'btn btn-primary'])?>
</div>
</div>
</fieldset>
</form>
On submit button click I m sending control to user_login_controller/login
here is user_login_controller.php
<?php
class User_login_controller extends MY_Controller
{
public function index()
{
$this->load->view('user/user_login_view');
}
public function login()
{
echo "User login function";
}
}
?>
but it's given me 404 error.however i have both files
and when I m going through url(http://localhost:8090/project/user_login_controller/login) :then its working. and i have loaded all the necessary helpers.
$autoload['helper'] = array('url','form');
what to do now?
You should set config['base_url'] in application/config/config.php
$config['base_url'] = 'http://localhost:8090/project/';
as form_open consider url to be set to http://localhost:80/project/ or http://[::1]/project/ if you didn't set config['base_url']
Follow below steps and it will sort-out the problem,
1. View
when you opening a form tag, if you want to add several attributes, use this method.
<?php
$attributes = array('class' => 'form-horizontal', 'id' => 'userloginform');
echo form_open('user_login_controller/login', $attributes);
?>
It's clean and error less.
2. Config.php
Go to application/config/config.php file, and set
$config['base_url'] = 'http://localhost:8090/your_project_folder_name/';
3. routes.php
Go to application/config/routes.php file, and set
$route['user_login_controller/(:any)'] = "user_login_controller/$1";
$route['user_login_controller'] = "user_login_controller";
This will work. Try it and let me know.
I'm writing a CodeIgniter 3 application. My goal is to have a view, which is constantly (as the code goes along) flushed with the output content. I read some answers in stackoverflow, but I am not sure, how to do this.
I have a Controller wich renders the view, in the update method.
<?php
defined('BASEPATH') OR exit('No direct script access allowed!');
class Dataupdate extends MY_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$this->render('dataupdate_list_view');
}
public function update() {
$this->render('dataupdate_update_view');
}
}
?>
Here is the class "MY_Controller".
<?php
defined('BASEPATH') OR exit('No direct script access allowed!');
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
$this->data['page_title'] = 'Team Portal';
$this->data['before_head'] = '';
$this->data['before_body'] = '';
}
/**
* Render method is used to render a view using a template.
* The given template delivers the HTML header and footer.
* The view contains the actual page content.
*
* #param string $the_view The view to be rendered
* #param string $template The template to render the view
*/
protected function render($the_view = NULL, $template = 'master') {
if ($template == 'json' || $this->input->is_ajax_request()) {
header('Content-Type: application/json');
echo json_encode($this->data);
} else {
// Get current user from database
$this->load->model('user_model');
$user = $this->user_model->get_record_by_name($_SERVER['REMOTE_USER']);
// Data to pass to view
$this->data['the_view_content'] = (is_null($the_view)) ? '' : $this->load->view($the_view, $this->data, TRUE);
$this->data['user'] = $user;
// Load view with data
$this->load->view('templates/' . $template . '_view', $this->data);
}
}
}
?>
Then I have a view, which outputs the content.
<div>
<!-- PAGE TITLE -->
<div class="page-title">
<h3>Daten Update</h3>
<div class="title_right">
</div>
</div>
<div class="clearfix"></div>
<!-- ALERTS -->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Fehler<small>Kleiner Text</small></h2>
<div class="clearfix"></div>
</div>
<div class="x_content bs-example-popovers">
<div class="alert alert-danger alert-dismissible fade in" role="alert">
<strong>Strong text.</strong>What to do now?
</div>
</div>
</div>
</div>
</div>
<!-- CONTENT -->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Neues Daten Update ausführen</h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<?php echo form_open('dataupdate/update'); ?>
<input type="text" name="test" />
<button>Ausführen</button>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>
This works fine. So far.
Now, what I try to achieve is the following:
I click on the "Ausführen" (Execute) button, which submits the form to the same url as this page
Then I want a php script to execute, which continously outputs content to the page. Not all content at once, but adds output after output to the page.
I hope I made myself clear.
Any suggestions or tutorials, on how to do that?
You'll need to force the output by calling ->_display method on the output class
For example for JSON output you'll do something like this:
$response = array('status' => 'OK');
$this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
->_display();
exit;
Same thing goes when you load a view, if you're 100% sure you won't need anything else, You may call _display manually to get the output; just be sure to call exit after that otherwise you'll end-up with a duplicated output (one from your manual call & the other from the automatic call).
Have a look at the user's guide for output class: http://www.codeigniter.com/user_guide/libraries/output.html
I am new in Yii framework. I am using Chtml form to send data from view to controller and want to print the result and insert it to database. But in my case its showing an empty array. I couldn't able to understand where I am doing wrong. So please help me regarding this.
in controller
public function actionTest1()
{
$obj = new Pad;
if(isset($_POST))
print_r($_POST);// to check the desired result
if(isset($_POST['Pad']))
{
$obj->attributes=$_POST['Pad'];
if($obj->save()) // insert the data
$this->redirect(array('view','id'=>$obj->id));
}
}
in view
<?php echo CHtml::beginForm('','post',array('id'=>'step1Form')); ?>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'Stop Pad after goal is reached'));
?>
</div>
<div class="currencyText padTop4 floatLeft">Stop Pad after goal is reached</div>
<div class="clearBoth"></div>
</div>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'No limites'));
?>
</div>
<div class="currencyText padTop4 floatLeft">No limites</div>
<div class="clearBoth"></div>
</div>
<div class="nextText">
<?php echo CHtml::link('Next >',array('pad/test1'));?>
</div>
<?php echo CHtml::endForm(); ?>
Please help. Thanks in advance!
You are not submitting your form and of course $_POST will be empty. Line below is just a link:
<?php echo CHtml::link('Next >',array('pad/test1'));?>
You must change it to :
<?php echo CHtml::submitButton('Next >');?>
And also add an action to your form.
Updating the question with the changes I have done. Its redirect me to pad/test1 showing array() as result.
in controller
public function actionTest1()
{
$obj = new Pad;
if(isset($_POST))
CVarDumper::dump($_POST);// to check the desired result
if(isset($_POST['Pad']))
{
$obj->attributes=$_POST['Pad'];
if($obj->save()) // insert the data
echo "=====";
}
}
in view
<?php echo CHtml::beginForm( '','post'); ?>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'Stop Pad after goal is reached'));
?>
</div>
<div class="currencyText padTop4 floatLeft">Stop Pad after goal is reached</div>
<div class="clearBoth"></div>
</div>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'No limites'));
?>
</div>
<div class="currencyText padTop4 floatLeft">No limites</div>
<div class="clearBoth"></div>
</div>
<div class="nextText">
<?php echo CHtml::button('Next >', array('submit' => array('pad/test1'))); ?>
</div>
<?php echo CHtml::endForm(); ?>
A common source of confusion among new Yii users is how the 'safe' validator works, how it works with other validators, and why it's necessary in the first place. This article means to clear up this confusion, as well as explain the notion of Massive Assignment.
http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/
I have a single page website, I have the insert working correctly, but I cannot seem to update the #results div after submit. The values are being inserted into the database just fine and if I hard refresh they are appearing. But not with the jQuery AJAX refresh. I am new to Ajax, so any help would be really appreciated, and any comments on code structure or bad habits, please comment as I am trying to learn the proper way to program.
Here is my code:
$(document).ready(function(){
$("form#addTodo").submit(function(){
alert('hello world'); // ensure function is running
var post_title = $('input#post_title').val(); // take values from inputs
var post_content = $('input#post_content').val();
$.ajax({
type: "POST",
url: "add.php",
data: "post_title=" + post_title + "&post_content=" + post_content,
success: function() {
// alert('success'); // ensure success
$('#results').fadeOut('slow').load('include/results.php').fadeIn('slow');
}
});
$('input#post_title').val(''); // clear form fields
$('input#post_content').val('');
return false;
});
});
Here is the results.php file:
<?php
$sql = $db->query('SELECT id, post_title, post_content FROM posts ORDER BY post_title ASC');
$results = $sql->fetchALL(PDO::FETCH_OBJ);
$count_posts = count($results);
?>
<?php if ( have_posts() == true) : ?>
<div class="accordion" id="accordion_main">
<?php foreach($results as $entry): ?>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion_main" href="#collapse-<?php echo $entry->id; ?>">
<?php echo $entry->post_title; ?>
</a>
</div>
<div id="collapse-<?php echo $entry->id; ?>" class="accordion-body collapse">
<div class="accordion-inner">
<p class="target"><?php echo $entry->post_content; ?></p>
<p>Edit Delete</p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php else: ?>
<h3>There are no posts to display at this time.</h3>
<?php endif; ?>
This works fine when it is included by the index.php, I just can't get it to refresh after posting the data to the DB.
<?php
require_once 'includes/db.php';
require_once 'includes/functions.php';
?>
<?php get_header(); ?>
<div id="results">
<?php include_once 'includes/results.php'; ?>
</div>
<hr />
<p>Add New Post</p>
<form id="addTodo" action="">
<div>
<label for="post_title">Post Title</label>
<input id="post_title" name="post_title">
</div>
<div>
<label for="post_content">Post Content</label>
<input id="post_content" name="post_content">
</div>
<div>
<button id="submit" type="submit">
Save
</button>
</div>
</form>
<?php get_footer(); ?>
I should mention that I haven't put in form validation yet. Any help is greatly appreciated, thanks.
Why not just return the data from success? and use that to make your html input
success:function(returnedData) {
if (returnedData.status = 'successful') {
//use javascript to append the data for example like
//returnedData.message to make html and insert into your current html
}
}
Note: returnedData has to be pushed out by your php in an json format.
you can find a similar answer to your request if you read this thread that I had recently answered : Jquery Mobile Post to PHP (same page)
But you must make some adaptation for you context ;-) !!
I think that is the unique solution if you're using unique file script
To insert data returned from php file just use $("element id or class").html() function to insert the data or html to specific area of your document
I have a main view with a menu which helps me display another view. It's similar to this:
<div id="page">
<div id="menu">
Page1
Page2
</div>
<div id="content">
<!-- Page1 or Page2 are displayed here -->
</div>
</div>
I'm using php's Yii framework. Which makes me not to use <?php include("menuview.php"); ?>. So I'm looking for a different solution. I can do this with Ajax, but I would also like the link to change to mypage/controller/Page2. With Ajax I can only get it to this: mypage/controller/index#Page2
in main view, instead of include do
<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>
UPDATE:
protected/views/controller/page1.php and protected/views/controller/page2.php content at your liking
protected/views/layouts/custom.php:
<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
<div id="menu">
<?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
<?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
</div>
<div id="content">
<?php echo $content; ?>
</div>
</div>
<?php $this->endContent(); ?>
protected/controllers/ControllerController.php:
class ControllerController extends Controller {
/**
* #var string the default layout for the views.
*/
public $layout = '//layouts/custom';
public function actionPage1() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page1');
else
$this->render('page1');
}
public function actionPage2() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page2');
else
$this->render('page2');
}
}
UPDATE2:
If you need the link in address bar to change too then your only option is to use regular link and not ajax <?php echo CHtml::link('Page1', array('controller/page1')); ?>
using ajax the preferred way is using hash like you mentioned.