Excuse me if this question is already asked everywhere, but i cant find answer that worked.
So I have a dynamically generated table, which is generated by selecting rows from a table in a database.
This is the view file that create the table.
all rows except the heading are created based on how many data found in the table in a database, so, all the inputs are have name like
<form class="form-horizontal" role="form" id="formDtKryw" method="POST" action="updKryw">
<div class="table-responsive form-group row">
<table class="table table-hover table-condensed table-bordered">
<thead class="text-center">
<tr>
<th>No</th>
<th>Nama</th>
<th>Telepon</th>
</tr>
</thead>
<tbody>
<?php $no = 0;
foreach ($dftr_crKryw as $data_crKryw):
$no_ktp[$no] = $data_crKryw->no_ktp;
?>
<input type="hidden" name="no_ktp[<?php echo $no; ?>]" id="no_ktp[<?php echo $no; ?>]" value="<?php echo $data_crKryw->no_ktp; ?>">
<tr>
<td><?php echo $no+1; ?></td>
<td><?php $this->db->select('nama,')->from('dt_prbd')->where('no_ktp', $data_crKryw->no_ktp);
$qry = $this->db->get();
if ($qry->num_rows() > 0) {
foreach($qry->result() as $data):
echo $data->nama;
endforeach;
}
?></td>
<td><?php
if ($data_crKryw->memo == NULL) {
?>
<select class="form-control input-sm" name="memo[<?php echo $no; ?>]" id="memo[<?php echo $no; ?>]">
<option value="">---Pilih---</option>
<option value="Memo 1">Memo 1</option>
<option value="Memo 2">Memo 2</option>
<option value="Memo 3">Memo 3</option>
</select>
<?php
}
else {
echo $data_crKryw->memo;
}
?></td>
</tr>
<?php
$no++;
endforeach;
}
?>
<input type="hidden" name="totData" id="totData" value="<?php echo $no; ?>">
<tr>
<td colspan="11" align="right"><button type="submit" id="submit" class="btn btn-primary">Simpan Data</button> <button type="reset" class="btn btn-danger">Hapus Form</button></td>
</tr>
</tbody>
</table>
</div>
</form>
This is the Controller that should catch the inputs
function updKryw(){
$totData = $this->input->post('totData');
$no_ktp = $this->input->post('no_ktp');
$memo = $this->input->post('memo');
$frmUpdKryw = array(
'totData' => $totData,
'no_ktp' => $no_ktp,
'memo' => $memo
);
$this->load->view('display_data', $frmUpdKryw);
}
And this is the view file that suppose to view all the captured information from the form
<html>
<head></head>
<body>
<?php
for($x = 0; $x < $totData; $x++){
echo $totData . "<br>";
echo $no_ktp[$x] . "<br>";
echo $memo[$x] . "<br>";
}
?>
</body>
</html>
All that displayed is only the $totData, and the $no_ktp[$x] and $memo[$x] is not display anything.
Anyone can help please if there is any error in my code or my logic.
Thanks in advance.
=========================================================================
edit: i started to think that my php engine is broken. why?
even this simple form doesn't display anything.
form.php
<form name="myForm" method="post" action="go.php">
<input type="text" name="name" value="firstname">
<input type="text" name="addr" value="firstaddr">
<button type="submit" name="submit" id="submit">save</button>
</form>
go.php
$name = $_POST[name];
$addr = $_POST[addr];
echo $name;
echo $addr;
First I recommend debug before saving data to $frmUpdKryw array
$this->input->post(NULL, TRUE); // returns all POST items with XSS filter
$this->input->post(); // returns all POST items without XSS filter
Then if data is not empty, before
$this->load->view('display_data', $frmUpdKryw);
add
var_dump($frmUpdKryw);
if all is good here, then to debug in beginning of the view
var_dump($totData);
var_dump($no_ktp);
var_dump($memo);
If all is good here then problem is in your loop, so you will need just fix it. Either you will know where you have problems:
failed with sending data with POST
failed with saving to new variable
failed with sending data to view
and last failed with loop
Hope it will help
when I solve another problem within the same app, this problem is fixed too, the problem is not in the code or logic. all codes are works as expected.
here are the link for the solution. not the solution that I expect, but its working.
The Solution
thanks to Abdulla and Oleg Sapishchuk
Related
I have a html table displayed using foreach loop in php. And I do even have buttons to be clicked in multiple rows. And the code goes like this:
<form method="post">
<table>
<tr> <th> Item </th> <th>Click to select</th></tr>
<?php
$query="select items from items_table";
$result=$con->query($query); //$con is connection variable already initialized
$row=mysqli_fetch_assoc($result);
foreach ($row as $index) //loop
{
?>
<tr>
<td><?php echo $index['items']; ?> </td>
<td><input type="button" value="select"> </td> //button here
</tr>
<?php } ?>
</table>
</form>
Now how can I get to know which button was pressed?
I have read some web pages, which says we need to be using AJAX, and I'm a newbie with no knowledge of how it works.. Please help me out!
I tried to have button inside a loop and expected that the buttons works correctly directly. But it gives wrong output.
If I got what u want I'd say you should handle your button or input
Some ways available
$index['items']
Is Not Correct where u used
<input value=<?php echo $row['id'] ?>
<input name=foo[<?php echo $row['id'] ?>] value=<?php echo $row['id'] ?> >
<input name='foo[]' value=<?php echo $row['id'] ?> >
Then handle them :
<?php
foreach($_REQUEST['foo'] as $name =>
$value){
echo $name ."posted and its value
is:". $value;
}
?>
OR
<?php
echo 'value of foo[1] Is '.$_REQUEST['foo[1]'] ;
?>
You can use FOR EXAMPLE $row['name'] Or any field name u have in your table intead of $row['id'] that I gave
So its been over 10 years since writing any code what so ever so i might be way off here. I have a table showing all students in a classroom populated by a foreach loop with an added box to input their grade. I need 1 submit button to insert one new row per student in the grades table in my database with their grade and the assignment name that is selected above.
I know i need to identify each grade value im just unsure how and it's only posting the person in the last row.
global $wpdb;
$asnames = $wpdb->get_results( "SELECT * FROM assignments" );
$results = $wpdb->get_results( "SELECT * FROM enrolled" );
?>
<div class="container"><center>
<form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
<table class="form-table" id='topper'>
<tbody>
<tr>
<th>Assignment Name</th>
</tr>
<tr>
<td>
<select name="Assignment">
<?php foreach($asnames as $asname) { ?>
<option value="<?php echo $asname->id ?>"><?php echo $asname->assign_name ?></option>
<?php
} ?>
</select>
</td>
</tr>
</tbody>
</table></center>
</div>
<br><br>
<div class="container"><center>
<form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
<table class="form-table" id='student'>
<tbody>
<tr>
<th>Students Name</th>
<th>Points</th>
</tr>
<?php foreach($results as $row){ ?>
<tr>
<td><?php echo "$row->student_name" ?></td>
<td><input type="text" class="regular-text" name="Grade"></td>
</tr>
<?php } ?>
<!-- </tbody> -->
</table>
<p><input type="submit" name="submitinsert" id="submitinsert" class="button button-primary" value="Submit"/></p> </form>
</center></div>
<?php
if ($_POST['submitinsert']){
$result = $wpdb->insert(
'grades',
array(
"student_name" => $row->student_name,
"assignment_id" => $asname->id ,
"grade" => $_POST['Grade']
)
);
}
I cant seem to get it to grab each row as it is in the table to insert, only the last row gets put in.
EDIT: I got the grade input worked out now just trying to get each loop put into the database with the single submit button.
My code is running a cycle that adds textareas where the user can input queries, which the div it is in also contains a dropdown with a list of servers available to run those queries in. At first it was working just fine for only the first iteration and not showing the list at all in the next dropdowns, but while I was messing with the code and pasted the query inside the cycle all dropdowns were filled but the server ID of the first textarea stopped being posted correctly, instead sending the value that's currently in the database. By other words it won't update properly.
<?php
if(mysqli_num_rows($result_query) > 0){
while($rowq = mysqli_fetch_assoc($result_query)){
$sql_servers = "SELECT id, name, address FROM servers ORDER BY id ASC";
$result_servers = mysqli_query($link, $sql_servers);
?>
<table>
<form name="formStep" method="post" action="">
<br>
<tr>
<textarea class="form-control scrollabletextbox" id="query<?php echo $rowq['step']?>" name="query<?php echo $rowq['step']?>"><?php echo $rowq['query'];?></textarea>
</tr>
<tr>
<td width="25%" style="vertical-align:middle;"><select id="server" name="server" class="form-control input-md">
<?php
if (mysqli_num_rows($result_servers) > 0) {
while($rows = mysqli_fetch_assoc($result_servers)){ ?>
<option value="<?php echo $rows["id"];?>" <?php if($rows['id']==$row_query2['id_server']) echo 'selected=\"selected\"' ?> ><?php echo $rows["name"];?></option>
<?php
}
}?>
</select>
</td>
<td style="padding:10px;"><input type="submit" name="submit" formaction="save.php?i=4&id=<?php echo $id; ?>&s=<?php echo $rowq['step'];?>" class="btn btn-block btn-primary" value="<?php echo $lableSave; ?>"></td>
</tr>
<?php
}
}?>
</form>
</table>
If I echo the server variable in the save.php it will not update for the new selected value in the dropdown, but it will work for all the subsequent iterations of the cycle.
Any way to solve this problem or the previous one before i started trying to hammer the code until it worked would be greatly appreciated.
i have a little problem with my program and not so experienced in php. I want to transfer an array of objects with a html form to my php file. First i will let you take a look on my code:
location.php (with executable forms)
<form action="action/add_items.php" method="POST">
<table border="1" width="20%">
<thead>
<th>
<?= $lang["location_task"][$location->getProperty("location_id")][$location_task->getProperty("location_task_id")]; ?>
</th>
</thead>
<tbody>
<?php
$location_task_items = $location_task->getProperty("location_task_items");
foreach ($location_task_items as $location_task_item) {
?>
<tr>
<td>
<?= $lang["item"][$location_task_item->getProperty("location_task_item_id")]; ?>
</td>
<td>
<?= $location_task_item->getProperty("location_task_item_value"); ?>
</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td>
<input type="hidden" value="<?php print_r($location_task_items); ?>" name="location_task_items"/>
<input type="submit" value="start"/>
</td>
</tr>
</tbody>
</table>
</form>
You can see that i only print the array in the input hidden value. Is that right?
add_items.php
$location_task_items = $_REQUEST["location_task_items"];
foreach($location_task_items as $location_task_item) {
if($Player_Has_ItemsClass->getObjectByIds($player_id, $location_task_item->getProperty("location_task_item_id")) == null) {
$player_has_items_attributes = array();
$player_has_items_attributes["player_has_items_player_id"] = $player_id;
$player_has_items_attributes["player_has_items_item_id"] = $location_task_item->getProperty("location_task_item_id");
$player_has_items_attributes["player_has_items_item_value"] = $location_task_item->getProperty("location_task_item_value");
$player_has_items = new Player_Has_Items($player_has_items_attributes);
$Player_Has_ItemsClass->insertObject($player_has_items);
} else {
}
}
I only get the array as string and this exception on the foreach in add_items.php:
Invalid argument supplied for foreach()
I also tried it with json_encode and json_decode:
print_r(json_encode($location_task_items))
json_decode($_REQUEST["location_task_items"]);
but only get (object attributes were public):
Call to undefined method stdClass::getProperty()
Thanks for you help :)
In your hidden field use json_encode and then in your destination use json_decode:
<input `type="hidden" value='<?php echo json_encode($location_task_items); ?>' name="location_task_items"/>`
You could do this
<input type="hidden" value="<?php echo serialize($location_task_items); ?>" name="location_task_items"/>
and then
$location_task_items = unserialize($_REQUEST["location_task_items"]);
Hello I am trying to make a survey using php and Mysql, with the code below I save every answer per questions, but When I tried to save comments_per_questions it did not save that field into the array it only save one comment.
How can I fix this function to save into the database Questions, Answers and Comments per questions?
Thanks in advance.
Database Structure
"Questions" (idquestion, question)
"Surveys" (idsurvey, idquestion, answers, comments_per_question, survey_number)
This part of code save the question and answers from the survey form.
public function NewSurveyMulti($answer = array())
{
if(!empty($answer)) {
foreach($answer as $questi => $value ) {
$this->MyDB->Write("INSERT INTO surveys (`idquestion`, `answers`,`comments_per_questions` )
VALUES(
'".$questi."',
'".$value[0]."',
'".$_POST["comment"]."')");
}
}
survey_form.php
<?php
// Fetch questions
$cuestionario = $con->Fetch("SELECT * FROM questions"); ?>
<form name="newDona" action="" method="post">
</table><?php
// Confirm there are questions being drawn from database
$numrows = (is_array($cuestionario))? count($cuestionario): 0;
if($numrows > 0) {
// Loop through questions
foreach($cuestionario as $row) { ?>
<tr>
<!-- Write the question -->
<td><?php echo $row["question"];?></td>
</tr>
<th>
<!-- Set the question id -->
<select name="answer[<?php echo $row['idquestion']; ?>][]">
<option value=""></option>
<option value="1">yes</option>
<option value="no">NO</option>
</select>
</th><?php
<th><textarea type="text" name="comment" maxlength="50" cols="130" rows="5"/ ></textarea></th>
} ?>
<tr>
<td colspan="5" align="center">
<input type="submit" name="send" id="send" value="SAVE" />
</td>
</tr>
</table>
</form>
<?php } ?>
Ok, now the question shows the comment field within the loop, do what you did with the select.
<textarea type="text" name="comment[<?php echo $row['idquestion']; ?>]" maxlength="50" cols="130" rows="5"/ >
and collect it in your loop with
$_POST["comment"][$questi]
ASIDE: You should prevent SQL injection as your code leaves your site vulnerable to attack.
Here's a working example :
<?php
echo '<form method="post">';
echo '<textarea name="comments[]" rows="8" cols="40"></textarea>';
echo '<textarea name="comments[]" rows="8" cols="40"></textarea>';
echo '<textarea name="comments[]" rows="8" cols="40"></textarea>';
echo '<button name="submit">Submit</button>';
echo '</form>';
if (isset($_POST['comments']))
{
var_dump($_POST['comments']);
}