I have some of wiredness going on in some code I am writing. I have a regular form to update a user account. The fields are populated with data from the database. after changing that needs to be changed, I can't submit the form. when I click on the button, it behaves like disabled submit with javascript but I didn't. On the otherhand if I use javascript and stop it from submitting and console log to see if a click is happening, it appears the button is being clicked but just nothing. below is my code in my view for the form.
form_open('members/users/update_curr_user');
$data5 = array('name'=>'username','id'=>'username','value'=>$uservar['username']);
echo 'Username :'.form_input($data5);
$data6 = array('name'=>'email','id'=>'email','value'=>$uservar['email']);
echo 'Email Address :'.form_input($data6);
$phone1 = array('name'=>'phone','id'=>'phone','value'=>$uservar['phone']);
echo 'Your phone number formatted like so: 0802-331-5544'.form_input($phone1);
switch ($uservar['active']):
case 0:
$data7 = array(
'name'=>'status',
'id'=> 'status',
'value' =>'Deactivated'
);
echo 'Status : Active or Deactivated'.form_input($data7);
break;
case 1:
$data8 = array(
'name' =>'status',
'id' =>'status',
'value'=>'active'
);
echo 'Status :Active or Deactivated'.form_input($data8);
break;
endswitch;
$group1 = array('name'=>'group','id'=>'group','value'=>$uservar['group']);
echo 'Group :'.form_dropdown('group',$groups).'<br />';
echo '<br /><br />';
//$data9 = 'id="updateuser"';
//echo form_submit('submit','Update User',$data9);
?>
<input name="submit" id="updateuser" type="submit" value="Update User" />
<?php echo form_close();?>
Because of how unsure I was of what was going on I manually created a button still the same. Other forms on the page are working ok. If it's of any consequence, I am using phil sturgeon's template library, ion_auth and firephp.
You're not actually writing out the form tag. You need to put an echo up there. Do this:
echo form_open('members/users/update_curr_user');
Your submit button should now work.
Related
I've got a form with two submit buttons which I want to test using Selenium.
View:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'profile-form',
'enableAjaxValidation' => true,
'action' => '',
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => true,
'validateOnType' => false,
),
));
?>
<input name="cancel" type="submit" value="Cancel" />
<input name="submit" type="submit" value="Save changes" />
<?php $this->endWidget(); ?>
Controller is nothing special, you can assume it just prints "Your profile has been saved" or "Your profile was not saved" depending on what $_POST['cancel'] it gets
Test code:
<?php
$this->open('/profile_form_url');
$submit_button_selector = 'css=#profile-form input[name="submit"]';
$cancel_button_selector = 'css=#profile-form input[name="cancel"]';
$this->clickAndWait($cancel_button_selector);
$this->assertTextPresent('Your profile was not saved');
$this->open('/profile_form_url');
$this->clickAndWait($submit_button_selector);
$this->assertTextPresent('Your profile has been saved');
The problem is that code works great in browser but not when running tests in Selenium/Firefox. When running tests, it "sees" the first button only (Cancel), clicking "Save changes" has the same effect. If you place Save changes button first, it will not "see" Cancel button.
If you turn enableAjaxValidation off, it works both in browser and Selenium, but I'd like to have a more elegant solution of course. Like for example turning off the AJAX validation on clicking on Cancel.
No, the problem doesn't depend on which locator you use for buttons (xpath, css, id).
clickAndWait() calls waitForPageToLoad() - ajax form validation will generally not trigger this event (unless a page loads), so your test will never complete; this is probably why if you turn ajax validation off it works.
You might want to look at the other options selenium provides (this is an old link to the free phpunit pocket guide that describes some other options - it's based on phpunit 3.1 though) such as using click() and then using waitForCondition() with some javascript to run and return true if the new text is displayed.
Refer to my code below, when user click on en button, the content will be changed to English, while clicking tw button, the content will be changed to Chinese.
However, the page will be refreshed each time when user click either en or tw button. I want to ask how can I implement AJAX content update in this case?
The result is when user click either en or tw button, the page won't be refreshed to change the content language.
Thanks
I have refer to Yii docs here, but seem that it is not appropriate for my case
C:\wamp\www\website\protected\views\site\index.php
<?php
$lang = isset($_GET["lang"]) ? $_GET["lang"] : "en_uk";
$lang = $lang == "en" ? "en_uk" : "zh_tw";
Yii::app()->setLanguage($lang);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<input type="submit" value="en" name="lang" />
<input type="submit" value="tw" name="lang" />
</form>
<div class="main">
<?php echo Yii::t(Yii::app()->controller->id, "Causeway Bay"); ?>
</div>
Best practice is to reload the page in these cases, because usually you have to update so much, that it is just not worth it.
That said, CHtml's ajaxSubmitButton is the cleanest way to implement this, because you can map every event of your call very easily. It looks something like this:
<?php
echo CHtml::ajaxSubmitButton('en', CHtml::normalizeUrl(array('site/changeLanguage')),
array(
'error'=>'js:function(){
alert("error");
}',
//if you add a return false in this, it will not submit.
'beforeSend'=>'js:function(){
alert("beforeSend");
}',
'success'=>'js:function(data){
alert("success, data from server: "+data);
}',
'complete'=>'js:function(){
alert("complete");
}',
//'update'=>'#where_to_put_the_response',
)
);
?>
You don't have to use every parameter of course. The update parameter can update a HTML tag instantly.
EDIT:
This can be done easily if you use the controller's renderPartial method, for instance in your site controller if you have the action responsible for the index.
public function actionIndex(){
//get variables, etc
if(Yii::app()->request->isAjaxRequest) {
$lang = $_POST['nameOfSubmit'];
}else {
//...
}
//if the 3rd parameter is true, the method returns the generated HTML to a variable
$page = $this->renderPartial('_page', array(/*parameters*/ ), true);
echo $page;
}
And then, in your view file you can simply have
<?php echo CHtml::ajaxSubmitButton('en', CHtml::normalizeUrl(array('site/index')),
array('update'=>'#content_div',));?>
and
<?php echo CHtml::ajaxSubmitButton('tw', CHtml::normalizeUrl(array('site/index')),
array('update'=>'#content_div',));?>
I am trying to implement recaptcha my form in CodeIgniter (without using the recaptcha library). This works fine, however as in my form, I am displaying each fields errors individually, I want to display recaptcha error next to its place in the form, can someone help me how can i do it?
The code from my controller:
$this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[3]|max_length[25]');
$this->form_validation->set_rules('email', 'Email address', 'trim|required|valid_email');
//With above set_rules i'm able to display each fields errors next to it,
//How can i display following recaptcha error next to it.
if (!$resp->is_valid)
{
//reCAPTCHA was entered incorrectly
die (
"The reCAPTCHA wasn entered incorrectly." .
"(reCAPTCHA said: " . $resp->error . ")
");
}
else
{
//Successful verification
die('Success!');
}
Thanks for any help.
If you aren't running the recaptcha field through the form validation library you won't be able to use the form_error() function, but you can easily send the error to your view as a variable:
if ($this->form_validation->run())
{
if ( ! $resp->is_valid)
{
$data['recaptcha_error'] = "reCAPTCHA said: ".$resp->error;
}
else
{
// Process form
}
}
$this->load->view('my_view', $data);
Then in your view (wherever/however you want):
<?php if (isset($recaptcha_error)): ?>
<label class="error">
<?php echo $recaptcha_error; ?>
</label>
<?php endif; ?>
I created a confirmation page for deleting an item. This works perfectly.
Now I want to appear the confirmation page in fancybox, still passing all the variables and delete the data or close the dialog.
It's important that the process still works as it does now (delete->confirmationpage->redirect/deletion) so that users with javascript disabled can perform these actions without a problem.
Now have I been reading about zend framework,ajax, json and more, but the more I read, the less I understand.
So in a nutshell, my question:
I want to pass a variable to the fancybox and if 'yes' perform the delete action or on 'no' close the dialog. This within the zend framework and jquery.
Any advice or tips are greatly appreciated!
You need to use content switching in your ajax which will then render your action appropriately, for example:
function confirmDeleteAction() {
if($this->_request->isXmlHttpRequest()) {
//This is ajax so we want to disable the layout
$this->_helper->layout->disableLayout();
//Think about whether you want to use a different view here using: viewRenderer('yourview')
}
//The normal code can go here
}
function deleteAction() {
//You probably don't want to show anything here, just do the delete logic and redirect therefore you don't need to worry where it's coming from
}
And in your fancybox, have a view that has a form and two buttons, the form should point to your delete action with the id of whatever your deleting as a get param. Set some javascript up that says something like (and this is mootools code, but you can convert it easily):
$('confirmButton').addEvent('click', function(e) {
e.preventDefault();
this.getParent('form').submit();
}
$('cancelButton').addEvent('click', function(e) {
e.preventDefault();
$('fancyBox').destroy(); //I'm not sure it has an id of fancyBox, never used it
}
Came across the question today and figured I could give it a fresh look. For Zend Framework the solution is really simple:
This is how the action looks:
public function deleteAction()
{
$this->_helper->layout()->disableLayout();
$this->view->news = $this->newsService->GetNews($this->_getParam('id'));
if($this->getRequest()->isPost())
{
if($this->getRequest()->getPost('delete') == 'Yes')
{
$this->newsService->DeleteNews($this->_getParam('id'), $this->view->user->username, $this->view->translate('deleted: ').'<strong>'.$this->view->pages[0]['title'].'</strong>');
$this->_helper->flashMessenger(array('message' => $this->view->translate('The page is deleted'), 'status' => 'success'));
$this->_helper->redirectToIndex();
}
elseif($this->getRequest()->getPost('delete') == 'No')
{
$this->_helper->flashMessenger(array('message' => $this->view->translate('The page is <u>not</u> deleted'), 'status' => 'notice'));
$this->_helper->redirectToIndex();
}
}
}
The delete.phtml
<div>
<h2><?php echo $this->translate('Delete news'); ?></h2>
<?php
foreach($this->news as $news)
{
?>
<p>
<?php echo $this->translate('You are now deleting <strong>\'').$news['title'].$this->translate('\'</strong>. Are you sure about this?'); ?>
</p>
<p>
<?php echo $this->translate('<strong>Note! </strong>This action is inreversable, even for us!'); ?>
</p>
<form action="<?php echo $this->baseUrl(false).'/news/index/delete/'.$news['id']; ?>" method="post">
<?php
}
?>
<input class="submit deleteYes" type="submit" name="delete" value="<?php echo $this->translate('Yes'); ?>" />
<input class="submit deleteNo" type="submit" name="delete" value="<?php echo $this->translate('No'); ?>" />
</form>
And this is how the link to delete a file looks (within a foreach loop with my database results)
<a class="deleteConfirmation" href="<?php echo $this->baseUrl(false).'/news/index/delete/'.$news->id; ?>">delete</a>
This works like you would expect; when you click on delete the user goes to the delete confirmation page and redirects the user back to the index after the form is submitted. But I wanted the confirmation in a dialog (in my case I use fancybox). To achieve this, add the following jquery to your index:
$('.deleteConfirmation').fancybox({
// Normal fancybox parameters
ajax : {
type : "POST"
}
});
I understand how to do it w/ a plain form w/o existing values, but let's say I have a view that I can call via http://domain.com/account/settings. let's say I have two fields, username, password and city, which are all pulled from the DB (except for password of course). So, if a user tries to submit the form and fails validation for whatever reason, where should I "redirect" them to? Right now, I have it showing the same view but the problem is, it pulls the info from the DB again. Should I be creating two different views?
The second view would essentially show the information they tried to enter along w/ the error message.
You do not need two separate views. Check out Form Helper's functions set_value(), set_select(), set_checkbox() and set_radio(). These re-populate form after its submission and validation. So in your case, you should specify the fields this way:
<input type="text"
name="username"
value="<?php echo set_value('username', $user['username']); ?>" />
<input type="text"
name="city"
value="<?php echo set_value('city', $user['city']); ?>" />
By default, the input will have $user['city'] value. But after failed validation it will be re-populated with previously entered values (including incorrect ones).
Just remember that all fields you want to re-populate need to be passed through form_validation library:
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('city', 'City', '');
On the same controller you could have something like this:
if ($this->form_validation->run('create_comment') === TRUE)
{
$this->comments_model->name = $this->input->post('name', TRUE);
$this->comments_model->email = $this->input->post('email', TRUE);
$this->comments_model->website = $this->input->post('website', TRUE);
$this->comments_model->comment = $this->input->post('comment', TRUE);
$this->comments_model->create_comment();
redirect(current_url());
}
$this->load->view('your_view');
That's all there is to it.
The idea is to have it redirect to itself or wherever you want, when the validation returns 'true' so that we kind of refresh the page, hence, update the page.
If the validation returns 'false' then you won't have to do anything.
Redirect to the same form.
And in your view give error information to the visitor.
There are two ways you can do this.
Use this error in your view. This will show validation error info.
echo validation_errors('<p class="error">','</p>');
Or you can use flashdata()
In your controller
...
...
$this->session->set_flashdata('msg', 'All fields are required. or other useful info here. Please try again!');
redirect('yourcontroller');
And in your view, you need to show it.
<?php
if ($this->session->flashdata('msg')){ //change!
echo "<div class='message'>";
echo $this->session->flashdata('msg');
echo "</div>";
}
?>
Had the same problem and discovered that a redirection makes you lose the data that would have been provided by form_error(...) or validation_errors(), except you store such data in a session or in an array being passed into the loaded view.
The point to note is that you should redirect only if the data you want passed around is in session, else you should just load a view. The latter ensures that you have your validation errors intact when you reach the loaded view.
Just load same view if form validation failed
controller
$userData=array(
'username'=NULL,
'password'=NULL
);
#set form validation rules
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
#get all posted data ,this helps you in two ways,
# 1. Get all form data and can be use here server side
# 2. repopulating the form data by passing to view page
$userData=$this->input->post(NULL, TRUE);
#check form validation result
if ($this->form_validation->run() == TRUE) {
//do the operation
redirect(URL);
}else{
$this->load->view($view, $userData);
}
View page
<form method=post action='URL'>
<input type='text' name='username'value='<?php echo $username?>'/>
<?php echo (form_error('username')) ? form_error('username', "<div style='color:red'>", "</div>") : ""; ?>
<input type='text' name='password' value='<?php echo $password?>'/>
<?php echo (form_error('username')) ? form_error('username', "<div style='color:red'>", "</div>") : ""; ?>
<input type='submit' value='submit'/>
</form>
This code display form errors and repopulate the form