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).
Related
I have a form (right now only one, but there will be more with similar parameters) and I validate it using jQuery Validation (it is too big word, I validate only one field using jQV), this form is send using AJAX. I have in this form 5 inputs:
text input: username, which is validated by separate PHP script remotely - it checks if this user exists in database of different site and if so, also checks if this user has some specified data, but this is not really important
2 times radio buttons (i mean 2x 2 radio buttons) which are validated in PHP (code later)
2 times PickAColor forms, which are also validated using PHP only with regexp for hexcolors
And I have a question. Is there any way to check this data in PHP in more, well, sophisticated way? Because, as I said, I validate them in similar way:
if($period != "3month" && $period != "7day") {
echo '<div class="alert">Wrong period!</div>';
exit;
}
Actually this form I am writing right now is rather short, but I also have some really big ones, and if's like this one above can take even 50 lines, and that pretty sucks. Most of them has similar expressions inside if, which means that I check if value sent by form is on the list of possible options provided by form (someone can change value parameters in Firebug and call himself hacker...). It's complicated and I don't know how to said that, but I think you understand me. So, once again - is there any sense to validate it in PHP and if so, is there any better way to validate it? Or maybe you have some other suggestions? Thanks a lot for your help.
Man you have to think your users can submit anything there, and to make the code clearer and more MVC oriented (and reusable) you need your validations in the app side, and generic functions in jQuery to just display errors to your users, so I would recommend you to do ajax post with the form sent to your php application, to an api controller that recives the form values, validates using the model and returns the object with the jew form values (if you need to clean up) and any error per field, then you just output in json :
{
errors: {
name: "Already used name",
field2: "Some bad text provided"
}
}
Then you just loop through the errors and set to the already existent placeholder of each error text like
<spam id="name-error" class="error"></spam>
<input type="text" name="name" value=""/>
Then you have a validation code that you can reuse when the form is submitted.
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.
I have a web app that has a big and complex form (fields, checks, etc).
I hade using standard OS form controls because they have visual (styling) limits.
I have been basically creating spans with IDs and attaching class or custom data attributes. I later need to send this to a PHP script for insertion into a database. This has worked well in the past, but I'm having to do a lot of manual processing of fields, which I don't think is efficient... Is there a better way?
I currently do,
IF ($('#foo').hasClass('on')){bar=1}
...
Then I manually compose a POST string via
foo=bar&bla=blabla ...
Then lots more on the PHP side to create an insert SQL statement.
Seems like its inefficient if you have dozens of fields... But I hate standard FORM elements...
Any suggestions? .... Loops? Arrays?
You should use a form. But how to make it pretty?
With JavaScript you can have a kind of front-end for the form. Hide the form elements and have some JS that changes the form field values on interaction.
Search for pretty forms jquery in Google.
You could use jQuery's serialize() method:
$('form .on').serialize();
jsFiddle example
From what I understand is that you don't want to manually get the value of each element that has the class 'on'. You could use jQuery's each function looping through all the elements you need. Something like:
var query = "";
$(".on").each(function(i){
if(i === 0) {
query+="?"+$(this).attr('id')+"="+$(this).text();
} else {
query+="&"+$(this).attr('id')+"="+$(this).text();
}
});
(see: http://jsfiddle.net/MkbrV/)
Is this something you are looking for?
So, I'm pretty much a novice when it comes to programming and I would certainly appreciate a bit of help with an issue I'm having trouble getting my head around. Simply put, how do I get the selected variables from jquery.chained.remote.js back into my form for processing?
Here's the functioning, sort of, sample page.
http://www.noradaron.com/samplesearch/index.php
The dropdown list functions just fine but I just don't understand how to get the selected values back into my form. Yes, I will freely admit my knowledge is limited and I may not fully understand the answer once it is given. Regardless, I certainly appreciate any help I can get. Please let me know if there is anything else I could provide that would make my question more clear.
Attach submit event handler to your form like this:
$('form[name="search-vehicles"').submit(function() {
//code
});
Inside code grab the values like:
var value = $('#vType').val();
And update your text with the new values. However I recomment you to not write "textmore text" But to write your text inside *DIV*s or *SPAN*s with *ID*s so you can easily access them and change their values like:
<div id="selectedName"></div>
Then you can use:
$('#selectedName').html(value);
Beside putting id="vType" add name="vType" (and so on) so you can have your data sent back to server with either GET or POST .
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