Dynamic Form Handling in PHP, jQuery & HTML - php

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;
}

Related

SagePay Protocol 3.00 PHP Server Integration Query String POST

I am a beginner PHP developer and trying to integrate sagepay with my site that would allow customers to access a link on my website 'www.example.com/payment' and fill out a form with the amount they owe me and a corresponding invoice number which I have assigned to them and their account number, their billing details, address, full name, postcode and delivery address which should then create a query string sent to the SagePay server.
Below, I have got an example of the form I will be putting in the site:
<form action="https://test.sagepay.com/gateway/service/vspform-register.vsp" method="post" id="SagePayForm" name="SagePayForm">
<input type="hidden" name="Vendor" value="Vendorname" />
<input type="hidden" name="VPSProtocol" value="3.00" />
<input type="hidden" name="TxType" value="PAYMENT" />
<input type="hidden" name="Currency" value="GBP"><br />
<input type="hidden" name="NotificationURL" value="http://example.com/payment/"
<input type="number" name="VendorTxCode" placeholder="Invoice Number" value="123456"><br />
<input type="number" name="Amount" placeholder="Amount" value="250"><br />
<input type="text" name="Description" placeholder="Description of Invoice" value="Payment for your services"><br />
<input type="text" name="BillingFirstnames" placeholder="Firstname(s)" value="Fname Mname"><br />
<input type="text" name="BillingSurname" placeholder="Surname" value="Surname"><br />
<input type="text" name="BillingAddress1" placeholder="Address Line 1" value="BillAddress Line 1"><br />
<input type="text" name="BillingAddress2" placeholder="Surname" value="BillAddress Line 2"><br />
<input type="text" name="BillingCity" placeholder="City" value="BillCity"><br />
<input type="text" name="BillingPostCode" placeholder="Post/Zip Code" value="W1A 1BL"><br />
<input type="text" name="BillingCountry" placeholder="Country" value="United Kingdom"><br /><br />
<input type="text" name="DeliveryFirstnames" placeholder="Firstname(s)" value="Fname Mname"><br />
<input type="text" name="DeliverySurname" placeholder="Surname" value="Surname"><br />
<input type="text" name="DeliveryAddress1" placeholder="Address Line 1" value="BillAddress Line 1"><br />
<input type="text" name="DeliveryAddress2" placeholder="Surname" value="BillAddress Line 2"><br />
<input type="text" name="DeliveryCity" placeholder="City" value="BillCity"><br />
<input type="text" name="DeliveryPostCode" placeholder="Post/Zip Code" value="W1A 1BL"><br />
<input type="text" name="DeliveryCountry" placeholder="Country" value="United Kingdom"><br />
<input type="submit" name="submit" value="Submit">
</form>
I keep getting the INVALID status error with the error code: 5030 which says: 5030 : We could not process your message, please check your integration settings or contact the support team
Any guidance will be appreciated. Thanks
I recommend starting out with http://www.sagepay.co.uk/file/9981/download-document/VspPHPKit.zip
Direct api:
https://github.com/colinbm/sagepay-php
Very simple to use.

HTML Element Arrays - Multidimensional

I'm not 100% sure of the correct terminology of what I'm even asking, but I currently have a form that is being posted that has an array of elements:
<form>
<input type="text" name="album_songs[0][title]" value="Let It Go" />
<input type="text" name="album_songs[0][writer]" value="Robert Lopez, Kristen Anderson-Lopez" />
<input type="text" name="album_songs[0][composer]" value="Frozen" />
<input type="text" name="album_songs[0][length]" value="3:44" />
<input type="text" name="album_songs[0][genre]" value="Soundtrack, Music, Animated" />
<input type="text" name="album_songs[0][songorder]" value="5" />
<input type="text" name="album_songs[1][title]" value="Love Is An Open Door" />
<input type="text" name="album_songs[1][writer]" value="Robert Lopez" />
<input type="text" name="album_songs[1][composer]" value="Frozen" />
<input type="text" name="album_songs[1][length]" value="2:07" />
<input type="text" name="album_songs[1][genre]" value="Soundtrack, Music, Animated" />
<input type="text" name="album_songs[1][songorder]" value="4" />
</form>
And in my code, I can access these elements like the following:
$songs = $_POST["album_songs"];
foreach($songs as $song) {
$title = $song["title"];
$writer = $song["writer"]; // turn into array??
$composer = $song["composer"];
$length = $song["length"];
$genre = $song["genre"]; // turn into array??
$songorder = $song["songorder"];
}
What I would like to have happen, is not only have an array of songs, but also an array of Writers and Genres. Is this even possible? I tried mocking up something like the following but it did not work:
<input type="text" name="album_songs[4][genre[0]]" value="Soundtrack" />
<input type="text" name="album_songs[4][genre[1]]" value="Music" />
<input type="text" name="album_songs[4][genre[2]]" value="Animated" />
Any suggestions or is it even possible?
I might consider a dropdown, but you almost had it:
<input type="text" name="album_songs[4][genre][]" value="Soundtrack" />
<input type="text" name="album_songs[4][genre][]" value="Music" />
<input type="text" name="album_songs[4][genre][]" value="Animated" />
Gives this:
Array
(
[4] => Array
(
[genre] => Array
(
[0] => Soundtrack
[1] => Music
[2] => Animated
)
)
)
Try to use square bracket syntax to convert your form inputs into an array.
I.E. :
<input type="text" name="album_songs_title[]" value="Title1" />
<input type="text" name="album_songs_title[]" value="Title2" />
Then, you will be able to browse your datas with a foreach().
I think it might work.

Sage Pay form integration encoding

<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.

How to capture form text fields as an array like a radio group

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

Add more (sets of) fields to form by jQuery

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...

Categories