basically i have a database, which contains a random amount of questions
i have then printed this out as a php table using a query. there is as many textboxes to input marks as there are questions in the database.
I want to create an array of my inputted data and then update the database with the marks in.
Here is my code
echo "<table border = '0' cellpadding ='10px'>";
echo "<tr>
<td> Question <td>Mark</td><td>Criteria</td>
<td>Feedback</td>
</tr>";
while ($row = mysql_fetch_array($result))
{
$question[]=$rows['question'];
echo "<tr>";
echo "<td>". $row['question']. "</td>";
echo "<td>" ."<input type = 'text' name = 'mark[]' size = '1' value = '0' id = 'mark'/>/". $row['maxMark'] . "</td>";
$maxMark[] = $row['maxMark'];
echo "<td>".$row['criteria']."</td>";
echo "<td>" . "<textarea name = 'feedback[]' id= 'feedback'>Enter Feedback here</textarea>". "</td>";
echo "</tr>";
}
echo "</table>";
echo "</tr>\n";
echo "</table>";
I am not sure how to create the array, with the marks you input. please help.
In short i want to populate an array with the marks i input
This is rather easy to be done. All you have to do is setting the attribute name of your input controls and add an index.
Example:
<input type="text" name="name[0]" /><input type="text" name="mark[0]" />
<input type="text" name="name[1]" /><input type="text" name="mark[1]" />
The post (or get) data your script receives will then contain an array instead of a single variable.
Related
I fetch data from mysql in php. List all items with checkbox and quantity (rqty) as input value where user can enter quantity. But I'm not able to relate checkbox with corresponding input value (rqty) in an array. If I select first checkbox and enter quantity it works fine but if select any other checkbox and enter quantity, it's not getting updated in database. My code is as below:
<?php
If (isset($_REQUEST['submit1'])!='') {
foreach($_POST['itm'] as $key=>$itm) {
$odate=date("Y-m-d H:i:s");
$rqty=$_POST['rqty'][$key];
$itm=$_POST['itm'][$key];
$sql1="insert into table (itemtype,itemname,qty ,retdate,reason) select itemtype, itemname, '$rqty', '$odate', 'Return' from stock where itemname='$itm';
mysql_query($sql1);
}
}
?>
<p>
<form action="" method="post" name="form1">
<?php
$query3 = "SELECT id, category, itemtype, itemname, qty, status FROM stock where location='godown'";
$comments3 = mysql_query($query3);
$qty = mysql_num_rows($comments3);
echo "Consumable Items: " . $qty;
print "<table class='blue'>
<thead>
<tr>
<td width=100>Return</td>
<td width=100>Category</td>
<td width=100>Item Type</td>
<td width=100>Item Name</td>
<td width=100>Available Qty.</td>
<td width=100>Return Qty.</td>
<td width=100>Status</td>
</tr>
</thead>";
while($row1 = mysql_fetch_assoc($comments3))
{
$it=$row1['itemname'];
print "<tbody>";
print "<tr>";
print "<td><input type='checkbox' name='itm[]' value='$it'></td>";
print "<td>" . $row1['category'] . "</td>";
print "<td>" . $row1['itemtype'] . "</td>";
print "<td>" . $row1['itemname'] . "</td>";
print "<td>" . $row1['qty'] . "</td>";
print "<td><input type='text' name='rqty[]' placeholder='Enter Qty' /></td>";
print "<td>" . $row1['status'] . "</td>";
print "</tr>";
print "</tbody>";
}
print "</table>";
?>
<input type='submit' name='submit1' value='Return' />
In inspect I can see that all rqty[] are getting posted with blank data. But in case of itm, only itm which is selected is getting posted which is correct.
enter image description here
Since I did not receive any solution for this, I changed the approach. Now handling each checkbox separately and not using any array. Added submit button in form list and posting required values to other php page where it's processed further.
That's because of your loop:
foreach($_POST['itm'] as $key=>$itm) {...
and input "itm" is checkbox: <input type='checkbox' name='itm[]' value='$it'></td>
For instance, if you have 5 checkboxes in the form-table but only 2 of them was checked - then you'll have only 2 items in array $_POST['itm'], not 5.
To fix this issue you need to loop through $_POST['rqty'] instead of $_POST['itm']
I have a table with 3 columns like so in index.php:
ID | StName | Inspect
_________________________________
1 Wisconsin INSPECT
2 Alabama INSPECT
3 Nebraska INSPECT
The right most column is a submit button which takes the user to a page called inspect.php. I want to send the corresponding ID column value to inspect.php when a user clicks the INSPECT button so that I can use that value inside inspect.php, how can I do this?
Currently I have:
//index.php
echo "<table border = 1>
<tr>
<td>ID</td>
<td>StName</td>
</tr>";
// go into a loop to create more rows -- omitted
echo "<tr>";
echo "<td>" . $resultArr[0] . "</td>";
echo "<td>" . $resultArr[1] . "</td>";
echo "<td> <form action='inspect.php' method='get'>
<input type='Submit' value='Inspect'>
</form>
</td>";
echo "</tr>";
echo "</table>";
$resultArr[0] contains the ID value I want to send to inspect.php
So add an input field inside the form with a name attribute which will be an index key name on inspect.php page
<form action='inspect.php' method='get'>
<input type='hidden' name="id" value='<?php echo $resultArr[0]; ?>'>
<input type='submit' value='Inspect'>
</form>
A better and simple way to go for this is without a submit, just use a hyperlink like
Inspect
And later process this id on inspect.php page
I think I got what you wanted to work. I am using different data in the array, but you should be able to change to your needs anyway.
On index.php:
$resultArr = array("dave","fff","erere");
echo "<table border = 1>
<tr>
<td>ID</td>
<td>StName</td>
</tr>";
// go into a loop to create more rows -- omitted
echo "<tr>";
echo "<td>" . $resultArr[0] . "</td>";
echo "<td>" . $resultArr[1] . "</td>";
echo "<td> <form action='inspect.php' method='get'>
<input type='hidden' name='id' value=' $resultArr[0] '>
<input type='submit' name='inspect' value='Inspect'> </td>";
echo "</tr>";
echo "</table>";
And on inspect.php:
if( $_GET['inspect']){
$name = $_GET['id'];
echo $name;
}
This outputs the value of $resultArr[0], which I think you wanted.
Hey Guys, I have a question for you.
Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could
compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><form><input method='post' type='number'></form></td>";
echo "</tr>";
}
echo "</table>";
This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned?
The primary key is the week - so this would be the one to relate to.
Hope you understand what my problem is:)
To do this you would use a hidden input field.
When you echo each row, and the form in that row, you would simply add an extra line:
`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
In full, your code would be:
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>
<form>
<input method='post' type='number'>
<input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
I think there should be some modifications that has to be done in loop.
echo "<td><form method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";
This code adds a submit button to each row. But, this shouldn't be what I think.
It should be rather this way,
echo "<form method='post'> ";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><input type='number' value='".$rows['col_name']."'></td>";
echo "</tr>";
}
echo "<input type='submit' ></form>";
Or make the input field look like this.
'<input type="text" name="row['.$row['id_column'].'][miles]" />';
It will return you an array when you post it.
foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}
I have a simple DB and a small table in it. This table named STOCK has only one column - names of different items in a stationery.
I am dynamically generating a form in PHP-MySQL which fetches Item from STOCK (such as Pencil, Pen etc.) and displays them by using echo() function. So I have generated a form which displays Item and correspondingly an <input> text box. Upon submitting this form, how can I access all the Names and their text inputs by traversing through the $_GET array?
The dynamically generated form is:
$res = mysql_query("SELECT * FROM Stock");
echo "<form name='input' action='add_stock_later.php' method='get'>";
echo "<table align='left' border='1'>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Remarks</>
</tr>";
while($row = mysql_fetch_array($res))
{
$item = $row['Item'];
echo "<tr>";
echo "<td>" . $item . "</td>";
echo "<td><input type='text' name='$item'></td>";
echo "<td><input type='text' name='Remarks'></td>";
echo "</tr>";
}
echo "<tr><td></td>
<td align='middle'><input type='submit' value='Submit'></td></tr>";
echo "</table>";
Now in the add_stock_later.php, how can I access all these $item and it's corresponding input without having to manually do it using $_GET['Pencil'], $_GET['Pen'] and so on.
First edit your form.
echo "<td><input type='text' name='".$item."[quantity]'></td>";
echo "<td><input type='text' name='".$item."[remarks]'></td>";
Then use foreach().
foreach ($_GET as $stationery => $info) {
$stationery; // name of the stationery
$info["quantity"]; // quantity of the stationery
$info["remarks"]; // remarks for the stationery
}
Something like so, add to it of course.
$required = array('pencil', 'pen', 'paper', 'staples');
foreach ($required as $getKey){
if (isset($_GET[$getKey]) && $_GET[$getKey] !=''){
$$getKey = trim($_GET[$getKey]);
}
}
This turns $_GET['pencil'] into $pencil and you don't need to worry about knuckleheads trying to inject garbage.
I am creating a web survey to collect data for a grad project/paper and am collecting the data into a MySQL database. What the webpage is supposed to do is the following: Respondents will see a word and then have to make one of four choices. Three choices are radio buttons and the fourth choice is "other" where they will have an opportunity to enter something into a text box. This seems to be a fairly typical set-up, but I am having trouble on the sql end of things.
The problem is that, although the web page seems to function properly, when I look at the database, none of the radio button values are present-- the only values that are passed to the sql server are those entered into a text box (including blank values where a radio button was checked and nothing was entered into the text box.
The code for creating the survey page is:
echo "<form action='insert2.php' method='post'>";
echo " <table border='1' cellpadding = '10' align = 'center'>
<tr>
<th>Num</th>
<th>Français</th>
<th>Choix 1</th>
<th>Choix 2</th>
<th>Choix 3</th>
<th>Choix 4</th>
</tr>";
for ($i=1; $i<=$total; $i++)
{
$query = "SELECT * FROM MyDataTable WHERE id = '$i'";
$word = $db->query($query);
$word = $word->fetch();
echo "<tr>";
echo "<td>" . "$i " . "</td>";
echo "<td width = '300'>" . $word[1] . "</td>";
echo "<td width = '300' align = 'center'>" .
" <input type='radio' name='word$i' value='one' id='wa_$i' /> " . $word[2] . "</td>";
echo "<td width = '300' align = 'center'>" .
" <input type='radio' name='word$i' value='two' id='wb_$i' /> " . $word[3] . "</td>";
echo "<td width = '300' align = 'center'>" .
" <input type='radio' name='word$i' value='three' id='wc_$i' /> " . $word[4] . "</td>";
echo "<td width = '300' align = 'center'>" .
" autre: <input type='text' name='word$i' id='wd_$i' /> </td>";
echo "</tr>";
}
echo "<br /><br />";
echo "</table>";
Everything works fine when I don't have the final form field (the textbox). Is it not possible to EITHER a radio button value OR a textbox value?
The 'insert2.php' code is as follows:
mysql_query("INSERT INTO `MyResultsTable` (w1) VALUES ('$_POST[word1]')");
$last_id = mysql_insert_id();
for ($i=2; $i<=$total; $i++)// This loop inserts the rest of the data into the DB
{
$temp = "word$i";
if (isset($_POST[$temp]))
{
mysql_query("UPDATE `MyResultsTable` SET w$i='$_POST[$temp]' WHERE id = $last_id");
}
Any help would be greatly appreciated. I'm new at this and have searched for a long time, but to no avail.
Regards,
Matt
Change your radios to
<input type='radio' name='word$i[]' value='one' id='wa_$i' />
<input type='radio' name='word$i[]' value='two' id='wb_$i' />
<input type='radio' name='word$i[]' value='three' id='wc_$i' />
(Notice the array brackets after the value in the "name" property)
And see if that fixes it.