I have a set of inputs - 4 questions which are each arrays (could range from 1-100 keys) - they may or may not start at 1 (may be like 95,96,97,98...)
I need to simultaneously pull the input from all 4 questions for each key 1 at a time, unfortunately I can not quite figure out how...
I am familiar with the foreach statement and I think it will probably be my best bet:
here is what I have
<textarea name="question[98]" rows="3" cols="60"></textarea>
<select name="anstype[98]">
<option value="break">Section Title</option>
...more options
</select>
<input name="d_on[98]" type="text" size="10">
<input name="a_d_on[98]" type="text" size="10">
the next set of inputs could be
<textarea name="question[99]" rows="3" cols="60"></textarea>
<select name="anstype[99]">
<option value="break">Section Title</option>
...more options
</select>
<input name="d_on[99]" type="text" size="10">
<input name="a_d_on[99]" type="text" size="10">
ideally I need to get these into a mysql insert statements
$insquery = "INSERT INTO questions (question, anstype, d_on, a_d_on) VALUES('$_POST['question[98]']', '$_POST['anstype[98]']', '$_POST['d_on[98]']', '$_POST['a_d_on[98]']') ";
again I cant know what the key is going to start at, any help is appreciated
foreach($_POST["question"] as $key=>$value)
{
$question=$value;
$anstype=$_POST["anstype"][$key];
$d_on=$_POST["d_on"][$key];
$a_d_on=$_POST["a_d_on"][$key];
// Run your query here for one complete entry and it will repeat with loop
}
You need to use the key like this:
$_POST['question'][]
In your case, for example: 98: $_POST['question'][98], but its better to iterate over it.
Related
I am trying to pass 2 arrays in a MYSQL table using HTML array, I want to insert both values in the same row at the same time of the loop, of course nested loop isn't going to work, the first input value passed successfully, but the second input inserts wrong & unrelated values. I am sure it's because the for loop logic is incomplete, but I can't seem to adjust properly. any help will be appreciated.
HTML (...html code inside PHP then this, $row['id'] is the value that shall be passed to POST):
<input type="text" id="mytextbox" name="comment[]" placeholder = "Add your comments here" required>
<input type="text" id="mytextbox" list="decision[]" name="decision" placeholder = "Choose your decision" required>
<datalist id="decision[]">';
echo '<option value="'.htmlspecialchars($row['id']).'">'.htmlspecialchars($row['name']).'</option>';
<input type="text" id="mytextbox" name="comment[]" placeholder = "Add your comments here" required>
<input type="text" id="mytextbox" list="decision[]" name="decision" placeholder = "Choose your decision" required>
<datalist id="decision[]">';
echo '<option value="'.htmlspecialchars($row['id']).'">'.htmlspecialchars($row['name']).'</option>';
<input type="text" id="mytextbox" name="comment[]" placeholder = "Add your comments here" required>
<input type="text" id="mytextbox" list="decision[]" name="decision" placeholder = "Choose your decision" required>
<datalist id="decision[]">';
echo '<option value="'.htmlspecialchars($row['id']).'">'.htmlspecialchars($row['name']).'</option>';
PHP (after successful POST of $comment as input(1) & $decision as input(2) and working queries):
for ($i=0;$i<count($comment);$i++){
$query = "INSERT INTO table (otherid,col1,col2) VALUES ('$otherid','$comment[$i]','$decision[$i]')";
$result = $dbc->query($query);
}
You are getting only the last decision, because you did not use square brackets in the field name, as you did with the comments field. name="decision" needs to be name="decision[]". Only then will PHP create an array out of multiple passed parameters of the same name; without square brackets, they simply overwrite each other.
The duplicate IDs are only of client-side importance - selecting from those lists, will likely not populate the correct input field, but it has little to do with what actually gets submitted, if you filled those fields by hand. But you should be able to make thos IDs dynamic, for example by appending the row ID.
<datalist id="decision-123">, with a matching list="decision-123" on the input field.
I have looked everywhere so I apologize if there's an answer to this already, I couldn't find it after hours of searching. Maybe it's not a good way to do it either...
I have a form that has "picks" of a litter, so 1st pick, 2nd pick, 3rd pick, etc. These picks also have genders (Female, Male)
I use a foreach query to grab the available pick spots. If someone isn't assigned to that pick spot, then the input field is enabled for manual typing
Within that foreach query, I need to somehow submit what pick it is and what gender, as well as the typed value, so I can find the pick and store it to the database... The other problem is, it's dynamic where someone may type in 3 input fields, or other times just 1 input field. Basically it'll show 3 input fields and the user can choose to use all 3 or just 1...
No matter what I've tried, it doesn't work. I don't even have my examples because I've tried everything I could find and had to scrap it all
The input, this is for males only but females are the same:
<?php
foreach ($malespickedquery as $pick){
$picktaken=$pick['user_id'];
$pickcustom=$pick['custom_name'];
$pickposition=$pick['pick_id'];
$suffix=ordinal($pickposition);
?>
<div class="form-row pb-3">
<div class="col-12">
<label class="control-label"><?php echo $pickposition.$suffix." Pick "; ?>Male:</label>
<?php if($pickcustom==true){ ?>
<input type="text" name="" class="form-control" value="<?php echo $pickcustom; ?>" />
<?php }elseif($picktaken!="0"){ ?>
<input type="text" name="" class="form-control" value="<?php echo $pick['public_name']; ?>" disabled />
<?php }else{ ?>
<input type="text" name="malepicks[<?php echo $pickposition; ?>][male][]" class="form-control" value="" />
<?php } ?>
</div>
</div>
<?php } ?>
I've tried foreach within foreach, but can never get all information. Most things just return the last input field as well... So if 3 of them are generated, the last input field would come through but not the others. I also did something with foreach where it returned ALL of the fields, even blank ones, which just got confusing and still didn't work right
Thank you in advance!
I need to update multiple records into a database on submit of a form. The form fields are repeating showing all the values but, the code doesn't seem to do anything. When someone hits submit, for each dataID presented in the hidden field, the progress and last_modified_date fields should be updated. I have googled and can't seem to find an answer anywhere. Any help would be appreciated. Thanks
Here is the update code:
if(isset($_POST['dataID'])){
foreach($_POST['dataID'] as $updateid){
$progress = $_POST['progress_'.$updateid];
$last_modified_date = $_POST['last_modified_date_'.$updateid];
$updateUser = "UPDATE data SET
progress='".$progress."',last_modified_date='".$last_modified_date."'
WHERE dataID=".$updateid;
mysqli_query($sdpc_i,$updateUser);
}
}
}
?><form method="post" name="form1" enctype="multipart/form-data">
<select title="progress" name="progress[]" id="progress" class="form-control form-control-sm-3">
<option selected value="">Select One</option>
<option value="Contract Sent">Contract Sent</option>
<option value="Approved">Approved</option>
<option value="With Legal">With Legal</option>
<option value="Declined">Declined</option>
<option value="Vendor Unresponsive">Vendor Unresponsive</option>
<option value="Approved/No Data Collected">Approved/No Data Collected</option>
</select>
<?php
while(!$district_results_private->atEnd()) {
?>
<input name="dataID[]" type="text" id="dataID" value="<?php echo($district_results_private->getColumnVal("dataID")); ?>" />
<?php
$district_results_private->moveNext();
}
$district_results_private->moveFirst(); //return RS to first record
?>
<input name="last_modified_date[]" type="hidden" id="last_modified_date" value="<?php echo date('Y-m-d'); ?>" />
<input type="image" src="images/save.png" name="submit" id="submit" alt="Save" />
</form>
--------------------------------------------
UPDATE:
Thank you so much for your help! I did what you said and I am still getting these errors.
Notice: Undefined index: progress in /var/www/html/progress_workflow_multiple3.php on line 97
Notice: Undefined index: dataID in /var/www/html/progress_workflow_multiple3.php on line 98
Warning: Invalid argument supplied for foreach() in /var/www/html/progress_workflow_multiple3.php on line 98
Here is the code I have:
<?php
$stmt = mysqli_stmt_init($sdpc_i);
mysqli_stmt_prepare($stmt, "UPDATE data SET progress = ?, last_modified_date = CURDATE() WHERE dataID = ?");
mysqli_stmt_bind_param($stmt, 'si', $progress, $dataID);
$progress = $_POST['progress'];
foreach ($_POST['dataID'] as $dataID) {
mysqli_stmt_execute($stmt);
}
?>
Here is the form code:
<label for="progress"></label>
<span class="small_links">
<select title="progress" name="progress" id="progress" class="form-control form-control-sm-3">
<option value="">Select One</option>
<option>Contract Sent</option>
<option>Approved</option>
<option>With Legal</option>
<option>Declined</option>
<option>Vendor Unresponsive</option>
<option>Approved/No Data Collected</option>
</select>
</span>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<?php
while(!$district_results_private->atEnd()) {
?>
<input name="dataID[]" type="text" value="<?php echo $district_results_private->getColumnVal("dataID"); ?>" />
<?php
$district_results_private->moveNext();
}
$district_results_private->moveFirst(); //return RS to first record
?>
</p>
</div>
</div>
<p> </p>
<p>
<input type="image" src="images/save.png" name="submit" id="submit" alt="Save" />
</form>
Your form has 3 essential parts (excluding the submit of course) for the user to interact with:
<select id="progress"> ...(progress field)
<input id="dataID"> ...(dataId fields) this may occur multiple times
<input id="last_modified_date"> ...(lastMod field)
The progress field will only occur once, so there is no benefit in structuring its data as an array. Furthermore, a select field will default to its first option, so there is no need for selected on the first option. Also, when the value value of an option is exactly the same as the option's text, you can safely omit the value attribute.
<select title="progress" name="progress" class="form-control form-control-sm-3">
<option value="">Select One</option>
<option>Contract Sent</option>
<option>Approved</option>
<option>With Legal</option>
<option>Declined</option>
<option>Vendor Unresponsive</option>
<option>Approved/No Data Collected</option>
</select>
The dataId fields are rightly given an array-type name. However, it is inappropriate to assign multiple elements with the same id attribute. If you application is not using the id attributes (for any fields), just omit the declaration(s). If you need to assign unique id attributes, you will need to provide an incrementing counter and append that counter to the end of the id value.
while(!$district_results_private->atEnd()) {
?>
<input name="dataID[]" type="text" value="<?php echo $district_results_private->getColumnVal("dataID"); ?>" />
<?php
$district_results_private->moveNext();
}
The lastMod field can be completely omitted from the document. You are always passing the current date with the submission and don't want the user fiddling with it -- good news, you can hardcode that directly into your sql and the user won't be able to touch it.
As for the UPDATE queries, best practice indicates that you should be implementing a prepared statement and binding dynamic values to it in a loop for security and stability.
(Untested -- I never use procedural syntax)
$stmt = mysqli_stmt_init($sdpc_i);
mysqli_stmt_prepare($stmt, "UPDATE data SET progress = ?, last_modified_date = CURDATE() WHERE dataID = ?");
mysqli_stmt_bind_param($stmt, 'si', $progress, $dataID);
$progress = $_POST['progress'];
foreach ($_POST['dataID'] as $dataID) {
mysqli_stmt_execute($stmt);
}
p.s. I hope that data isn't your real table name -- because it is bad/imprecise/non-descriptive. Name it something intuitive/logical/expressive.
I have three inputs type text in an HTML page and a button which if clicked duplicate each text box (Javascript) making them 6.
<input type="text" name="category[]">
<input type="text" name="quantity[]">
<input type="text" name="amount[]">
<button>Add more</button>
Which generate same inputs again:
<input type="text" name="category[]">
<input type="text" name="quantity[]">
<input type="text" name="amount[]">
A piece of code in Cakephp I have been trying:
$data = $this->request->data;
foreach($data['category'] as $index => $value){
$this->ModelName->save($value);
}
Trying to get two rows inserted at once with quantity, category and amount as columns. But it is not inserting and not giving any error.
Is there a way I can achieve this?
Thanks.
I'm not sure how your model works in cakephp, but you should be able to get a complete grouping of data like:
foreach($data['category'] as $index => $value){
$category = $value
$quantity = $data['quantity'][$index];
$amount = $data['amount'][$index];
// use the above 3 variables however you need to to persist the model
//$this->ModelName->save($value);
}
On a side note, you may want to consider reordering your html inputs to be like:
<input type="text" name="item[0][category]">
<input type="text" name="item[0][quantity]">
<input type="text" name="item[0][amount]">
And then maintain the next index, incrementing the numeric index of item for each additional group
This will allow you to iterate like:
foreach($data['item'] as $index => $group){
//$group['category'];
//$group['quantity'];
//$group['amount'];
}
Hi gladly I want to put the output of my rows from a table in on single text field. This is what I currently have in my view:
{{Form::label('tag', 'tags')}}
#foreach ($task->tagtask as $tt)
<input type="text" name="tag_name" class="form-control" value='{{ $tt->tag['tag_name'] }}'>
#endforeach
The problem is if I have a task with for example 2 tags. Then its going to loop two times and then I have two textfields with each a value of a single tag. I would like to have one single textfield with all the tags that a task has as a value. But I really don't know how to achieve this.
Can someone help me, please? Gladly I'm waiting for your response. Anyway thanks for your answer.
You can use #foreach inside the value attribute:
{{Form::label('tag', 'tags')}}
<input type="text" name="tag_name" class="form-control" value='#foreach ($task->tagtask as $tt) {{ $tt->tag['tag_name'] }} #endforeach'>
You can always use a little of pure PHP.
{{Form::label('tag', 'tags')}}
<input type="text" name="tag_name" class="form-control" value="<?php foreach($task->tagtask as $tt) echo $tt->tag['tag_name'];?>">