Codeigniter - Appended Array items lost when post to controller - php

I have a view page
$nameArray = array('name' => 'Formname', 'id' => 'Formname');
echo form_open_multipart('controller/file_upload_function',$nameArray);
<table id="companytable" >
<tr><td><input type="file" id="dummyfile" name="dummyfile"></td></tr>
<tr>
<td >
<label id="addlabel" style="cursor: pointer;">
<i class="fa fa-plus-circle" aria-hidden="true"></i></label>
</td>
<tr>
<td>
<input type="text" name="companyname[]" class="cmp_textbx" placeholder="Enter Company">
</td>
</tr>
<tr>
<td>
<input type="submit" id="btn_upload" name="btn_upload" value="Save">
</td>
</tr>
</table>
<?php echo form_close(); ?>
Using Jquery I need to append more 'companyname' fields according to user(On cliking 'add company' label).'companytable' is the id of the table.
$("#addlabel").click(function(){
$("#companytable").append('<tr><td><input type="text" name="companyname[]" class="cmp_textbx" placeholder="Enter Company"></td></tr>');
});
The problem is when I try to submit my view and try to get the array items 'companyname'.
$company_items = array();
$company_items = $this->input->post('companyname');
print_r($company_items);
I am able to get only the first item of the array that is already given the view page.The appended array item is lost.Can somebody help me, please?

Your HTML code works fine. The problem is from jquery or add button. if you are using html button tag for that, then you have to use preventDefault() in the jquery like
$('.add').click(function(e){
e.preventDefault();
$("#companytable").append('<tr><td><input type="text" name="companyname[]" class="cmp_textbx" placeholder="Enter Company"></td></tr>');
});
or you can use input while selecting type as button, for this you don't need any changes for your code.

Related

Get value of a table data on button click

I am building a online exam application, here paper name and no. of papers are retrieved from database. Now I want to get the paper code of that paper for which I clicked the start button. Code for the table is here:
<form method="post" action="exam_page.php" >
<table >
<tr style="background-color: #7F859E;color:white; height:50px;">
<th style="padding-left:140px; width:550px;">Paper</th>
<th style="padding-left:40px;">Time</th>
<th style="padding-left:40px;">Duration</th>
<th style="padding-left:40px; width:250px;"></th>
</tr>
<?php
$i=1;
while($row=mysql_fetch_array($rs)){?>
<tr style="height:80px; background-color: #CCCCCC;">
<td style="padding-left:40px;">
<input type="text" value="<?=$row['paper_code']?>" name="paper_code<?=$i?>" readonly><?=$row['paper_name']?>
</td>
<td style="padding-left:40px;">
<input type="text" value="<?=$row['time']?>" readonly style="width:90px;">
</td>
<td style="padding-left:40px;">
<input type="text" value="<?=$row['duration']?> Min" readonly style="width:90px;">
</td>
<td style="padding-left:40px;"><button style="width:100px;">Start</button></td>
</tr>
<?php $i++; } $_SESSION['exam']=$i; ?>
</table>
</form>
Name your submit button, (also make it a submit type) and assign the paper code to its value attribute.
<button type="submit" style="width:100px;" name="clicked" value="<?=$row['paper_code']?>">
Start
</button>
Now, in exam_page.php you can get the value of the clicked button from $_POST['clicked']. (Or whatever you decide to name it.)
To get the values from the other inputs associated with the button you clicked, you can add the paper code to their names instead of using $i.
<input type="text" value="<?=$row['time']?>" name="time[<?=$row['paper_code']?>]">
and in exam_page.php you can get the value from $_POST['time'][$_POST['clicked']], etc.
If they aren't intended to be editable in your form, though, I would recommend using something else to display them and just loading them from the database in exam_page.php instead. Otherwise, your users will be able to override the readonly attribute and submit different values.
Try using javascript onclick functions:
<script>
function getPaperCode(paperCode)
{
alert(paperCode);
}
</script>
Then edit your input add onclick event:
<input type="text" value="<?php echo $row['paper_code']; ?>" onclick="getPaperCode('<?php echo $row["paper_code"]; ?>');" name="paper_code<?php echo $i; ?>" readonly><?=$row['paper_name']?>
Once you click. it will alert the value of the button
passed your unique id value or examcode via hidden field like this
<input type="hidden" name="id" value="<?=$row['eid']?>">
and on button click perform query like
$id=$_POST['id'];
select * from table_name where id='$id';

Reset button not working in html (php)

