Two Forms One Submission - php

My site has three columns. I have two fields within a form that need to be in the left column and 1 field of what is currently a different form that needs to be in the middle column. The thing is, I want them to behave as one form... (or be one form if that is possible).
When The form is submitted, the data from all the fields, needs to be passed to the action page.
What is the best way of achieving this? h

If you don't have to support old browsers, you can use the form attribute on some elements to make them behave like they are in the form.
See http://www.impressivewebs.com/html5-form-attribute/
<form id="some_form">
...
</form>
<input form="some_form" />
Else, the better way is to make the form element a parent of all fields. Just move the form element out of the first column.

Columns do not a form make. Wrap all of your column divs in one form tag.

It sounds like you control the code. The simplest solution is to make a single form. What you're looking for is non-standard functionality and whatever solution you settle on my cause problems in the future.

Related

Customize Symfony forms and get more control over validation process

I use doctrine:generate-admin to build CRUD operations for one of my models called "Alumnos". I know that Symfony generate the views under /apps/site/modules/alumnos/views and I'm trying to customize the input fields using Twitter Bootstrap Framework. The _form.php file has this code:
<?php echo $form['nombres']->renderLabel() ?>
<?php echo $form['nombres']->renderError() ?>
<?php echo $form['nombres'] ?>
But I want to add a (*) to required fields, a class to input fields and also wrap errors inside <div> tags with properly markup. How I can achieve this?
Also (don't know if it's better to open a new question for this part) I six fields and need to achieve this:
If the first three fields (less said 1,2,3) are populated with values then validation pass
If the second three fields (less said 4,5,6) are populated with values the validation pass
At least firs three fields or second three fields are required so in all cases 1,2,3 can't be empty or 4,5,6 can't be empty
How I deal with this validation?
If that's only in one form, you can go the hacky way:
replace, <?php echo $form['nombres']->renderLabel() ?> with HTML, that simple ^^
For your second question, you need a post validator in your form to check several fields. Here is an example:
http://symfony.com/legacy/doc/cookbook/1_2/en/conditional-validator
Maybe look in the jobeet tutorial if there is a post validator example.
For the first part of your question you should look at http://symfony.com/legacy/doc/more-with-symfony/1_4/en/06-Advanced-Forms at the bottom of the page they describe how to create a new WidgetFormSchemaFormatter.
For the second part, as Francois said, use a post validator, or a custom validator.

Submit multiple forms as one

