Create array from submitted values - html

How do I create 2 HTML arrays from value pairs on form submit?
from <input type="text" name="val_a1" value="1">
to <input type="text" name="val_b1" value="2">
from <input type="text" name="val_b1" value="3">
to <input type="text" name="val_b1" value="4">
from <input type="text" name="val_c1" value="5">
to <input type="text" name="val_c1" value="6">
to look like
array[1,3,5] and array[2,4,6]
Do I need to five unique field names, like in my example or just keep val_a and val_b?

Use brackets in the field names to have the input value return an array:
from <input type="text" name="from[0]" value="1">
to <input type="text" name="to[0]" value="2">
from <input type="text" name="from[1]" value="3">
to <input type="text" name="to[1]" value="4">
from <input type="text" name="from[2]" value="5">
to <input type="text" name="to[2]" value="6">
Note that the keys (0, 1, 2) are optional and could be anything you want (or none at all), but I used them so it would make more sense once you get the return values. You should receive from and to as an array when submitting the form now.

AFAIK you can have several query values with the same name. So it would then depend on what backend you are using to parse the querystring. Common practice is to name several fields that should be grouped together name[], and most backends will turn that into an array then. So try both methods out on your backend and check how they are treated!

if you just want an array you can do it like this
from <input type="text" name="from[]" value="1">
to <input type="text" name="to[]" value="2">
from <input type="text" name="from[]" value="3">
to <input type="text" name="to[]" value="4">
from <input type="text" name="from[]" value="5">
to <input type="text" name="to[]" value="6">
you don't need to number them they will be in order they are recieved
but you can go into nesting too see this post for more details

Related

why this pattern=”^re\-\d{6}$" not working properly in html

it's not giving me error message when I input different pattern, what did I do wrong ?
<label for="receipt">Receipt number *</label>
<input name="receipt" id="receipt"
placeholder="re-nnnnnn" required="required"
pattern="^re\-\d{6}$" />
It depends on what you want to achieve. For example, if your goal is re-123456, you should remove the first back-slash.
<form action="">
<label for="receipt">Receipt number *</label>
<input name="receipt" id="receipt" placeholder="re-nnnnnn" required="required" pattern="^re-\d{6}$" />
</form>

Woocommerce bookings

I'm trying to create custom steps on checkout page with Woo bookings plugin.
So I have 3 steps.
create booking product with parameters like duration, start date, end date.
Extra services
Payment
My problem is that I need to submit form details, in other words add this booking product to cart, but without submitting order.
I'm using this structure with hardcoded values, but whem I'm submitting this it acts like place order button, not like add to cart button. How do I just add to cart?
<div class="form-group">
<input type="radio" id="Terminal1withcancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal1withcancelation">terminal 1</label>
<input type="radio" id="Terminal1nocancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal1nocancelation">terminal 1</label>
<input type="radio" id="Terminal2withcancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal2withcancelation">terminal 2</label>
<input type="radio" id="Terminal2nocancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal2nocancelation">terminal 2</label>
<input type="hidden" name="wc_bookings_field_duration" value="4">
<input type="hidden" name="wc_bookings_field_start_date_month" value="07">
<input type="hidden" name="wc_bookings_field_start_date_day" value="23">
<input type="hidden" name="wc_bookings_field_start_date_year" value="22">
<input type="hidden" name="wc_bookings_field_start_date_to_month" value="07">
<input type="hidden" name="wc_bookings_field_start_date_to_day" value="26">
<input type="hidden" name="wc_bookings_field_start_date_to_year" value="2022">
</div>
<button type="submit" name="btn1" value="btn1" class="buy custom_add_btn" id="nextBtn" onclick="nextPrev(1)">NEXT</button>

Cannot store multiple radio button values in php and mysql