I have placed my input type reset button inside form but its still not working.
<form method='post' action='process/update_news_action.php' >
<tr>
<td colspan='2' class="col-md-4">Update News Content</td>
</tr>
<tr >
<td colspan='2' class="col-md-8"><textarea name="news" id="text"><?php echo $rows["news"] ; ?></textarea></td>
</tr>
<tr>
<td class="col-md-4"><input name='submit' type="submit" value='Publish' class="btn btn-success" /></td>
<td class="col-md-4"><input name='reset' type="reset" value='Reset' class="btn btn-primary" /></td>
</tr>
</form>
I have also tried <button type="reset" value="Reset">Reset</button>
If you're expecting the reset button to empty out the form, that's not the way reset works. It will reset the form fields to what they were when the page was loaded. In this case, you have pre-filled content in your form, so clicking reset will simply undo any user changes since the page-load.
To reset all form fields to blank/nothing, you will need javascript. First get the form DOM object, then iterate over all the input types you want to reset, and set each field.value = '' (input text types / textarea), or modify attributes (for select, radio, checkbox etc.) to deselect and so on. Turn that into a function (there's probably a ready piece somewhere out there, I seem to have lost mine), and attach it to your reset button's click event -- or your form's reset event.
Edit: On a quick search, here's a basic example of how to clear a form to empty values.
Edit: If you're not concerned over anything but text fields, here's a simple way to do this. We add a "reset" event listener to the form, and when it fires, all fields are set to empty string.
function setFormCleanReset(formId) {
let formEl = document.querySelector(formId);
// Add event listener for reset event
formEl.addEventListener('reset', function(e) {
// Iterate all non-hidden fields, set values to ''
for(const fieldEl of formEl.querySelectorAll('input:not([type=hidden])')) {
// #todo check input type and handle "select" etc.
fieldEl.setAttribute('value', '');
}
});
}
// usage: setFormCleanReset('my_form_id');
I think you forgot to insert form tag in your html. It should be work if you insert button code into <form>..button code here..</form>.Something like this:
<form>
<input type="text">
<td class="col-md-4">
<input name='reset' type="reset" value='Reset' class="btn btn-primary" />
</td>
</form>
You should place the <input type="reset"> in a form like this,it will definitely work
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Tring Reset</title>
<style>
</style>
</head>
<body>
<form>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type=reset></input>
</form>
</body>
</html>
You just need to put your reset button inside form. In following code I have taken one input box also an an example. I hope it helps.
<form>
<input type="text" />
<table>
<td class="col-md-4">
<input name='reset' type="reset" value='Reset' class="btn btn-primary" />
</td>
</table>
</form>
can you please try without echo anything inside textarea
try this
<form method='post' action='process/update_news_action.php' >
<tr>
<td colspan='2' class="col-md-4">Update News Content</td>
</tr>
<tr >
<td colspan='2' class="col-md-8"><textarea name="news" id="text"></textarea></td>
</tr>
<tr>
<td class="col-md-4"><input name='submit' type="submit" value='Publish' class="btn btn-success" /> </td>
<td class="col-md-4"><input name='reset' type="reset" value='Reset' class="btn btn-primary" /> </td>
</tr>
</form>
I think reset button is working fine.
Excuse Me! You are using default value in input attribute. Remove default value from it. Then type anything then click rest it will make your text box empty.
I have a form submitting variables to another PHP file. In the second file, there is a button that leads back to the first file for eventual correction od submitted values. The values are retained in the form by $_POST. The "Reset" button works only when the form is used the first time (empty). After submitting and return from the second PHP file, the "Reset" doesn't do anything, no values are cleared. The button seems to be fully inactive.
<button title="<?php echo $lang['reset-button-hint']; ?>" alt="TOOLTIP 1" type="reset" name="reset"><?php echo $lang['reset-button']; ?></button>
#Jaspreet Kaur You need to add form tag outside the table. Please check my previous comment for it.

Automatically click the button for you if there is a date

I have a file called file.php which has:
$row2= pg_fetch_array($result2);
$website = 'http://validate/?id='.$newDateString;
<tbody>
<tr>
<td><?php echo'<a href="'.$website.'">'. $row['name']; ?></td>
The code sends to a file named jasmine.php the url with the date since jasmine contains a calendar. It sends it when you click on the date which contains a link to jasmine.
Jasmine.php:
<div id="content">
<h2>Hello, Please let me know which date you are looking for </h2>
<div><label style="font-size: 14pt; font-family: Verdana;">Posting Date: </label></div><br />
<input type="text" id="date" value= "<?php echo $_GET['id']?>" class="css-input" placeholder="Posting Date..." readonly="readonly" /><br /><br />
<input type="button" id="validate" value="Let's work!" class="btn" />
<input type="button" id="clear" value="Wanna go Again?" class="btn" />
</div>
<div id="contentarea2" style="display: none;">
<h2>Results:</h2>
<div class="view_alerts">
<table>
<tr>
<td>
Code
</td>
<td>
# Posted
</td>
</tr>
<tbody id="view_alerts_tbody">
</tbody>
</table>
</div>
</div>
In Jasmine, I get the date already written for me from file.php, all I need to do is click the button so it could show me a table, is there a way, that it clicks the button on its own if theres a date written, so when I go on that link, it already puts the date and takes me to the table in jasmine.php
Taking the code from the answer above the if statement here should evaluate the input fields value (id is '#date'). An empty value will default to false hence the ! before the selector.
jQuery(document).ready(function()
{
if(!$('#date').val()) {
jQuery("#Your-Button-ID").click();
}
});
You can use below JQuery code for your purpose. When page load is completed (i.e. Page is ready)
jQuery(document).ready(function()
{
jQuery("#Your-Button-ID").click();
});

Create Update Command at Same form With Many Data

I have a form with PHP language and I want to use it for inserting data, updating data, and deleting data at once. First row of table is for insert data, second table and other row is for edit or delete data.
So, I have create form like this:
FYI : UBAH is Update/Edit, HAPUS is Delete, TAMBAH is Add/Insert
I've already done with inserting data, but I'm stuck while Updating or deleting data because the ID that the button I choose is always at last ID (in picture is the ID always refer to "Sarjana") so it doesn't works because I can only update and delete the last data that called at table.
So, what I want to ask is, how to make it works without AJAX? Because what I search in Google, they said it can be done with AJAX, but I don't understand AJAX (except it will stuck forever without ajax). And I don't want to create an href link just like "beginner example" that throw it to another pages then edit/delete it.
Note: It's just like a listview at ASP but I want to do it at PHP code. Final Words, I want to create Inline editing at my PHP code.
I hope You understand and sorry for my bad English.
Add a unique id to each submit button:
<input type="submit" name="submit[1]" value="Submit">
<input type="submit" name="submit[2]" value="Submit">
<input type="submit" name="submit[3]" value="Submit">
Then you grab the id from $_POST or $_GET. For example:
Array
(
[submit] => Array
(
[3] => Submit
)
)
Edit:
Take this concept - HTML form elements to PHP arrays - and apply it to the other elements.
<table>
<tr>
<th>Program</th>
<th>Department</th>
<th></th>
</tr>
<tr>
<td><input type="text" name="program[1]" value=""></td>
<td><input type="text" name="department[1]" value=""></td>
<td>
<input type="submit" name="edit[1]" value="Edit">
<input type="submit" name="delete[1]" value="Delete">
</td>
</tr>
<tr>
<td><input type="text" name="program[2]" value=""></td>
<td><input type="text" name="department[2]" value=""></td>
<td>
<input type="submit" name="edit[2]" value="Edit">
<input type="submit" name="delete[2]" value="Delete">
</td>
<tr>
<td><input type="text" name="program[3]" value=""></td>
<td><input type="text" name="department[3]" value=""></td>
<td>
<input type="submit" name="edit[3]" value="Edit">
<input type="submit" name="delete[3]" value="Delete">
</td>
</tr>
</table>
Again: use the unique IDs to set each row's form elements apart.
Here's one method for grabbing the data in PHP. Obviously you need to get the ID from the submit button and then use that ID to reference elements in the $_POST or $_GET array.
if ( isset($_POST['edit']) && is_array($_POST['edit']) ) {
$key = key($_POST['edit']);
$program = $_POST['program'][$key];
$department = $_POST['department'][$key];
}

PHP POST value from a looped table

Please help me out of this.....
I am designing a table which is inside a form.
the table is generated based on while loop.
In each row there is a download button.
when i click download the POST value should get the same row information.
But my POST variable is giving me the last row information only.
I tried using input-type as hidden... But it did not work
Here is the code for your reference
enter code here
<form name="simpleform" method="post" action="insert.php">
<?php
$data = "environment";
$user_name = $_SESSION['username'];
$serch = mysql_query("SELECT * FROM data WHERE (data_category = '" . $data . "') ");
while ($record=mysql_fetch_assoc($serch))
{?>
<tr class="warning">
<td >
<input type="text" value=<?php echo $record['data_ID'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_ID'];?> name="dataid" />
</td>
<td >
<input type="text" value=<?php echo $record['data_name'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_name'];?> name="dataname" />
</td>
<td >
<input type="text" value=<?php echo $record['data_downloads'];?> readonly="readonly">
<input type="hidden" value=<?php echo $record['data_downloads'];?> name="datadown" />
</td>
<td >
<input type="text" value="" >
<input type="hidden" value="" name="datause" />
</td>
<td>
<input type="submit" name="simplesubmit" value="Go to download" />
</td>
</tr>
<?php }
exit;
?>
</tbody>
</form>
The problem is that you are using the same name attribute for all your controls. Thus, when PHP receives the form, they get overwritten, and you only see the last value of the form.
The simplest way to avoid that is just appending [] to the end of your names -- eg name=dataid[]. This will make PHP take all arguments as an array, so you don't lose data.
The second problem, is that your submit button also has the same name - you should diversify it by using some row-specific data in its name, such as 'name="submit-'.$record['data_name'].'"'
For more info, more code from you is needed, such as what are the data you are printing like.
Every post button can have its name and value, so if you change your code to produce a traceable post button name you can just do whatever you want with that.
<table>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[1]" value="OK" />
</tr>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[2]" value="OK" />
</tr>
</table>
When the form is posted its very easy to capture which button is clicked;
if ($_POST["submit"]) {
$id = key($_POST["submit"]);
}
Thanks for info... and good response. As you said , i replaced the same and saw the post value is giving me all arguments as array. My purpose is to let the client download file that he clicks. so if the client click the first row button in the table, the post value should get only that Data_name. So that i can run a query to get the URL of that data_name and download

Categories