I have two forms on the page. To the user it looks like 1 form, and I actually wish it was one form. But the way I'm trying to reuse code and include things, I can't avoid two forms in the source code... trying to act as one.
I don't want to do ajax submit, I want a normal post submit, my form handler has redirects in it. How can I submit both of these, and get values that make sense on the server side. something like $_POST['form1]['whatever'] $_POST['form2]['thing']
Maybe take all the inputs from form 2, rename all of them with a prefix, and append them to form 1? I can't find a non-messy way of doing this. I don't think I need code, just a plan. Least messy idea wins.
Maybe take all the inputs from form 2, rename all of them with a
prefix, and append them to form 1?
That's exactly what you have to do. Wouldn't be much of an answer without a code sample, so here you go.
$("#form2 :input").appendTo("#form1")[0].submit()
now in php you'll have $_POST['thing'] containing an array with two values. Alternatively you can rename all of the inputs from form2:
$("#form2 :input").attr("name",function(name){
return name + "_form2";
}).appendTo("#form1")[0].submit();
You can try to collect values of one form with jQuery.serializeArray() and then generate hidden inputs with names and values from variable storing result of previously called jQuery.serializeArray() and insert them to second form on submit event of form.
You should be able to combine both forms fields into one single form in PHP. If your code doesn't allow it, it must be in terrible shape.
If you are using simple scripts, you should be able to cut the forms into parts producing the fields, and the other parts producing the form outlines, either as separate scripts, or simply as separate functions.
i.e.:
<?php
function form_body($params) {
// here's the code for echoing fields according to $params
}
function form($params) {
// here's the code to build the form properties
$f_properties = '....';
echo '<form '.$f_properties.'>';
form_body($params);
echo '</form>';
}
?>
Then it's just a matter of combining $params from form1 and form2 to get the definitive form.
If you're using OOP, it's probably very easy to derive a new class containing both forms, and have it output their fields.
This are very simplistic advices, but you don't provide any source to help refactoring, so I can only provide vague/generic code examples.
Going the js way to combine forms on the client side will turn into a lot of problems down the line to maintain and evolve the code, and bring a lot of issues (security not the least of them).

How to create a PHP script that can handle multiple forms

Everytime I create a website for a client, I write the form HTML and then write the php script to handle that data. Sometimes the forms are long, sometimes there are multiple forms - it can get messy.
Before I begin to try and write my dynamic php form handler I'd really like some best practice advice and tips.
I thought of gathering all of the posted variables into an array to handle them. But then how do I know which values were supposed to be required or what they mean?
Maybe something already exists to fix this problem!
Thanks a lot,
Henry
Just a bit more info, what I have in mind is a php script which is flexible enough to work with any form built for it with any amount of inputs. I guess I see it as one file that sits on the server and multiple forms will be sending Ajax requests to it, which it can then satisfy
I don't think you should go with arrays, since the $_POST is already an array anyway.
But what about some sort of naming convention in your code?
ex:
<input type="text" name="txt_username" /> //prefix txt or whatever seems fitting.
Then use regular expressions to find what type of data you expect and act accordingly. You could for example write a class that handles different sorts of input and depending on the prefix in the name-property pass the data to the correct function.
Change the name of you submit button according to your form name. Then in php use a conditional statement to determine which form is posted and get your php working aas you wanted for different forms.
something like this
<input type="submit" name="signupform"/>
in php
if(isset($_POST['signupform']))
{
//Do this
}
elseif(isset($_POST['loginform']))
{
//do this
}
known issue:
You will need to keep those submit names unique and there should not be any other for element in any form being posted to that php file.
You can also add a hidden field
<input type="hidden" name='action' value='signup'/>
and then use a switch case with $_POST['action'] key.
I find it helps to seperate form parts by assigning them to an array e.g.
<input type="text" name="Users[username]" id="Users_username" />
That way, I can easily pick out sections by accessing the array key in php e.g.
$_POST['Users']; // returns username from above example

Nested Input Elements do not seem to appear in the dom

Im building a wordpress plugin for client that does a number of jobs.
My goal is to simply get all the input tags in some html and use the data.
I have some html(that contains inputs)
The user fills the inputs in and clicks save.
Javascript puts the entire htmlinto another hidden input for POSTING purposes.
I then retrieve the html from posted item ie: $_POST["my_html"]
I get the input elements using the DOM. getElementsByTagName.
But the input values are EMPTY.
Am I doing something wrong. Can this be done (above) ?
Why choose such a difficult path? Just submit your form normally and get the values from $_POST. As for your method, my guess (since no code is provided) is that you try to add whole DOM element as a string. You need to set each's elements value (element.value) not the whole element. I could clarify my answer if some code could be provided.
The correct way to serialize a form is not to store its' html markup. You should be storing key-value pairs instead, which can be neatly serialized in a number of ways, JSON being a very popular and easily graspable method.
There's also the possibility of submitting your form directly to the handling script, which has been a working solution since HTML 2.0.

Customizing Zend_Form

I am dealing with Zend_Form right now and I am having a difficult time figuring out how to:
Use custom images for form buttons and,
Insert text and links in specific places (in my case I want to put a "forgot your password?" link before the submit button).
I've read through the manual but am not seeing anything about this.
I think Zend_Form_Decorators may help you: http://framework.zend.com/manual/en/zend.form.decorators.html
try to read also this article, I think it is the best one about decorators in Zend_Form
http://devzone.zend.com/article/3450-Decorators-with-Zend_Form
for your usage will be most helpful part about "Full Customization Using the ViewScript Decorator", try to find it in article
You can write straight up HTML in your view, just make sure that the element names, select options, hidden values etc. correspond to your Zend_Form elements (or set the element decorators to just ViewHelper and use echo $this->form->element where the elements should be). It's just the same POST data anyway when you submit the form. If you're using the form in more than one place, check out the ViewScript decorator.
CSS?
You can use an element's setDescription() method to add a description to it. If I remember correctly, this renders into a 'p class="hint"' element, but you can of course change that using the form decorators.

Categories