I have a page with a form for creating users. A user has an hobby, which can be created on the same page by clicking on the second button which opens the page for creating a hobby. After creating the hobby, the previous user form should be shown with the user input inserted before going to the hobby page.
Is there a way to do something like with typo3 flow / fluid without using AJAX?
I tried to submit the input to a different action by clicking on the createHobby button --> The action redirects to the new hobby page, where the user can create the hobby and after creation it should redirect back to the user form with the already filled out input fields by the user .
I used...
<input type='submit' value='Create' formaction='/hobby/create' />`
to achive this, but it seems there are some problems with the uris... I get following error:
#1301610453: Could not resolve a route and its corresponding URI for the given parameters.
I think the using the attribute formaction is not a good solution for every case, as it is not supported by IE < 10 as you can see here. I think a JavaScript backport should also be considered (dynamically change the action attribute of the form when clicking on the second button, before actually submitting the form).
Concerning your error, you should not – and probably never – use direct HTML input, instead try to focus on Fluid ViewHelpers, which allow TYPO3 to create the correct HTML input.
Try this instead:
<f:form.submit value="Create" additionalAttributes="{formaction: '{f:uri.action(controller: \'hobby\', action: \'create\')}'}" />
You can make an $this->forward(...) in an initializeActiondepending on an param of your action.
Lets imagine your default Form action is "create". So you need an initializeCreateAction:
public function initializeCreateAction()
{
if ($this->arguments->hasArgument('createHobby')) {
$createHobby = $this->request->getArgument('createHobby');
if ($createHobby) {
$this->forward('create', 'Hobby', NULL, $this->request->getArguments());
}
}
}
Now you must name your input createHobby and assign your createAction this param:
In fluid:
<f:form.button type="submit" name="createHobby" value="1">Create Hobby</f:form.button>
In your Controller:
public function createAction($formData, $createHobby = false)
{
...
}
can you explain something more ... what you show has nothing to do with typo3, I don't know where you inserted that, what version of typo3 , using any extension extra ?
Related
I have a custom plugin that uses a class. In the class I add an admin dashboard menu item that displays a "Settings" form. Let's say the class looks like this:
class MyPlugin {
function __construct () { ... }
function show_menu_form () {
$url = admin_url("admin_post
echo <<<EOFORM
<form method="POST" action="$url"/>
Click submit to do stuff.
<input type="hidden" name="action" value="do_stuff"/>
<input type="submit/>
</form>
EOFORM;
add_action('wp_post_do_stuff', [$this, '_handle_stuff']);
}
function _handle_stuff () {
echo "Doing stuff...";
}
}
The form prints properly in the dashboard page. When I click submit, it goes to a completely blank screen. What I want it to do is:
Do something
Go back to the same page, showing a message like, "Stuff was done." above the same form, and the user can click the form submit again to do more stuff.
If I leave the action blank on the <form> tag it will go back to the same page, and I can manually process the get/post params myself... that seems like a bad solution (but the only one I can see so far).
Any tips?
You have 2 ways to do this:
Form submit to the same page and before rendering the form you check if the form already submitted. If yes then call a function to display "Stuff was done" and any further steps. If not, then display the form.
In this way, you won't need any javascript in your plugin and I IMOO will be simpler and straight forward.
Submit using AJAX and display the confirm message using javascript. You can read the docs AJAX in Plugins to see how to do this in the admin menu. This is might be a more elegant solution without requiring any page refresh.
Which one to use depends on the requirements of your plugin. Either way, don't forget to validate inputs
I'm using Codeigniter and wants to know how I can make a checkbox that submits the form on click?
Secondly, this checkbox will be one of several checkboxes that will act as a filter like products > $20, products < $30, how do i pass it in the url? I'm thinking /1+2+3
Haven't worked with Codeigniter much, but I can answer how to make the form submit on checking the checkbox with JS:
<form id="something">
<input type="checkbox" name="foo" id="foo" value="yes" />
</form>
<script type="text/javascript">
$("#foo").click(function() {
if ($(this).is(":checked"))
$("#something").submit();
});
</script>
The javascript questions seem to have been solved already; let's step to the codeigniter ones.
You can pass the url in one of those two ways.
Idiomatic but limited: as /1/2/3/4/etc. The controller function handling that url could both use func_get_args to read them or, if you already know how many parameters will be passed at the most, give a default value of null to all non-necessary paramenters;
Not Codeigniterish but seriously better for search parameters: enable the query strings on your config file, pass arguments as you would normally with GET (min=2&max=3 and so on) and get their value with CI's input class ($min = $this->input->get('min')).
This has nothing to do with PHP, nor CodeIgniter. The solution is to submit the form in the onclick event of the element.
<input type="checkbox" name="filterx" onclick="document.forms[0].submit()" />
You can use the OnSubmit event of the form to nicely format the url, if you like.
To do this, you can
get the values of all desired elements,
build a nice url from it,
set the url using location.href = yourniceurl,
cancel the regular submit by returning false.
Note that both solutions require javascript to be enabled. So it is a good thing to have other means of submitting the form (submit button). Don't rely on submitting by pressing Enter. Opera will use the Enter key for toggling the checkbox instead of submitting the form.
If you like, you can hide the submit button using Javascript, that way, users having Javascript will have their form auto-submitted, while users without can use the button.
You will need to make sure that your server side form validator not only accepts the nice url, but the ugly url (which posts values like ?filterx=on) too.
I'm trying to build a multi step/page form in PHP and CodeIgniter and I was wondering if any of you could help me.
How can I have a multi step form in CI that updates rather than inserts again when you return to the previous step with the back button? How can I have a form that doesn't have those back button POST form resend messages?
Edit: without JS if possible
Thanks!
Here's my answer from another question. It gives you forward/backward ability without the chance to lose data, instantly jumps between pages, is EASY to code, needs no sessions, and is framework-independent (can be used in any situation):
I develop a product for the Psychology market that does 250 question psychological based testing. To make a test that isn't completely overwhelming, I break the form up into 25 question segments while outputting it in a loop via div tags with a sequential ID appended (ie. div1, div2, div3) Each div is set to display:none but the first.
I then provide the user with a button that toggles the current div + 1 (ie if on div 1, it would do a $(#div2).show() etc. Back buttons do the opposite.
The important part is that the form covers ALL divs. Then its just a matter of swapping out the forward/back button at the end with a submit button.
Voila! Yes, low-tech. But FAST....and no chance to EVER lose values going forward or backward.
So, a rough truncated example:
<form>
<div id="div1">
First 25 Questions
<input type="button">shows next div</input>
</div>
<div id="div2" style="display:none">
Second 25 Questions
<input type="submit">Submit Form</input>
</div>
</form>
Create a unique ID which you use in all steps of your wizard. Save that ID to the database upon the initial saving of your form.
Forward this ID to the next steps using a input type="hidden".
When saving a step, first try to match the ID and, if you find it int the database, perform an update instead of an insert.
To avoid the "do you want to resend post data", perform each wizard step in two CodeIgniter controller actions:
SaveStep5(POST: form instance ID + other "wizards step 5" inputs):
looks up the form instance ID in the database and performs insert/update commands;
redirects to LoadStep6 and passes the form instance ID in a GET parameter;
LoadStep6(GET: form instance ID);
looks up the form instance in the database,
if the instance is not found: error handling
if the instance is found, renders the input form for "step 6"
If you want to avoid those messages which warn the user about resending posts, and you also want to have multiple proper pages rather than just different steps in javascript, you can put the answers in the URL as GET parameters..
so after the first form submission, you will get form2.php? in the URL.. you can add those answers as hidden variables in form2 and so on.
It is not a very elegant solution though. I'd recommend you to use javascript: add a custom handler for form submission and submit form content via ajax, and then load the next form on ajax complete.
Also, like the other person answered, on the server end, you will need a unique ID which fetches/updates the submission data in the database.
I like the approach of waiting on the database update until all steps have been completed. You could store all the data in the intermediate steps in a session. I suppose you could even save the model object you're using (if you're using one) in a session and after all steps have been completed you can do the database insert.
I have a model to store my wizard data with a variable for each field on the form:
class Class_signup_data extends CI_Model {
const table_name="signups_in_progress";
public $market_segment; // there is a field named 'market_segment' in the wizard view
...
...
I have one controller to handle the whole process, with parameters for the session_id and the stage of the process we are at:
class Signup extends CI_Controller {
public function in_progress($session_id=NULL,$stage=1) {
$this->index($session_id,$stage);
}
public function index($session_id=NULL,$stage=1) {
if ($session_id===NULL) $session_id=$this->session->userdata('session_id');
...
...
In this controller I have a switch for which stage we are at - it looks for a 'Prev' button first:
switch ($stage) {
case 2:
if ($this->input->post('prev')) { // if they click Previuous, the validations DON'T need to be met:
$signup_data->save_to_db(array_merge(array('ip'=>$_SERVER['REMOTE_ADDR'],'session_id'=>$session_id,'signup_stage' => '1',
'signup_complete' =>'0'),$this->input->post()),$this->db,$session_id);
$this->load->helper('url');
redirect("/signup/in_progress/".$session_id."/1");
And later in the switch I use CI's Validations to display a form and process 'Next' if it was clicked or it is just being called with /signup/in_progress/session/2:
$this->form_validation->set_rules("your rules");
if ($this->form_validation->run() == FALSE) {
$this->load->view('signupStage2',array('signup_data'=>$signup_data));
} else {
$signup_data->save(array_merge(array('ip'=>$_SERVER['REMOTE_ADDR'],'session_id'=>$session_id,'signup_stage' => '3',
'signup_complete' =>'0'),$this->input->post()),$this->db,$session_id);
$this->load->helper('url');
redirect("/signup/in_progress/".$session_id."/3");
};
At the bottom of each view (eg 'signupStage2.php') I have the prev and next buttons:
<span class="align-left"><p><input type="submit" name="prev" class="big-button"
value="<- Prev" /><input type="submit" name="next" class="big-button"
value="Next ->" /></p></span>
I've got a CakePHP search form that has 'type'=>'get'. Basically, one of the elements in the form is a submit button of type image. When the form is posted, in the URL I always get these x & y coordinates of the image submit button:
http://site.com/controller/action?x=22&y=36&query=hello
Is there any way I can prevent the coordinates from showing up in the URL? The reason is so that someone else could use the same URL to perform the same search, without that unintuitive stuff in the link.
Thanks!
You could use some javascript on the button:
document.getElementById('myImageButton').onclick = function() {
this.form.submit();
return false;
};
Alternatively, in your controller in the beforeFilter function, you could check for the presence of the unwanted variables, strip them out and redirect to the nice URL. This does mean there'll be 2 HTTP requests made though.
Sounds like you are looking to do a Post/Redirect/Get.
Here are two examples of doing this in CakePHP:
Searching on surname
Searching on multiple fields
Advantages of redirecting a POST to a GET request are:
Users don't get the "Do you want to resubmit?" dialog if they refresh
The resulting page/query can be bookmarked
You can utilise CakePHP's built-in SEF routing, so instead of URLs with /search?q=contact you can get /search/contact
Instead of using submit helper function, use button function and set button type to submit.
echo $this->Form->**button**($this->Html->image('header_search_icon.png'), array(**'type'=>'submit'**));
Im looking for a way to have a form in cakephp that the user can add and remove form fields before submitting, After having a look around and asking on the cake IRC the answer seems to be to use Jquery but after hours of looking around i cannot work out how to do it.
The one example i have of this in cake i found at - http://www.mail-archive.com/cake-php#googlegroups.com/msg61061.html but after my best efforts i cannot get this code to work correctly ( i think its calling controllers / models that the doesn't list in the example)
I also found a straight jquery example (http://mohdshaiful.wordpress.com/2007/05/31/form-elements-generation-using-jquery/) which does what i would like my form to do but i cannot work out how to use the cakephp form helper with it to get it working correctly and to get the naming correct. (obviously the $form helper is php so i cant generate anything with that after the browser has loaded).
I an new to cake and have never used jQuery and i am absolutely stumped with how to do this so if anyone has a cakephp example they have working or can point me in the right direction of what i need to complete this it would be very much appreciated.
Thanks in advance
I would take the straight jquery route, personally. I suppose you could have PHP generate the code for jquery to insert (that way you could use the form helper), but it adds complexity without gaining anything.
Since the form helper just generates html, take a look at the html you want generated. Suppose you want something to "add another field", that when clicked, will add another field in the html. Your html to be added will be something like:
<input type="text" name="data[User][field][0]" />
Now, to use jquery to insert it, I'd do something like binding the function add_field to the click event on the link.
$(document).ready( function() {
$("#link_id").click( 'add_field' );
var field_count = 1;
} );
function add_field()
{
var f = $("#div_addfield");
f.append( '<input type="text" name="data[User][field][' + field_count + ']" />' );
field_count++;
}
Of course, if a user leaves this page w/o submitting and returns, they lose their progress, but I think this is about the basics of what you're trying to accomplish.
This was my approach to remove elements:
In the view, I had this:
echo $form->input('extrapicture1uploaddeleted', array('value' => 0));
The logic I followed was that value 0 meant, not deleted yet, and value 1 meant deleted, following a boolean logic.
That was a regular input element but with CSS I used the 'display: none' property because I did not want users to see that in the form. Then what I did was that then users clicked the "Delete" button to remove an input element to upload a picture, there was a confirmation message, and when confirming, the value of the input element hidden with CSS would change from 0 to 1:
$("#deleteextrapicture1").click(
function() {
if (confirm('Do you want to delete this picture?')) {
$('#extrapicture1upload').hide();
// This is for an input element that contains a boolean value where 0 means not deleted, and 1 means deleted.
$('#DealExtrapicture1uploaddeleted').attr('value', '1');
}
// This is used so that the link does not attempt to take users to another URL when clicked.
return false;
}
);
In the controller, the condition $this->data['Deal']['extrapicture1uploaddeleted']!='1' means that extra picture 1 has not been deleted (deleting the upload button with JavaScript). $this->data['Deal']['extrapicture1uploaddeleted']=='1' means that the picture was deleted.
I tried to use an input hidden element and change its value with JavaScript the way I explained above, but I was getting a blackhole error from CakePHP Security. Apparently it was not allowing me to change the value of input elements with JavaScript and then submit the form. But when I used regular input elements (not hidden), I could change their values with JavaScript and submit the form without problems. My approach was to use regular input elements and hide them with CSS, since using input hidden elements was throwing the blackhole error when changing their values with JavaScript and then submitting the form.
Hopefully the way I did it could give some light as a possible approach to remove form fields in CakePHP using JavaScript.