I am creating rows of HTML table like this
<?php foreach($products as $key=>$val) { ?>
<tr class="tr_entry">
<td><input class="input_entry" id="no" name="no" type="text"></td>
<td>
<select id = "stage" name="stage">
<option value="1">01</option>
<option value="2">02</option>
</select>
</td>
</tr>
<?php } ?>
and posting this form in a controller using "post" method. In controller I am using this
print_r($_POST);exit;
but I am getting only data of last row of table. Need help.
For all the iterations of for loop your input's name are same.For each iteration of for loop, old values are replaced by new values,so
at the end for loop you will get values of last tr of the table.
Use below code to get all the inputs in form.
<?php foreach($products as $key=>$val) { ?>
<tr class="tr_entry">
<td><input class="input_entry" id="no" name="no<?php echo $key ?>" type="text"></td>
<td>
<select id = "stage" name="stage<?php echo $key ?>">
<option value="1">01</option>
<option value="2">02</option>
</select>
</td>
</tr>
<?php } ?>
Related
I understand to call and update since rows using prepared statements but I'm having trouble understanding how to update multiple records.
I have a simple attendance register with a select box to define if they attended or not. I call the rows in a table format:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
<tbody>
<tr>
<th scope="col">Name</th>
<th scope="col">Attendance</th>
</tr>
<?php if($Event_list->rowCount() > 0) {
foreach ($Event_list->fetchAll() as $Event_list_row) { ?>
<tr>
<td>
<?php echo ucwords($Event_list_row['firstname'])." ".ucwords($Event_list_row['surname']); ?>
</td>
<td>
<input type="hidden" name="id" value="<?php echo $Event_list_row['booking_id']; ?>">
<select name="outcome">
<option value="0">Choose</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
<input type="submit" value="Update" name="update">
</form>
I thought I could just use foreach again to update each record, but clearly I'm doing something wrong:
if(isset($_POST["update"])) {
$Update_ID = $_POST["id"];
$Update_Outcome = $_POST["outcome"];
foreach($_POST['id'] as $count => $id) {
$Attendance_Update_SQL = "
UPDATE event_booking SET
`confirmed`= :outcome
WHERE `id`= :id";
$Attendance_Update = $dbconn->prepare($Attendance_Update_SQL);
$Attendance_Update->bindParam(':id', $Update_ID[$count], PDO::PARAM_STR);
$Attendance_Update->bindParam(':outcome', $Update_Outcome[$count], PDO::PARAM_STR);
if($Attendance_Update->execute()) {
// Add in success message?
}
}
}
I get the following error:
Warning: Invalid argument supplied for foreach()
relating to the foreach row
Quick Fix:
rename name="id" to name="id[]"
rename name="outcome" to name="outcome[]"
Note: this syntax and reliance on index is problematic when you add checkbox types to a form.
Ok, the biggest problem is your input names are going to conflict
eg <select name="outcome"> will only set the last records outcome from your form.
I would use var_dump($_POST) to understand your post data
Suggestion, rename your "name" attributes as name="record[id][property]"...
<select name="record[<?php echo $Event_list_row['booking_id']; ?>][outcome]">
<option value="0">Choose</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
You would then use
foreach( $_POST['record'] as $id => $data )
where $id and $data['outcome'] is now clearer to understand and use.
I need a solution for this. Here I want to validate form elements. I did it with PHP but what my problem is on clicking submit button validation process takes place but select box value changes to select dept.
FOR EXAMPLE, if you just click submit button without giving student id by only select dept.. the message shows enter studentid but select box value changes to select dept again as it page reloads first..
php:
if($_POST['submit']!='')
{
$c=0;
if($_POST['studid']=='' ) {
$msg_id="Enter stud id";
$c++;
}
if($_POST[studdept]=='') {
$msg_dept="Enter stud dept";
$c++;
}
if($c==0) {
echo "form has been submitted..";
}
}
HTML:
<form id="myform" action="" method="post">
<table>
<tr>
<td> Studid: *</td> <td> <input type="text" name="studid" maxlength="10" value="<?=$_POST[studid]?>"> </td> <td> <?php echo $msg_id; ?> </td> </tr>
<tr>
<td> StudDept: *</td>
<td>
<select name="studdept">
<option value="" selected="selected" >select dept</option>
<option value="cse" >cse</option>
<option value="eee" >eee</option>
<option value="mech" >mech</option>
<option value="ece" >ece</option>
</select>
</td>
<td><?php echo $msg_dept; ?> </td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"> </td>
</tr>
</table>
</form>
can someone help me here?
Thanks in Advance...
You have to generate your <option></option> list through loop (if you're generating Department list from Database Table) where you have to put condition which you had put it for first <option> pair.
Or You can put a Turnary Operator (if you're using static list).
<select name="studdept">
<option value="none" <?php echo ($_REQUEST['studdept']=='none'?'selected="selected"'):'';?> >select dept</option>
<option value="cse" <?php echo ($_REQUEST['studdept']=='cse'?'selected="selected"'):'';?> >cse</option>
<option value="eee" <?php echo ($_REQUEST['studdept']=='eee'?'selected="selected"'):'';?> >eee</option>
<option value="mech" <?php echo ($_REQUEST['studdept']=='mech'?'selected="selected"'):'';?> >mech</option>
<option value="ece" <?php echo ($_REQUEST['studdept']=='ece'?'selected="selected"');:''?> >ece</option>
</select>
And use $_REQUEST instead $_GET or $_POST.
The method of your form is POST and you are trying to get the department using GET <? echo ($_GET['studdept'] == 'cse' ? 'selected="selected"' : '') ?>.
Anyway, you'll need to improve your HTML so you check if any of the deptartments are selected (not just 'cse'). A for/while loop will do the job.
In addition, I would add " " to the post variables, as you did in if($_POST['studid']=='' ) (but you didn't in if($_POST[studdept]==''))
Your specifies that the form data will be submitted using the POST method.
The way that PHP does this is to store all the "posted" values into an associative array called "$_POST".
Be sure to take notice the names of the form data names, as they represent the keys in the "$_POST" associative array.
Use: $_POST['studdept'] instead of: $_GET['studdept']
Beacause you use method POST for send data and you are trying to get value using $_GET['studdept'].
so you have two ways:
(1) just change the method to GET or
(2) change $_GET['studdept'] to $_POST['studdept'].
I need an example that can let me populate the 2nd,3rd,...,7th dropdown list (all same entries) after the 1st dropdown list has been selected. I have all the values stored in an array in PHP before the HTML head.
I've looked at some examples, and couldnt find a specific solution
For example:
Let's say I have a warehouse full of franchisees and outlets under those aforementioned franchisees, I need to create order lists for 7 days ahead for that specific franchisee with n outlets. My solution was that the user/warehouse operator would pick a franchisee, then the 2nd (up to 7th) drop down list would have the outlets who are under that picked franchisee.
Thanks.
<?php
$productClass = new product();
$productClass->setProductList();
$franchiseeClass = new users();
$franchiseeClass->getAndSetAllFranchisees();
$franchiseeArray = $franchiseeClass->getUserList();
$operatorOutletClass = new users();
$operatorOutletClass->getAndSetUserByLevel("5");
?>
<html><head></head><body>
<div align="center">
<form name="BBOFranchisee" method="post" action="?" onSubmit="return checkSubmit()">
<table align="center" border="1">
<tr>
<td>
Franchisee:
</td>
<td colspan="8">
<select name="displayFranchisee" size="1" onChange="populateOutlet('findOutletByFranchisee.php?fid='+this.value)">
<option label="franchisees" value="0">--Choose Franchisee--</option>
<?php
for($i=0;$i<count($franchiseeArray);$i++)
{
foreach($franchiseeArray[$i] as $key => $val)
{
echo "<option label=\"franchisees\" value=\"$key\">$val</option>\n";
}
} ?>
</select>
</td>
</tr>
<tr>
<?php for($j=0;$j<7;$j++) { ?>
<td>
Outlet:
<select name="displayOutlet<?php echo $j; ?>" size="1">
<option label="outlets" value="0"> </option>
</select>
</td>
<?php } ?>
</tr>
</body>
</html>
I guess the continuation is the javascript part.. i tried to look at the solution that http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html provides, but it wont work with multiple drop downs.
In order to use that javascript example, you need to add an id to your 2nd-7th selects - id="displayOutlet<?php echo $j; ?>"
<?php for($j=0;$j<7;$j++) { ?>
<td>
Outlet:
<select name="displayOutlet<?php echo $j; ?>" id="displayOutlet<?php echo $j; ?>"size="1">
<option label="outlets" value="0"> </option>
</select>
</td>
<?php } ?>
Then in your javascript code, you can add a loop for the 2nd-7th selects.
if(req.status == 200) { // which represents ok status
for(var i=0;i<7;i++){
document.getElementById('displayOutlet'+i).innerHTML=req.responseText;
}
}
First of all I would like to say that I am very new to PHP so maybe it is a stupid question, but there we go:
I have a two dropdown select tags that have the following code in html and php:
<select>
<option value="default">---a---</option>
<?php for($i=1; $i<+100; $i++) { ?>
<option value="<?php $i ?>"><?php echo $i ?></option>
<?php } ?>
</select>
<select>
<option value="default">---b---</option>
<?php for($j=1; $j<=100; $j++) { ?>
<option value="<?php $j ?>"><?php echo $j ?></option>
<?php } ?>
</select>
Then I have a table with a row and a column (well at least that has to be changed I mean):
<table>
<tr>
<th>Date</th>
<th>Temp</th>
<th>Nr a</th>
<th colspan="2">a 1</th>
<!--another th___nr a-->
</tr>
<tr>
<td colspan="3"> </td>
<th>something</th>
<th>else</th>
<!--another something else___nr a-->
</tr>
<tr>
<td>
<?php
//echo date(DATE_RFC822);
echo date('jS\, F\, Y');
?>
</td>
<td>
<input type="number" name="temp" placeholder="temp" />
</td>
<th>b 1</th>
<td>
<input type="number" name="something" placeholder="something" />
</td>
<td>
<input type="number" name="else" placeholder="else" />
</td>
<!--another td___nr a-->
</tr>
<!--another tr___nr b-->
</table>
What I want to do is that with the first dropdown to control the number of a's (columns) and with the second dropdown the number of b's (rows).
Is this possible with PHP and if it is, how?
I was thinking of somehow to memorize the number we choose in the dropdown into a variable and with some for's to resolve it, but I don't really know how to do it, IF it is possible.
Thank you very much in advance for your time for helping me :)
PHP is a server side language, so you would have to post your (a) and (b) inputs back to the server then use PHP to construct the relevant data that could be used to generate the table you want on the page
<form action="index.php" method="post">
<!--put your select elements here-->
<select name="columns"></select>
<select name="rows"></select>
<input type="submit" />
</form>
Then in the top of the PHP file something like
<?php
// using terniary operator here - you can google that
// basically ensures the vars are set to 1 if there is no post from the form
$columns = (isset($_POST['columns'])) ? $_POST['columns'] : 1;
$rows = (isset($_POST['rows'])) ? $_POST['rows'] : 1;
?>
Then in your table
<table>
<?php foreach ($rows as $row):?>
<tr>
<?php foreach ($columns as $column):?>
<td></td>
<?php endforeach;?>
</tr>
<?php endforeach;?>
</table>
You could enhance this as Dieter suggested with Ajax, but for pure PHP you'll need something like this.
Its a very basic example though, and will need fleshing out to fulfil anything more than generating an empty HTML table with X columns and Y rows.
Edit: This is a completed file that should work. Just save the code below this line as test.php and try it out. Then you can amend it to do exactly what you need.
Sorry, I made a little mistake too, I forgot to convert the posted data into a range that could be used in a foreach loop. That's corrected below
<?php
// test.php
// using terniary operator here - you can google that
// basically ensures the vars are set to 1 if there is no post from the form
$c = (isset($_POST['columns'])) ? $_POST['columns'] : 1;
$r = (isset($_POST['rows'])) ? $_POST['rows'] : 1;
$columns = range(1, $c);
$rows = range(1, $r);
?>
<form action="test.php" method="post">
<!--put your select elements here-->
<select name="rows">
<option value="default">---a---</option>
<?php for($i=1; $i<+100; $i++) { ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php } ?>
</select>
<select name="columns">
<option value="default">---b---</option>
<?php for($j=1; $j<=100; $j++) { ?>
<option value="<?php echo $j ?>"><?php echo $j ?></option>
<?php } ?>
</select>
<input type="submit" />
</form>
<table>
<tr>
<?php foreach ($columns as $column):?>
<th>Temp Head</th>
<?php endforeach;?>
</tr>
<?php foreach ($rows as $row):?>
<tr>
<?php foreach ($columns as $column):?>
<td>Temp Data</td>
<?php endforeach;?>
</tr>
<?php endforeach;?>
</table>
I need to populate the relevant text box with the results of a Select drop down.
My Javascript so far is
<script language="JavaScript">
var mytextbox = document.getElementById('error');
var mydropdown = document.getElementById('errormsg_id');
mydropdown.onchange = function(){
mytextbox.value = this.value;
}
</script>
My select box and text boxes are built as arrays from a query so there is nultiple of them.
I need to be able to populate the corresponding text box with the results from the Select next to it.
Any ideas?
Thanks
<td width="1%">
<select style="width:100%" name="errormsg_id[]" id="errormsg_id[]">
<?php do { ?>
<option value="<?php echo $row_rserrormsg['errormsg_id']?>"
<?php if ($row_rsdates['errormsg_id'] == $row_rserrormsg['errormsg_id']) { echo("selected='selected'"); } ; ?> >
<?php echo $row_rserrormsg['error_message']?></option>
<?php } while ($row_rserrormsg = mysql_fetch_assoc($rserrormsg)); ?>
<?php mysql_data_seek($rserrormsg, 0);
$row_rserrormsg = mysql_fetch_assoc($rserrormsg);?>
</select>
</td>
<TD align="center" class="adminuser">
<input style="width:96%;text-align:left;" autocomplete="on" title="Enter Error Message" type="text" name="error[]" id="error[]" value="<?php echo $row_rsdates['error_message']; ?>" maxlength="100"/>
</TD>
Assuming You will be using jQuery and Your HTML is like this:
<table>
<tr>
<td><select class="my_select" name="errormsg_id1[]" id="errormsg_id1">...</select></td>
<td><input name="errormsg_id1_txt" id="errormsg_id1_txt" title="..." />
</tr>
...
<tr>
<td><select class="my_select" name="errormsg_idX[]" id="errormsg_idX">...</select></td>
<td><input name="errormsg_idX_txt" id="errormsg_idX_txt" title="..." />
</tr>
</table>
this could be done using jQuery:
$(document).ready(function(){
$('.my_select').change(function(){
$('#'+$(this).attr('id')+'_txt').val($(this).val());
});
});
Hope it is correct, have no time to test it...
Here is a JSFiddle/DEMO on how to get it to work with sample selects:
I think this approach is a little better than #shadyyx since it removed the need for you to manually increment the input and select field names.
<table>
<tr>
<td><select class="select" name="errormsg_id[]">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" title="..." />
</tr>
...
<tr>
<td><select class="select" name="errormsg_id[]">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" title="..." />
</tr>
</table>
And the JQuery:
$(document).ready(function(){
$('.select').change(function(){
$(this).parent().parent().find('.error_text').val($(this).val());
});
});
NOTICE: If you are using plain Javascript, see this DEMO
<table>
<tr>
<td><select class="select" name="errormsg_id[]" id="errormsg_id1" onChange="update('errormsg_id1')">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text"name="errormsg_id_txt[]" id="errormsg_id_txt1" title="..." />
</tr>
...
<tr>
<td><select class="select" name="errormsg_id[]" id="errormsg_id2" onChange="update('errormsg_id2')">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" id="errormsg_id_txt2" title="..." />
</tr>
</table>
And Javascript:
function update(element){
var e = document.getElementById(element);
var id = element.substring(element.length-1);
document.getElementById("errormsg_id_txt"+id).value = e.value;
}
You need to think of way to uniquely identify your corresponding set of elements. The most convenient way I would suggest would be to use the primary key value of the data row.
And example would be like
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id']; //A primary key field
echo '<input type="text" name="text'.$id.'" id="text'.$id.'" />';
echo '<select name="select'.$id.'" id="select'.$id.'" />...</select>";
}
Next, call a function from the select menu, so that only the relevant text box is updated. Update the above select portion to read something similar to this.
echo '<select name="select'.$id.'" id="select'.$id.'" onchange="updateBox(\'text'.$id.'\', this.value)" />...</select>";
Now, create a very simple simple function to update the text box.
function updateBox(element, value) {
el = document.getElementById(element);
el.value = value;
}