form submit only giving last value in a loop codeigniter - php

Here is my view
<?php for($i=0;$i<count($acb['def']);$i++) { ?>
<input type="text" name="xyz" value="<?php echo $abc['def'][$i]?>" />?>
Here is my controller
$xxx=$this->input->post('xyz')
Now when i submit the form the last value only gets posted to controller
then i found that the name is same for all fields so it takes last value , so i changed the input name as
name ='xyz[$i]'
Now i need to post values , How to post values with this

You need to send name as array rename it to 'xyz[]' here
<input type="text" name="xyz[]" value="<?php echo $abc['def'][$i]?>" />?>

You use to below code...
<?php for($i=0;$i<count($acb['def']);$i++) { ?>
<input type="text" name="xyz[]" value="<?php echo $abc['def'][$i]?>" />
?>

Related

Codeigniter set_value() foreach loop

I generate a form, which mostly consists of input fields that are already populated with values from the db.
So I do this currently like so:
<input id="misc" name="misc" value="<?php echo $workout['misc']; ?>" />
But when I try and do this:
<input id="misc" name="misc" value="<?php echo set_value($workout['misc']); ?>" />
along with a validation rule, the form does reload itself, the error message does display BUT the form is reset
What am I doing wrong?
As per the manual:
Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
The above form will show "0" when loaded for the first time.
Hence in your case:
<input id="misc" name="misc" value="<?php echo set_value('misc', $workout['misc']); ?>" />
OR
<input id="misc" name="misc" value="<?php echo set_value('misc'); ?>" />
Documentation:
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

How to use GET to retrieve check box selected value in php

I am using Mysql PDO query to fetch the required result and it is saved in and array $newfamily. Now with the help of this array I am implementing check-box with the given code-
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<input type='checkbox'<?php echo $name;?>"><?php echo $name;?></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
Now in specific_family.php how can I retrieve al the selected check-box values in an array ?
Also when I use back button of browser I can see previously selected values as ticked. How can I remove this ?
Please guide.
The checkbox should have:
A label
The checkbox needs:
A name attribute
A value attribute
It must not have:
An end tag (unless you are using XHTML)
So:
<label>
<input type="checkbox"
name="new_families[]"
value="<?php echo htmlspecialchars($name); ?>">
<?php echo htmlspecialchars($name); ?>
</label>
The values of all the checked checkboxes will then appear in the array $_GET['new_families'] when the form is submitted.
If you add the name attribute to your input thus:
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<label for="<?php echo $name?>"><?php echo $name?></label>
<input type='checkbox' name="<?php echo $name;?>" id="<?php echo $name;?>"></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
then your checkboxes will show up by name in your $_GET array.
Hope that helps :)

Creating an Array with PHP

I have a form with multiple text inputs that all have the same name. How would I process that with my PHP when the user submits the form?
HTML:
<input type="text" name="interest"/>
I've assumed you're using POST.
You would use
<input type="text" name="interest[]">
Then on the post page, you could use:
foreach($_POST['interest'] as $i){
echo $i. "<br>";
}
or whichever method you wanted to use to get the POST data.
You could also do something like:
<input type="text" name="interest[music]"/>
<input type="text" name="interest[food]"/>
You can then call this data by using:
<?php echo $_POST['interest']['music']; ?>
<input type="text" name="interest[]"/>
You should add square brackets. This triggers PHP to put them in an array like this:
HTML
<input type="text" name="interest[]"/>
<input type="text" name="interest[]"/>
PHP
//Get values
var_dump($_POST['interest']);
Use brackets in your input field to create an array of POST values:
<input type="text" name="interest[]"/>
<?php
var_dump($_POST['interest']); // will be an array
?>

Is POST method used to send user defined inputs only

Well, I have 2 php files. The first one user form.php has an html form to collect user inputs and send those data to other php file mysql_insert.php via POST method.
The problem is that along with 6 user inputs, I also have to transfer one more variable count(as discussed in code below) which is not input from user.
I tired following approach:
user form.php--->
<?php
$count=file_get_contents("cnt.txt","r");
$count=$count+1;
echo"Welcome!<br/>You are student number $count.<br/>";
?>
<html>
<p>Fill in the following information to save your marksheet to the database:</p><br/>
<form action="mysql_insert.php" method="POST">
Name:<input type="text" name="name" value=""/><br/><br/>
Marks(out of 100)<br/>
Subject 1:<input type="text" name="sub1" value=""/><br/>
Subject 2:<input type="text" name="sub2" value=""/><br/>
Subject 3:<input type="text" name="sub3" value=""/><br/>
Subject 4:<input type="text" name="sub4" value=""/><br/>
Subject 5:<input type="text" name="sub5" value=""/><br/><br/>
<p name="count" value="$count"></p>
<input type="submit" value="Submit"/>
</form>
</html>
I simply named an empty P element as count and set its value as $count to send it along with input variables(correct me if there is something wrong here, I am very novice to php).
And on the receiving end I used following code:
mysql_insert.php--->
<?php
require("connect.php");
$name=$_POST['name'];
$s1=$_POST['sub1'];
$s2=$_POST['sub2'];
$s3=$_POST['sub3'];
$s4=$_POST['sub4'];
$s5=$_POST['sub5'];
$count=$_POST['count'];
.
.
.
?>
Now on the line $count=$_POST['count']; the browser is throwing the error:
Notice: Undefined index: count in C:\xampp\htdocs\Vikas-117-PHP\level 3\mysql_insert.php on line 10
It seems the count is not being posted to this file.
Please guide me where I am wrong.
P.S.: I can of course use the file_get_contents() in the mysql_insert.php and get the count value directly in this file, but I am considering that way as my last option.
So please help if the non user-input variable can be posted via forms???
Thanks a million!!!
you have to put your $count variable in a hidden field in your form.. The data which are put in form inputs only get posted in form submit..
<input type="hidden" name="count" value="<?php echo $count ?>">
<p name="count" value="$count"></p>
you can not use directly any html tag for post data only use html form fields for posting data.
for use html tag values post you need to use js/ajax/php
<p name="count"><?php echo $count;?></p>
or better you use
<textarea name="count"><?php echo $count;?></textarea>
or for data not showing use hidden field
and get by name this p value or use class or id for get and post data using ajax
<p name="count" value="$count"></p>
This is not an input element so you cant post directly. You can however use jquery to get the value and POST using js.
You need to have the value inside input type text or hidden if you want to do the way you are doing now.
Change this line : <p name="count" value="$count"></p>
to the : <input type="hidden" name="count" value="<?=$count?>" />
You can use hidden elements like the following
use
<input type="hidden" value="<?php echo $count; ?>" name="count"/>
instead of
<p name="count" value="$count"></p>
Instead of
<p name="count" value="$count"></p>
Use
<input type="hidden" name="count" value="<?php echo $count;?>">
this line will not work : <p name="count" value="$count"></p>
you can use <input type="hidden" name="count" value="<?php echo $count; ?>" />

pass value from page 1 to page 2 and display the value

this my id num column with a value
<td>ID NUMBER:</td>
<td><input type="hidden" name="txtEmpID" value="<?php echo $this->getArr['id']?>">
<input type="text" <?php echo (isset($this->postArr['EditMode']) && $this->postArr['EditMode']=='1') ? '' : 'disabled'?> name="txtEmployeeId2" value="<?php echo (isset($this->postArr['txtEmployeeId']))?$this->postArr['txtEmployeeId']:$edit[0][5]?>" maxlength="50">
how can to pass value from column txtEmpID to another page(reset.php) and display the value in the reset.php
this is reset.php
<tr>
<td align="right">Employee ID </td>
<td align="left"><input name="staffid" type="text" class="textfield_login"
value="how to display the value from 1st page into here?"/></td>
</tr>
help me please
Wrap the elements in a form and post the form a page.Then you can access the post data via $_POST if your method is post and $_GET if your method in form is get.
<form method="post" action="reset.php">
<td>ID NUMBER:</td>
<td><input type="hidden" name="txtEmpID" value="<?php echo $this->getArr['id']?>">
<input type="text" <?php echo (isset($this->postArr['EditMode']) && $this->postArr['EditMode']=='1') ? '' : 'disabled'?> name="txtEmployeeId2" value="<?php echo (isset($this->postArr['txtEmployeeId']))?$this->postArr['txtEmployeeId']:$edit[0][5]?>" maxlength="50">
</td>
</form>
In reset.php
echo $_POST['txtEmpID'];//Will contain the value of txtEmpID
There are two ways I can think of to do this.
The first is to use a form.
<form action="reset.php" method="get">
<input type="text" id="txtEmployeeId2" <?php echo (isset($this->postArr['EditMode']) && $this->postArr['EditMode']=='1') ? '' : 'disabled'?> name="txtEmployeeId2" value="<?php echo (isset($this->postArr['txtEmployeeId']))?$this->postArr['txtEmployeeId']:$edit[0][5]?>" maxlength="50">
<input type="submit" value="Submit">
</form>
The second
is to use Javascript/jQuery to get the value and pass it through the URL as an equivalent GET request.
However, you will need some sort of trigger to call the javascript function, like a button, for example.
<button onclick="sendValue()">Submit</button>
<script type="text/javascript">
function sendValue()
{
parent.location = 'reset.php?txtEmployeeId2='+document.getElementById("txtEmployeeId2").value;
}
Note that this will require you to add the ID attribute to your textbox.
For both solutions, you simply have to change the line in your reset.php file:
This is assuming that both the name attribute of your input and the ID attribute are the same. Replace "txtEmployeeId2" with whatever name or ID you want to give to the text box inputs.
<input name="staffid" type="text" class="textfield_login" value="how to display the value from 1st page into here?"/>
To
<input name="staffid" type="text" class="textfield_login" value="<?php echo $_GET["txtEmployeeId2"];?>"/>
You can also use session. ($_SESSION superglobal array)

Categories