It is easy to add more fields to a html form by jQuery. Then, we can serialize the fields, if the have the same name but what if we have a set of fields? For example
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
Now I want to add a new set of fields by jQuery as
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
and so forth. I process the form with PHP to insert movies into mysql database with three columns of (name, director, year). In the above-mentioned example, it is hard to serialize the fields to create appropriate $_POST arrays. How should I serialize jquery-added sets of movies?
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
Nothing else. On the server you will get (in case of POST) array in $_POST['movie_name'], $_POST['movie_director'] and $_POST['movie_year'];. Elements with the same index are from the same set of inputs.
What kind of problem with serialization do you have?
<form>
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<hr />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<br />
<input type='button' id='serialize' value='Click me' />
</form>
and js code:
$('#serialize').click(function(){
alert($('form').serialize());
});
when you want to submit the data just write
$.post('script.php', $('form').serialize(), function() {alert('Saved');});
ps: if you are afraid to lose something, just compare count($_POST['movie_name']), count($_POST['movie_director']) and count($_POST['movie_year']).
or you can add indexes
<input type="text" name="movie_name[0]" />
<input type="text" name="movie_director[0]" />
<input type="text" name="movie_year[0]" />
<input type="text" name="movie_name[1]" />
<input type="text" name="movie_director[1]" />
<input type="text" name="movie_year[1]" />
Based on useful discussion with Cheery, I came to conclusion that the best and safest way is to use
<input type="text" name="movie_name[i]" />
<input type="text" name="movie_director[i]" />
<input type="text" name="movie_year[i]" />
where we define each i with jQuery to serialize the fields SAFELY. This way, we can be sure that serialized arrays are parallel and well matched, without misplacing.
You can do something like this:
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
// OTHER:
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
And do this to all... In Jquery, you create a function that create field as needed... I'm not the best in JQuery so I can't help you for this but the way I told you worked fine for me with PHP...
Related
<html>
<form name="pp_form" action="https://test.sagepay.com/Simulator/VSPServerGateway.asp?Service=VendorRegisterTx" method="post">
<input name="VPSProtocol" type="hidden" value=2.23 />
<input name="TxType" type="hidden" value=PAYMENT />
<input name="Vendor" type="hidden" value="myusername" />
<input name="VendorTxCode" type="hidden" value="thevendortxcode" />
<input name="Amount" type="hidden" value="30" />
<input name="Currency" type="hidden" value="GBP" />
<input name="Description" type="hidden" value="Test payment" />
<input name="NotificationURL" type="hidden" value="myurl" />
BillingFirstnames: <input name="BillingFirstnames" type="text" /><br>
BillingSurname: <input name="BillingSurname" type="text" /><br>
BillingAddress1: <input name="BillingAddress1" type="text" /><br>
BillingCity: <input name="BillingCity" type="text" /><br>
BillingPostCode: <input name="BillingPostCode" type="text" /><br>
BillingCountry: <input name="BillingCountry" type="text" /><br>
DeliverySurname: <input name="DeliverySurname" type="text" /><br>
DeliveryFirstnames: <input name="DeliveryFirstnames" type="text" /><br>
DeliveryAddress1: <input name="DeliveryAddress1" type="text" /><br>
DeliveryCity: <input name="DeliveryCity" type="text" /><br>
DeliveryPostCode: <input name="DeliveryPostCode" type="text" /><br>
DeliveryCountry: <input name="DeliveryCountry" type="text" /><br>
<p>Click here to submit
<input type="submit" value="here">
</p>
</form>
</html>
This form currently works using the simulator. Clearly, none of this is being encoded. Firstly, will this work on the test/live environment? Secondly, am I allowed to do it this way and if not, how can I correct it?
Thanks
Alex
It's not going to work. Firstly, the simulator is so far out of date that it doesn't even support protocol 3.00 (earlier ones are deprecated and will cease to function in the live environment from July). Some key features are also missing.
Secondly, you appear to want to use the Server protocol. The registration post for this consists of the values you have above as name-value pairs, posted as the request body to https://test.sagepay.com/gateway/service/vspserver-register.vsp
I recommend having a look at the Server protocol document on sagepay.co.uk - using the simulator isn't going to help.
I was wondering if it would be possible to have a variable in the URL created when submitting a form?
form:
<form class="register_form" action="action.php" method="get">
Team Name*: <input type="text" name="teamname" required />
Team Region*: <input type="text" name="teamregion" maxlength="4" required />
Team Leader*: <input type="text" name="teamleader" maxlength="16" required />
Team Members: <input type="text" name="teammembers" />
<input name="register_submit" type="submit" value="Register" />
</form>
I'd like the link to end up as: http://.../action.php?do=register
My reasoning for this is so that I can use action.php for more than one thing using if statements. Thanks ^^
Just append the variable you want to the action link.
<form class="register_form" action="action.php?do=register" method="get">
Team Name*: <input type="text" name="teamname" required />
Team Region*: <input type="text" name="teamregion" maxlength="4" required />
Team Leader*: <input type="text" name="teamleader" maxlength="16" required />
Team Members: <input type="text" name="teammembers" />
<input name="register_submit" type="submit" value="Register" />
</form>
Or you can add a hidden field to your form:
<input type="hidden" name="do" value="register" />
Sure, the form action URL can have a query string:
<form class="register_form" action="action.php?do=register" method="POST">
The form data will be sent via POST but do will still be available via GET.
You need to add this to the form
<input type="hidden" name="do" value="register">
Yes, it is possible. You can use any one of following methods
1) You can set the name of your submit button "do"; As the value of your submit button is "Register"
<input type="submit" name="do" value="Register" />
OR
2) You can add a hidden field to your form
<input type="hidden" name="do" value="register" />
I hope I ask this question correctly, and if not please direct me how to repair it. I have had it deleted as a post once already...
My goal is to submit a form with one drop down, with numbers like 100, 200, 300 (for how many T-shirts you want to order)... Then depending on what is selected from the drop down have a series of text boxes (for number placement) that must add up to the selected number of shirts you want to order from the dropdown.
My idea is to capture all these text fields in an array, and send them off to a function to be added...
Can someone assist me please?
Here is the form code I know does not work, but I want it to work...
<form>
<label>
<input type="checkbox" name="PoloDesign" value="100" id="PoloDesign_0" />
100</label>
<br />
<label>
<input type="checkbox" name="PoloDesign" value="200" id="PoloDesign_1" />
200</label>
<br />
<label>
<input type="checkbox" name="PoloDesign" value="300" id="PoloDesign_2" />
300</label>
<br />
<input type="text" name="name[1]" id="name1" value="{$name1}"/>
<input type="text" name="name[1]" id="name2" value="{$name2}"/>
<input type="text" name="name[1]" id="name3" value="{$name3}"/>
<input type="text" name="name[1]" id="name4" value="{$name4}"/>
<input type="text" name="name[1]" id="name5" value="{$name5}"/>
<input type="text" name="name[1]" id="name6" value="{$name6}"/>
<input type="text" name="name[1]" id="name7" value="{$name7}"/>
<input type="submit" value="submit"/>
</form>
Just change each
name="name[1]"
To
name="name[]"
Then the fields are posted as an array you can iterate through in PHP
if (is_array($_POST['name']):
foreach ($_POST['name'] as $key=>$field):
// do something here
$yourKey = $key +1;
$yourValue = $field;
I have changed your code a little and tried to make it work using regular expression:
<?php
$name_array = preg_grep('/name[1-9]*/', $_GET);
?>
So, basically it checks all submitted variables and creates array from all variables that have name at start and a number at end. So, the form part should change to look like this:
<input type="text" name="name1" id="name1" value="{$name1}"/>
<input type="text" name="name2" id="name2" value="{$name2}"/>
<input type="text" name="name3" id="name3" value="{$name3}"/>
<input type="text" name="name4" id="name4" value="{$name4}"/>
<input type="text" name="name5" id="name5" value="{$name5}"/>
<input type="text" name="name6" id="name6" value="{$name6}"/>
<input type="text" name="name7" id="name7" value="{$name7}"/>
I tested on Apache2 and PHP 5.3
Ugh! Any ideas?
Have a form which tracks user activity for given time intervals. Each time interval can have inputs for "location" & multiple "activities". So the form has a variable number of time intervals and a variable number of activities per interval.
How can I set this up in HTML for smooth processing PHP? EDIT: Additional intervals and activities can be added via jQuery.
My initial thought is convoluted so anyone have a better idea?
<!--Interval 1 with 4 activities-->
<input type="text" name="interval[]" />
<input type="text" name="location[]" />
<input type="text" name="activity[]" />
<input type="text" name="activity[]" />
<input type="text" name="activity[]" />
<input type="text" name="activity[]" />
<!--Interval 2 with 2 activities-->
<input type="text" name="interval[]" />
<input type="text" name="location[]" />
<input type="text" name="activity[]" />
<input type="text" name="activity[]" />
This is a nightmare on the server side.
You may need to isolate activities per location / interval. A solution would be to use an index for each interval, this involve to change the way you create the new html elements for each interval.
So the resulting code would be :
<!--Interval 1 with 4 activities-->
<input type="text" name="interval[1][]" />
<input type="text" name="location[1][]" />
<input type="text" name="activity[1][]" />
<input type="text" name="activity[1][]" />
<input type="text" name="activity[1][]" />
<input type="text" name="activity[1][]" />
<!--Interval 2 with 2 activities-->
<input type="text" name="interval[2][]" />
<input type="text" name="location[2][]" />
<input type="text" name="activity[2][]" />
<input type="text" name="activity[2][]" />
But it's not as smooth to manipulate as :
<!--Interval 1 with 4 activities-->
<input type="text" name="interval[1][interval]" value="interval id" /> // optional
<input type="text" name="interval[1][location]" value="location" />
<input type="text" name="interval[1][activity][]" value="activity" />
<input type="text" name="interval[1][activity][]" value="activity" />
<input type="text" name="interval[1][activity][]" value="activity" />
<input type="text" name="interval[1][activity][]" value="activity" />
<!--Interval 2 with 2 activities-->
<input type="text" name="interval[2][interval]" value="interval id" /> // optional
<input type="text" name="interval[2][location]" value="location" />
<input type="text" name="interval[2][activity][]" value="activity" />
<input type="text" name="interval[2][activity][]" value="activity" />
Which will give you a nice tree ( in the form of arrays of arrays ) to iterate over.
In order to do that, you need a javascript function that keep a track of the current interval index ans use that index to order the array of intervals.
Server side all you have to do is ( in case of POST method ) :
foreach ( $_POST['interval'] as $k => $interval ) {
echo $interval['interval'];
echo $interval['location'];
foreach ( $interval['activity'] as $id => $activity )
echo $activity;
}
I have a question.
Currently this form will be dynamically generated.
Example,
<form method="POST">
<input type="text" name="location" id="location1" />
<input type="submit" value="Submit!" />
<input type="text" name="location" id="location2" />
<input type="submit" value="Submit!" />
<input type="text" name="location" id="location3" />
<input type="submit" value="Submit!" />
<input type="text" name="location" id="location4" />
<input type="submit" value="Submit!" />
</form>
So whenever i press submit, it will take last value of form only. How do i make it take all $_POST?
Thank you.
You could give each field a unique name: location1, location2....
Alternatively, you could build an array.
To do that, add a [] to each element's name:
<input type="text" name="location[]" id="location1" />
this will give you an array in $_POST["location"].
Don't forget that before using the data (e.g. in a database query or page output) you will need to sanitize each array element separately (using mysql_real_escape_string() or htmlspecialchars() or whatever is needed in your situation.)
Give each input its own name or use:
<input type="text" name="location[]" id="location1" />
Then PHP will treat it like an array.
Either give each input a different name attribute, for example location1, location2, location3 and location4 then access with $_POST['location1'] $_POST['location2'] etc.
Alternatively (and probably preferred if this form is being generated 'dynamically'), change the name attribute for each input to location[] then access the values entered as an array in PHP. For example...
HTML
<form method="post">
<input type="text" name="location[]" id="location1" />
<input type="submit" value="Submit!" />
<input type="text" name="location[]" id="location2" />
<input type="submit" value="Submit!" />
<input type="text" name="location[]" id="location3" />
<input type="submit" value="Submit!" />
<input type="text" name="location[]" id="location4" />
<input type="submit" value="Submit!" />
</form>
PHP
print_r($_POST['location']);
echo "<form method='post'>";
for($x=1;$x<=4;$x++)
{
echo "<input type='text' name='location".$x."' id='location".$x."'/>";
echo "<input type='submit' value='Submit!' />";
}
echo "</form>";