I want to store radio button values for each question to my database. I ran it and it stores only for the first question. I know that it may be wrong using if and elseif. The user answers in all questions how can I store them all in mysql table? Does it need to use for loop?
saveunit.php
<?php
include'connect.php';
if(isset($_POST['u1'])){
$u1 = $_POST['u1'];
mysql_query("INSERT INTO unit_form (u1) VALUES ('$u1')");
}
elseif(isset($_POST['u2'])){
$u2 = $_POST['u2'];
mysql_query("INSERT INTO unit_form (u2) VALUES ('$u2')");
}
?>
unitform.php
<form action="saveunit.php" method="POST">
U1 I have found the unit intellectually interesting and stimulating : <br />
1 <input type="radio" name="u1" value="1"> 2 <input type="radio" name="u1" value="2"> 3 <input type="radio" name="number" value="u1"> 4 <input type="radio" name="u1" value="4"> 5 <input type="radio" name="u1" value="5"> <br /> <br />
U2 I have gained knowledge that I consider valuable : <br />
1 <input type="radio" name="u2" value="1"> 2 <input type="radio" name="u2" value="2"> 3 <input type="radio" name="u2" value="3"> 4 <input type="radio" name="u2" value="u2"> 5 <input type="radio" name="u2" value="5"> <br /> <br />
U3 I have acquired skills and abilities that I consider valuable : <br />
1 <input type="radio" name="u3" value="1"> 2 <input type="radio" name="u3" value="2"> 3 <input type="radio" name="u3" value="3"> 4 <input type="radio" name="u3" value="4"> 5 <input type="radio" name="u3" value="5"> <br /> <br />
<input type = "Submit" name = "Submit1" value = "Submit">
<input type="reset" name="reset" value="Clear">
I dont think that you need an if and an else if. It stores the u1 because it always execute the first if(), that's why it always store the first data . What you need to do is to have all the units in one if().
Try this...
if(isset($_POST['u1'])){
$u1 = $_POST['u1'];
$u2 = $_POST['u2'];
$u3 = $_POST['u3'];
$u4 = $_POST['u4'];
$u5 = $_POST['u5'];
mysql_query("INSERT INTO unit_form (u1,u2,u3,u4,u5) VALUES ('$u1','$u2','$u3','$u4','$u5')");
}

Html multidimensional array in

I would like to pass multiple value with check box,is any provision to pass multiple value using
check box in html.
<input type="checkbox" name="service_id[]" value="1">
please suggest me how should be it is possible
It can be done this way:
HTML
<form method="post">
<input type="checkbox" name="service_id[0][]" value="1">
<input type="checkbox" name="service_id[0][]" value="2">
<input type="checkbox" name="service_id[1][]" value="3">
<input type="checkbox" name="service_id[1][]" value="4">
<input type="checkbox" name="service_id[1][]" value="5">
<input type="submit">
</form>
PHP
<?php
if(!empty($_POST['service_id']))
var_export($_POST['service_id']);
You can only have one value per input. If you want to have multiple values, then either:
give each set of values a unique identifier and resolve it on the server
encode the data in a format like JSON or CSV and then parse it on the server
If you want to have multiple inputs with different values, then just create multiple elements in the HTML.
PHP will discard all but one of them if they come from inputs which share a name and that name doesn't end with [], but your name does:
<input type="checkbox" name="service_id[]" value="1">
<input type="checkbox" name="service_id[]" value="2">
<input type="checkbox" name="service_id[]" value="3">
<input type="checkbox" name="service_id[]" value="4">
<input type="checkbox" name="service_id[]" value="5">
<input type="checkbox" name="service_id[]" value="6">

Generete Unique Id and pass into text field

I am new to php and would like some help on solving a problem I am having. I have a form that has 3 visible input fields, Name, Email address, phone number. Also it includes 3 hidden fields, one is a value set to 2500, the second is a currency, the third needs to be a unique ID. I need help on how to generate a unique id and pass the text/value into a input field in another form. Below is my code. I need the random id to be displayed on the input field merchantid. Thanks
<form action="https:urlthathastheform" method="post">
<input type="hidden" id="merchantid" name="merchantid" />
<input type="hidden" id="currency" name="currency" value="$" />
<label for="names">Names</label>
<input type="text" name="names" id="names" />
<label for="email_address">Email Address</label>
<input type="text" name="email_address" id="email_address" />
<label for="phone_number">Phone Number</label>
<input type="text" name="phone_number" id="phone_number" />
<label for="amount">Amount</label>
<input type="text" name="amount" value="2500" />
<input type="submit" name="submit" value="submit" />
</form>
php has a function to generate a unique id.
<input type="hidden" id="merchantid" name="merchantid" value="<?php echo uniqid();>"/>
Note, this id is based on the system clock. http://php.net/manual/en/function.uniqid.php