SOLVED
I found he issue, while I was creating new ROW I was accidently grabbing the tag and it was creating new form wrap every time I added the row.
So it ended up being:
form
Row 1
form 2
Row 2
form 3
Row 3
form END
I am trying to submit my form with dynamically added fields however I keep only getting the initial one.
SetUp
HTMLDOM
FORM<br/>
div->Item<br/>
--Input<br/>
--SelectOption<br/>
--TextArea<br/>
--RadioButton<br/>
div->Item(EndWrap)
I have jQuery Grab initial wrap into a variable, once I click ADD ROW elements get +1 count.
I have tried the following on form Submit:
foreach ($_POST['user'] as $key=>$value ) {
echo $key . " : " . $value;
}
<input id="user" type="text" name="user[]" autocomplete="off"/>
However that only shows me the user of the first ITEM -> Wrap or ROW.
My question would be what is the best way to iterate over dynamically added elements inside the form on submission so that I can add values to the DB.
Thank you!
Even a direction to a valid tutorial will be greatly appreciated (had no luck finding any that work).
Match your code with the following one:
<?php
if(isset($_POST['sub'])){
foreach ($_POST['user'] as $key=>$value ) {
echo $key . " : " . $value."<br />";
}
}
?>
<html>
<body>
<form method='POST'>
Val 1: <input id="user" type="text" name="user[]" autocomplete="off"/><br />
Val 2: <input id="user" type="text" name="user[]" autocomplete="off"/><br />
Val 3: <input id="user" type="text" name="user[]" autocomplete="off"/><br />
Val 4: <input id="user" type="text" name="user[]" autocomplete="off"/>
<input type="submit" name="sub"/>
</form>
</body>
</html>
This is working for me. Although, the ids must not be same for all fields but I am taking same as per your case.
Related
Is there anyway to save a textboxs as an array similar to how multiple checkboxs can be saved as an array? When you save multiple checkboxs to an array the value only comes up in the array when the checkbox has a check in it. Is this the same with textboxs? Do they only have value when they're not empty?
I'm trying to save these textboxs as an array (I only took the part of the form that you all need to see)
<form action="formemail.php" method="POST" name="rcr">
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="submit" name="sendwork" id="sendwork" value="Send Work Order"></td>
</form>
In the next page I'm trying to take these values and just output them in a simple way (later I'm going to be adding them to an email using foreach if I can)..... below is formemail.php
<?php
$description = $_POST["desc"];
echo $description[0];
echo $description[1];
?>
I can't get anything to echo for the life of me. And I'm not sure what I'm doing wrong.
tried ?
if( isset( $_POST["desc[]"] ) ) echo "is set ; )";
btw. same multiple id's can cause you problems if you work with JS/JQuery.
Try to use foreach and test submitted values:
foreach($_POST['desc'] as $post)
{
echo !empty($post) ? $post.'<br />' : 'An empty value.<br />';
}
Attempt 1: This shows what I want to achieve. The same array but different key and value pair.When printed, it list out all the keys and values.
<?php
$rental[]=array('day'=>1,'rate'=>10);
$rental[]=array('day'=>2,'rate'=>20);
$rental[]=array('day'=>3,'rate'=>30);
$rental[]=array('day'=>4,'rate'=>40);
print_r($rental);
?>
Attempt 2: Now, I'm using a single form to submit multiple times.I expect the key and values to be stored inside an array each time the form is submitted. But what happens is, the last value pair overwrites the previous one.Therefore only one pair is shown. So far, I can store them with the help of session. But I just wonder if there's a way to achieve it via php array on the same page?
<?php
$rental[]=array('day'=>$_POST['days'],'rate'=>$_POST['rental']);
print_r($rental);
?>
<form action="#" method="post">
Number of Days: <input type="text" name="days" value=""><br/>
Rental rate: <input type="text" name="rental" value=""><br/>
<input type="submit" name="add_rental_rate" value="Add Rental Rate">
</form>
You can use session to achieve what you want, see this code:
<?php
session_start();
if(isset($_POST['days']) && isset($_POST['rental'])){
$_SESSION['info'][] = array($_POST['days'] => $_POST['rental']);
}
if(isset($_SESSION['info'])) {
for($i = 0; $i < count($_SESSION['info']); $i++) {
foreach($_SESSION['info'][$i] as $days => $rental){
echo '<p>' . $days . '<br>';
echo $rental . '</p>';
}
}
}
?>
<form action="#" method="post">
Number of Days: <input type="text" name="days" value=""><br/>
Rental rate: <input type="text" name="rental" value=""><br/>
<input type="submit" name="add_rental_rate" value="Add Rental Rate">
</form>
You should read some docs about session:
http://php.net/manual/en/intro.session.php
This is my second code but the problem is I have 3 queries. So it only returns the last product_id when i Click update it always return product_id=3, but i want update the product_id=2
<form action="update_qty.php" method="POST">
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
<?php } ?>
</form>
Your problem is that the PHP is server side and you need something client side to read the value of the text box. You would need a page refresh to pass the text field value to the server so it could write it to the url in the anchor tag. Which is what the form submit would do, but as it would have submitted the value already the anchor tag would be pointless
To do it without a page refresh use Javascript. It would be easy to do with jQuery. You could add an event that writes whatever is entered in the text box the the anchor tags href as it is typed.
I'll do something more like this.
One form per product.In your case when you submit the form the qty value will always be the las found.
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<form action="update_qty.php" method="POST">
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
</form>
<?php } ?>
You can add more information like this
update
You can not get all values as like that because input name overwrite in every loop iteration.
For multiple values you can try in two ways like:
<?php
while($getorder = mysqli_fetch_array($order)){
$newArr[] = $getorder['price']."~".$getorder['product_id']."~". $ getorder['qty'];
} //while end
?>
<input type="hidden" name="allinputs" value="<?=$newArr?>">
Input field outside the loop.
In php explode array value with ~ and get the all values.
Other solution is that
Your input field name must be change like:
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price_<?=$getorder['product_id']?>">
<?php } ?>
Change field name in every iteration.
In current scenario either you need three different buttons or the best solution to use AJAX request .
update
On update_qty.php u can use like this
<?php echo $_GET['product_id'];?>
I am trying to get information from an html form and pass it to my php file as an array
here is a snippet of the code for my html form:
<form action ="upload.php" method="post">
Name<input id= "n" type="text" name="info[name]" /><br />
Address: <input id="a" type = "text" name="info[address]" /><br />
City: <input id="c" type = "text" name="info[city]" /><br />
</form>
then in my php file I tried to print the content:
$information = $_POST['info'];
echo $information['name'];
but nothing gets printed to the page
I don't think you can do that (shove it all into a separate array (apart from $_POST)) directly with your HTML. You'll have to do some extra PHP work at the beginning.
Firstly, for the love of all that is holy, make your HTML input names cleaner:
<form action ="upload.php" method="post">
Name<input id= "n" type="text" name="info_name" /><br />
Address: <input id="a" type = "text" name="info_address" /><br />
City: <input id="c" type = "text" name="info_city" /><br />
</form>
And now, for the PHP:
//this is how I would do it, simply because I don't like a bunch of if/elseifs everywhere..
//define all the keys (html input names) into a single array:
$infoKeys[0]='name';
$infoKeys[1]='address';
$infoKeys[2]='city';
//define your end array
$information=array();
//now loop through them all and if they're set, assign them to an array. Simple:
foreach ( $infoKeys as $val ){
if(isset($_POST['info_'.$val])){
$information[$val]=$_POST['info_'.$val];
}//end of isset
else{
$information[$val]=null;
}//end of no set (isset===false)
}//end of foreach
//now, when you want to add more input names, just add them to $inputKeys.
//If you used the if/elseif ways, your doc would be plastered in ifs and elseifs.
//So i personally think the looping through the array thing is neater and better.
//but, feel free to change it, as I have a feeling I'll have allot of critics because of this method.
// anyway, that should do it. The var $information should be an array of all your 'info_' html inputs....
Happy coding!
I have created a form, with 5 textbox fields and I want to add those five entries in the database. I want to use the textbox "array", that way I can use a for-each when saving to the database. As anyone, any code on how to do this or can direct me in the right path?
input type="text" value="whateva" name= ?php text[0] ?>
input type="text" value="whateva" name= ?php text[1] ?>
input type="text" value="whateva" name= ?php text[2] ?>
if (isset($_POST['Submit']) {
//add to db
(for-each $text as $val) {
//add to db
}
}
Is this possible?
HTML
<input type="text" value="whateva" name="text[]" />
<input type="text" value="whateva" name="text[]" />
<input type="text" value="whateva" name="text[]" />
PHP
if (!empty($_POST['text'])) {
foreach ($_POST['text'] AS $value) {
// add to the database
$sql = 'INSERT INTO tableName SET fieldName = "' . mysql_real_escape_string($value) . '"';
}
}
Yes, HTML supports arrays. just name your textareas like this:
<textarea name="field[]"></textarea> /* Notice square brackets */
For this example, in PHP, your $_GET or $_POST will have array key with name 'field' and values from these textareas.
If 'Submit' is the name of the submit button. yeah that will work.
but few suggestions:
correct it as:
< input type="text" value="whateva" name= "" />
Use validation for the text submitted by user
IMPORTANT: "GET A BOOK ON PHP" and learn it. Seriously, if you learn this way, you wont become a good programmer. You are learning it the hardway. Book is must for you.