How to get multiple values from input box using php - php

This is the output of my code
Every item has its own item number. I will be entering randomly a value for 3 items, after doing so I will click on the add button and will be redirected to a confirmation page. How will I get the values with its corresponding item?
Thanks in advance for the answers :)

Use input name as array.
<input type="text" name="price[]" />
<input type="text" name="price[]" />
<input type="text" name="price[]" />
<input type="text" name="price[]" />
<input type="text" name="price[]" />
Get values like this
$price = $_POST['price'];
foreach($price as $key => $val ) {
echo $val;
echo "<br>";
}

You can bind or keep the text box name same as your database row ID so when you submit and in the target page you can redo the select and update the field in the database like
<input type="text" name="$id" />
in the target page use Select Query looping and give
Update table_name set field_name=$_REQUEST[$id] where id=$id

Related

dynamically selecting all variables with the same prefix in php

I have a form and there is a button to append another set of input boxes if you wish to add more information. Everytime it adds a new set of boxes all the input boxes get a unqiue number added on for that set of input boxes.
Example:
If you have three sets of input boxes it would look like this:
name, age, gender, dob
name1, age1, gender1, dob1
name2, age2, gender2, dob2
However, when I send this information over to my php file I extract the information from the array so each one is a variable. So, name would be $name and name1 would be $name1 and so on. But my question is how can I sanitize and validate all the names at once and all the ages at once etc..
The reason I am asking is because I have googled this alot and I can't find an answer on how to do this.
Try to create sets as given in sample below:
For first set:
<input type="text" name="name[]" id="name1" />
<input type="text" name="gender[]" id="gender1" />
<input type="text" name="age[]" id="age1" />
<input type="text" name="dob[]" id="dob1" />
For second set:
<input type="text" name="name[]" id="name2" />
<input type="text" name="gender[]" id="gender2" />
<input type="text" name="age[]" id="age2" />
<input type="text" name="dob[]" id="dob2" />
and set all the further sets accordingly.
Now, to get posted data you can use
<?php
echo "<pre>".print_r($_POST, true)."</pre>";
?>
You may use something like this for each entity:
<input type="text" name="age[]" id="age1" />
Here, id should be in incremental order with JavaScript or jQuery and name should be same which will give you an array for all the attributes in $_POST or $_REQUEST
Print $_REQUEST and you will come to know how exactly you can get all the data.
You are already getting the array for the name, age, etc.
To sanitize use array_map() function in php. It will sanitize the array.
EXAMPLE
$result = array_map("stripslashes", $result);
Here $result is an array

PHP: Adding multiple products to post order form

I'm building a sort of php shop that will allow a user to enter amounts for various products and then a form will process the order. There is no payment processing etc. What I am struggling to do is add each product to each amount to send through to the order form as a $_POST variable. Currently my ordered items are a standard form:
<input type="text" id="value" name="ordered-items[]" />
I thought I could add the product ID as a hidden variable, but that will just add it for every product.
<input type="hidden" id="value" name="products[<?php the_id(); ?>]" />
How can I marry up the product to the order quantity and then send that to the order form for processing, this part I can do myself.
Thanks In advance
You can name the product quantity based on it's ID like this:
<input type="text" name="item[<?php the_id(); ?>]" />
You can then access the product quantities on the server side using for each:
foreach($_POST as $index => $value){
// $index is product id and $value is product quantity
}
Why not put the quantity in a separate input field. In case of a normal text field the visitor can then even change the quantity:
<input type="text" id="item1" name="ordered-items[]" /><input type="text" id="qty1" name="quantity[]" />
<br>
<input type="text" id="item2" name="ordered-items[]" /><input type="text" id="qty2" name="quantity[]" />
<br>
<input type="text" id="item3" name="ordered-items[]" /><input type="text" id="qty3" name="quantity[]" />
In php you can process the data like this:
<?php
$items = $_POST['ordered-items'];
$quantities = $_POST['quantity'];
for($i = 0; $i < count($items); $i++) {
echo $items[$i] . ' has quantity ' . $quantities[$i];
}

Auto fill insert form with $_GET data

I have a search form, where the user can insert in the same field the name of one or more authors.
I have this code:
Author<br /><input type="text" name="autore" value=<?php echo $_GET['autore'] ?> ><br/>
To automatically fill the Author field on the next page.
My problem is that if the user writes for example:
san, gli, tro
In the Author field I'll get only 'san,' , while I want 'san, gli, tro' .
How can I solve this?
What happens if you do:
Author<br /><input type="text" name="autore" value="<?php echo $_GET['autore'] ?>" ><br/>
(Notice quotes around the value attribute)
You need to put quotes around value from php. Like this:
Author<br /><input type="text" name="autore" value="<?php echo $_GET['autore'] ?>" ><br/>
In your case the final html will be
Author<br /><input type="text" name="autore" value=san gli tro ><br/>
so the value of attribute value is san and the other to are empty attribute names.
With quotes, the final html will be
Author<br /><input type="text" name="autore" value="san, gli, tro" ><br/>
Which is what you need.

Inserting paired input fields into mysql

I need to insert pairs of input values from a form into mysql database. For example here are my input boxes:
<input type="text" name="roomType1" size="30" />
<input type="text" name="roomRate1" size="30" />
<input type="text" name="roomType2" size="30" />
<input type="text" name="roomRate2" size="30" />
<input type="text" name="roomType3" size="30" />
<input type="text" name="roomRate3" size="30" />
etc..
And my sql database is set up as follows:
RoomType
RoomRate
HID
So basically I need to figure out how to pass the two input fields into each field together in the same row. I am not sure if I should do a for loop or how I can get each two and insert it with the same ID. I hope this makes sense. and any help would be GREATLY appreciated!
Those inputs must be part of a form with method="post". Then, on the php side use this:
<?php
$i = 1;
while(isset($_POST['roomType'.$i]))
{
$roomType = $_POST['roomType'$i];
$roomRate = $_POST['roomRate'$i];
// perform your sql statements here
$i++;
}
?>
Since your inputs are paired, it's enough to check if only one of them exists;

How do you manipulate form values based off local input?

I have a form that has a series of check boxes and some line items have a text input next to them that defines quantity of the item.
<input type="checkbox" name="measure[][input]" value="<?=$item->id?>">
<input class="item_mult" type="text" name="measure[][input]" />
What is the best way to capture the integer from the input field and have it correspond to the check box so I can use it to calculate the total later on?
<input type="checkbox" name="measure[<?php echo $item->id; ?>][checked]" value="<?php echo $item->id; ?>">
<input class="item_mult" type="text" name="measure[<?php echo $item->id; ?>][input]" />
That should give the values $measure[1]['checked'] (only present if checked) and $measure[1]['input']
I corrected your short tags - they're a bad idea, as they are disabled in php by default so can cause issues if you move servers
You can give your array a name/id to associate them, just add it into the name attribute:
<input type="checkbox" name="measure[1][my_checkbox]" value="<?=$item->id?>">
<input class="item_mult" type="text" name="measure[1][my_text]" />

Categories